CodeType.java

/*
 * Copyright (C) 2019 sw4j.org
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.sw4j.tool.barcode.random.config;

import com.google.zxing.BarcodeFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * This enum enumerates all available barcode types that are usable in the application.
 * </p>
 * @author Uwe Plonus &lt;u.plonus@gmail.com&gt;
 */
public enum CodeType {

    /**
     * <p>
     * Barcode type Aztec Code.
     * </p>
     */
    AZTEC("aztec", BarcodeFormat.AZTEC),
    /**
     * <p>
     * Barcode type Data Matrix.
     * </p>
     */
    DATAMATRIX("datamatrix", BarcodeFormat.DATA_MATRIX),
    /**
     * <p>
     * Barcode type QR code.
     * </p>
     */
    QRCODE("qrcode", BarcodeFormat.QR_CODE),
    ;

    /**
     * <p>
     * A lookup table to resolve a type name (e.g. qrcode) to the corresponding enum (e.g. QRCODE).
     * </p>
     */
    private static final Map<String, CodeType> CODE_TYPES = new HashMap<>();

    /**
     * <p>
     * The name of the type. Can be used for lookup by name.
     * </p>
     */
    private final String type;

    /**
     * <p>
     * A mapping to the ZXing barcode format.
     * </p>
     */
    private final BarcodeFormat format;

    /**
     * <p>
     * Static initializer to fill the lookup table {@link #CODE_TYPES}.
     * </p>
     */
    static {
        for (CodeType codeType: CodeType.values()) {
            CODE_TYPES.put(codeType.getType(), codeType);
        }
    }

    /**
     * <p>
     * The constructor for a concrete enum value.
     * </p>
     * @param type the type of the barcode format (e.g. qrcode).
     * @param format the ZXing format of the barcode (e.g. QR_CODE).
     */
    CodeType(final String type, final BarcodeFormat format) {
        this.type = type;
        this.format = format;
    }

    /**
     * <p>
     * Return the type of the barcode (e.g. qrcode).
     * </p>
     * @return the type of the barcode.
     */
    public String getType() {
        return type;
    }

    /**
     * <p>
     * Return the ZXing barcode format of the barcode (e.g. QR_CODE).
     * </p>
     * @return the ZXing barcode format.
     */
    public BarcodeFormat getFormat() {
        return format;
    }

    /**
     * <p>
     * Return an enum by doing a lookup with the type string (e.g. qrcode).
     * </p>
     * @param type the type string to look up (e.g. qrcode).
     * @return the enum with the given type string.
     */
    public static CodeType lookup(final String type) {
        return CODE_TYPES.get(type);
    }

}