EncodingConfig.java

/*
 * Copyright (C) 2020 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.fasterxml.jackson.annotation.JsonProperty;
import java.util.Objects;

/**
 * <p>
 * This class configures a encoding for a code to generate. The code may be separated into equal groups with a
 * separation character.
 * </p>
 * @author Uwe Plonus &lt;u.plonus@gmail.com&gt;
 */
public class EncodingConfig {

    /**
     * <p>
     * The type of the encoding.
     * </p>
     */
    private final String type;

    /**
     * <p>
     * The optional count of characters until the next separation character.
     * </p>
     */
    private final Integer sepCount;

    /**
     * <p>
     * The optional separation character.
     * </p>
     */
    private final Character sepChar;

    /**
     * <p>
     * Create a new {@code EncodingConfig} with the given parameters.
     * </p>
     * @param type the type of the encoding.
     * @param sepCount the number of characters after which a separation should occur. May be {@code null}.
     * @param sepChar the separation character. May be {@code null}.
     */
    public EncodingConfig(
            @JsonProperty("type") final String type,
            @JsonProperty("sep-count") final Integer sepCount,
            @JsonProperty("sep-char") final Character sepChar) {
        if (type == null) {
            throw new IllegalArgumentException(String.format("%s: Missing type", getClass().getSimpleName()));
        }
        this.type = type;
        this.sepCount = sepCount;
        this.sepChar = sepChar;
    }

    public String getType() {
        return type;
    }

    public Integer getSepCount() {
        return sepCount;
    }

    public Character getSepChar() {
        return sepChar;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 19 * hash + Objects.hashCode(this.type);
        hash = 19 * hash + Objects.hashCode(this.sepCount);
        hash = 19 * hash + Objects.hashCode(this.sepChar);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final EncodingConfig other = (EncodingConfig) obj;
        if (!Objects.equals(this.type, other.type)) {
            return false;
        }
        if (!Objects.equals(this.sepCount, other.sepCount)) {
            return false;
        }
        return Objects.equals(this.sepChar, other.sepChar);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(type);
        if (sepCount != null) {
            sb.append("(").append(sepCount).append("|").append(sepChar).append(")");
        }
        return sb.toString();
    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {

        /**
         * <p>
         * The type of the encoding.
         * </p>
         */
        private String type;

        /**
         * <p>
         * The optional count of characters until the next separation character.
         * </p>
         */
        private Integer sepCount;

        /**
         * <p>
         * The optional separation character.
         * </p>
         */
        private Character sepChar;

        public Builder() {
        }

        public Builder setType(final String type) {
            this.type = type;
            return this;
        }

        public Builder setSepCount(final Integer sepCount) {
            this.sepCount = sepCount;
            return this;
        }

        public Builder setSepChar(final Character sepChar) {
            this.sepChar = sepChar;
            return this;
        }

        public EncodingConfig build() {
            return new EncodingConfig(type, sepCount, sepChar);
        }

    }

}