project-jcarbon / jcarbon

0 stars 1 forks source link

Generating default locale carbon intensity as a Java enum #6

Open atpoverload opened 3 months ago

atpoverload commented 3 months ago

Currently our proposed implementation to retrieve the default carbon intensities for locales is loaded from a csv. We could generate a Java enum from a carbon intensity csv as part of the codebase. Possible initial layout generated from https://github.com/jlee5205/jRAPL/blob/main/src/jcarbon/src/resources/emissions/WorldIntensity.csv:

package jcarbon.emissions;

public enum CarbonLocale {
    ABW("Aruba", 542.8195532899147),
    AFG("Afghanistan", 108.65829596412556),
    AGO("Angola", 175.60894163125997),
    AIA("Anguilla", 475.00000000000006),
    ALA("Aland Islands", 475.00000000000006),
    ...
    UNKNOWN("???", 0);

    public final String localeName;
    public final double carbonIntensity;

    CarbonLocale(String localeName, double carbonIntensity) { 
        this.localeName = localeName;
        this.carbonIntensity = carbonIntensity;
    }

    CarbonLocale fromLocale(Locale locale) {
        return CarbonLocale.valueOf(locale.getISO3Country());
    }

    CarbonLocale getDefault() {
        return CarbonLocale.fromLocale(Locale.getDefault());
    }
}

We can also leverage this in other emissions constructs to deal with just carbon locales:

    // this could easily live in the converter now!
    LocaleCarbonIntensity fromLocale(CarbonLocale locale) {
        return new EmissionsConverter(locale.carbonIntensity);
    }