OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

package org.openconcerto.modules.label.gs1;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;

public class GS1AIElements {
    static final HashMap<String, GS1ApplicationIdentifier> GS1_128_AI = new HashMap<>();
    static {
        GS1_128_AI.put("00", new GS1ApplicationIdentifier(0, 18, false, false));
        GS1_128_AI.put("01", new GS1ApplicationIdentifier(0, 14, false, false));
        GS1_128_AI.put("02", new GS1ApplicationIdentifier(0, 14, false, false));
        GS1_128_AI.put("10", new GS1ApplicationIdentifier(0, 20, true, false));
        GS1_128_AI.put("11", new GS1ApplicationIdentifier(0, 6, false, false));
        GS1_128_AI.put("12", new GS1ApplicationIdentifier(0, 6, false, false));
        GS1_128_AI.put("13", new GS1ApplicationIdentifier(0, 6, false, false));
        GS1_128_AI.put("14", new GS1ApplicationIdentifier(0, 6, false, false));
        GS1_128_AI.put("15", new GS1ApplicationIdentifier(0, 6, false, false));
        GS1_128_AI.put("17", new GS1ApplicationIdentifier(0, 6, false, false));
        GS1_128_AI.put("20", new GS1ApplicationIdentifier(0, 2, false, false));
        GS1_128_AI.put("21", new GS1ApplicationIdentifier(0, 20, true, false));
        GS1_128_AI.put("240", new GS1ApplicationIdentifier(0, 30, true, false));
        GS1_128_AI.put("241", new GS1ApplicationIdentifier(0, 30, true, false));
        GS1_128_AI.put("242", new GS1ApplicationIdentifier(0, 6, true, false));
        GS1_128_AI.put("250", new GS1ApplicationIdentifier(0, 30, true, false));
        GS1_128_AI.put("251", new GS1ApplicationIdentifier(0, 30, true, false));
        GS1_128_AI.put("253", new GS1ApplicationIdentifier(13, 30, true, false));
        GS1_128_AI.put("254", new GS1ApplicationIdentifier(0, 20, true, false));
        GS1_128_AI.put("255", new GS1ApplicationIdentifier(13, 25, true, false));
        GS1_128_AI.put("30", new GS1ApplicationIdentifier(0, 8, true, false));
        GS1_128_AI.put("310", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("311", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("312", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("313", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("314", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("315", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("316", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("320", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("321", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("322", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("323", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("324", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("325", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("326", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("327", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("328", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("329", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("330", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("331", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("332", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("333", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("334", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("335", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("336", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("340", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("341", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("342", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("343", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("344", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("345", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("346", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("347", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("348", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("349", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("350", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("351", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("352", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("353", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("354", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("355", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("356", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("357", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("360", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("361", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("362", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("363", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("364", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("365", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("366", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("367", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("368", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("369", new GS1ApplicationIdentifier(0, 6, false, true));
        GS1_128_AI.put("37", new GS1ApplicationIdentifier(0, 8, true, false));
        GS1_128_AI.put("390", new GS1ApplicationIdentifier(0, 15, true, true));
        GS1_128_AI.put("391", new GS1ApplicationIdentifier(3, 18, true, true));
        GS1_128_AI.put("392", new GS1ApplicationIdentifier(0, 15, true, true));
        GS1_128_AI.put("393", new GS1ApplicationIdentifier(3, 18, true, true));
        GS1_128_AI.put("400", new GS1ApplicationIdentifier(0, 30, true, false));
        GS1_128_AI.put("401", new GS1ApplicationIdentifier(0, 30, true, false));
        GS1_128_AI.put("402", new GS1ApplicationIdentifier(0, 17, false, false));
        GS1_128_AI.put("403", new GS1ApplicationIdentifier(3, 30, true, false));
        GS1_128_AI.put("410", new GS1ApplicationIdentifier(0, 17, false, false));
        GS1_128_AI.put("411", new GS1ApplicationIdentifier(0, 17, false, false));
        GS1_128_AI.put("412", new GS1ApplicationIdentifier(0, 17, false, false));
        GS1_128_AI.put("413", new GS1ApplicationIdentifier(0, 17, false, false));
        GS1_128_AI.put("414", new GS1ApplicationIdentifier(0, 17, false, false));
        GS1_128_AI.put("420", new GS1ApplicationIdentifier(0, 20, true, false));
        GS1_128_AI.put("421", new GS1ApplicationIdentifier(0, 0, true, false));
        GS1_128_AI.put("422", new GS1ApplicationIdentifier(0, 3, false, false));
        GS1_128_AI.put("423", new GS1ApplicationIdentifier(3, 15, true, false));
        GS1_128_AI.put("424", new GS1ApplicationIdentifier(0, 3, false, false));
        GS1_128_AI.put("425", new GS1ApplicationIdentifier(0, 3, false, false));
        GS1_128_AI.put("426", new GS1ApplicationIdentifier(0, 3, false, false));
        GS1_128_AI.put("7001", new GS1ApplicationIdentifier(0, 13, false, false));
        GS1_128_AI.put("7002", new GS1ApplicationIdentifier(0, 30, false, false));
        GS1_128_AI.put("7003", new GS1ApplicationIdentifier(0, 10, false, false));
        GS1_128_AI.put("7004", new GS1ApplicationIdentifier(0, 4, true, false));
        GS1_128_AI.put("8001", new GS1ApplicationIdentifier(0, 14, false, false));
        GS1_128_AI.put("8002", new GS1ApplicationIdentifier(0, 20, true, false));
        GS1_128_AI.put("8003", new GS1ApplicationIdentifier(14, 30, true, false));
        GS1_128_AI.put("8004", new GS1ApplicationIdentifier(0, 30, true, false));
        GS1_128_AI.put("8005", new GS1ApplicationIdentifier(0, 6, false, false));
        GS1_128_AI.put("8006", new GS1ApplicationIdentifier(0, 18, false, false));
        GS1_128_AI.put("8007", new GS1ApplicationIdentifier(0, 30, true, false));
        GS1_128_AI.put("8008", new GS1ApplicationIdentifier(8, 12, true, false));
        GS1_128_AI.put("8018", new GS1ApplicationIdentifier(0, 18, false, false));
        GS1_128_AI.put("8020", new GS1ApplicationIdentifier(0, 25, true, false));
        GS1_128_AI.put("8100", new GS1ApplicationIdentifier(0, 6, false, false));
        GS1_128_AI.put("8101", new GS1ApplicationIdentifier(0, 10, false, false));
        GS1_128_AI.put("8102", new GS1ApplicationIdentifier(0, 2, false, false));
        GS1_128_AI.put("8110", new GS1ApplicationIdentifier(0, 30, true, false));
        GS1_128_AI.put("8200", new GS1ApplicationIdentifier(0, 70, true, false));
        GS1_128_AI.put("90", new GS1ApplicationIdentifier(0, 30, true, false));
        GS1_128_AI.put("91", new GS1ApplicationIdentifier(0, 30, true, false));
        GS1_128_AI.put("92", new GS1ApplicationIdentifier(0, 30, true, false));
        GS1_128_AI.put("93", new GS1ApplicationIdentifier(0, 30, true, false));
        GS1_128_AI.put("94", new GS1ApplicationIdentifier(0, 30, true, false));
        GS1_128_AI.put("95", new GS1ApplicationIdentifier(0, 30, true, false));
        GS1_128_AI.put("96", new GS1ApplicationIdentifier(0, 30, true, false));
        GS1_128_AI.put("97", new GS1ApplicationIdentifier(0, 30, true, false));
        GS1_128_AI.put("98", new GS1ApplicationIdentifier(0, 30, true, false));
        GS1_128_AI.put("99", new GS1ApplicationIdentifier(0, 30, true, false));
    }

    private final ArrayList<String> keysAndValues = new ArrayList<>();

    public GS1AIElements() {

    }

    public boolean isEmpty() {
        return this.keysAndValues.isEmpty();
    }

    public boolean containsKey(String key) {
        final int size = this.keysAndValues.size();
        for (int i = 0; i < size; i += 2) {
            if (this.keysAndValues.get(i).equals(key)) {
                return true;
            }
        }
        return false;
    }

    public boolean containsValue(String value) {
        final int size = this.keysAndValues.size();
        for (int i = 1; i < size; i += 2) {
            if (this.keysAndValues.get(i).equals(value)) {
                return true;
            }
        }
        return false;
    }

    public String getKey(int index) {
        return this.keysAndValues.get(index * 2);
    }

    public String getValue(int index) {
        return this.keysAndValues.get(1 + index * 2);
    }

    public String get(String key) {
        final int size = this.keysAndValues.size();
        for (int i = 0; i < size; i += 2) {
            if (this.keysAndValues.get(i).equals(key)) {

                return this.keysAndValues.get(i + 1);
            }
        }
        return null;
    }

    public String put(String key, String value) {
        GS1ApplicationIdentifier ai = GS1_128_AI.get(key);
        if (ai == null) {
            throw new IllegalArgumentException("AI " + key + " unknown");
        }
        if (ai.variableLength) {
            if (value.length() < ai.minLength) {
                throw new IllegalArgumentException("AI " + key + " value length must >= " + ai.minLength + " but is " + value.length() + " for value " + value);
            }
            if (value.length() > ai.length) {
                throw new IllegalArgumentException("AI " + key + " value length must be <= " + ai.length + " but is " + value.length() + " for value " + value);
            }
        } else {
            if (value.length() != ai.length) {
                throw new IllegalArgumentException("AI " + key + " value length must be " + ai.length + " but is " + value.length() + " for value " + value);
            }
        }

        final int size = this.keysAndValues.size();
        for (int i = 0; i < size; i += 2) {
            if (this.keysAndValues.get(i).equals(key)) {
                final String old = this.keysAndValues.get(i + 1);
                this.keysAndValues.set(i + 1, value);
                return old;
            }
        }
        this.keysAndValues.add(key);
        this.keysAndValues.add(value);
        return null;
    }

    public String remove(String key) {
        final int size = this.keysAndValues.size();
        for (int i = 0; i < size; i += 2) {
            if (this.keysAndValues.get(i).equals(key)) {
                this.keysAndValues.remove(i);
                return this.keysAndValues.remove(i);
            }
        }
        return null;
    }

    public void clear() {
        this.keysAndValues.clear();
    }

    public int size() {
        return this.keysAndValues.size() / 2;
    }

    public void dump(PrintStream out) {
        for (int i = 0; i < size(); i++) {
            out.print("(");
            out.print(getKey(i));
            out.print(")");
            out.println(getValue(i));
        }
        out.flush();
    }

    public static GS1ApplicationIdentifier getApplicationIdentifier(String k) {
        return GS1_128_AI.get(k);
    }

    public String formatHumanReadable() {
        StringBuilder b = new StringBuilder();
        int size = size();
        for (int i = 0; i < size; i++) {
            b.append('(');
            b.append(this.getKey(i));
            b.append(')');
            b.append(this.getValue(i));
        }
        return b.toString();
    }
}