DutyUnit.java

package com.tradecloud.domain.duties;

import org.apache.commons.lang3.ArrayUtils;

/**
 * The statistical units of {@code OtherDuty} values. Business decided there was
 * no need to keep this as static data as if rarely changes.
 * https://connect.devstream.net/display/Dev/Tariffing.
 */
public enum DutyUnit {

    ABSOLUTE_ALCOHOL("AA", null),
    AREA_M2("SM", "m2"),
    BRUSH_WIDTH("BW", null),
    BUTTON_SIZE("BS", null),
    CARAT("CT", null),
    CARDS("CR", null),
    GRAMS_ETHAMBUTOL("GE", null),
    INTERNATIONAL_UNIT("IU", null),
    KILOWATT("KW", null),
    LENGTH_CM("CM", "cm"),
    LENGTH_M("ME", "m"),
    LENGTH_MM("MM", "mm"),
    LITRE_GROSS_STORAGE("GS", null),
    MASS_G("GR", "g"),
    MASS_KG("KG", "kg"),
    MASS_T("KK", "t"),
    MASS_NETT_G("GN", null),
    MASS_NETT_KG("KN", null),
    MEDICAL_UNIT("MU", null),
    NUMBER("NO", "u", "(vru/pack)", "packs", "PACKS"),
    PAIRS("PR", "2u"),
    PART("NX", null),
    PERCENTAGE_LOCAL("LC", null),
    PERCENTAGE_PROPYL("PA", null),
    RELATIVE_DENSITY("RD", null),
    VOLUME_L("LI", "l"),
    VOLUME_M3("MC", "m3"),
    VOLUME_MM("ML", "ml"),
    VOLUME_AA_L("LA", null);

    private final String sarsCode;
    private final String[] sarsTariffServerCode;

    DutyUnit(String sarsCode, String... sarsTariffServerCode) {
        this.sarsCode = sarsCode;
        this.sarsTariffServerCode = sarsTariffServerCode;
    }

    public String getSarsCode() {
        return sarsCode;
    }

    public String[] getSarsTariffServerCode() {
        return sarsTariffServerCode;
    }

    public static DutyUnit getBySarsTariffServerCode(String unitCode) {
        for (DutyUnit unit : DutyUnit.values()) {
            if (unitCode != null && ArrayUtils.contains(unit.getSarsTariffServerCode(), unitCode))
                return unit;
        }
        return null;
    }

    public static DutyUnit getBySarsCode(String sarsCode) {
        for (DutyUnit unit : DutyUnit.values()) {
            if (sarsCode != null && unit.getSarsCode().equalsIgnoreCase(sarsCode))
                return unit;
        }
        return null;
    }
}