View Javadoc
1   /*
2    * Copyright (C) 2020 sw4j.org
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16   */
17  package org.sw4j.tool.barcode.random.config;
18  
19  import com.fasterxml.jackson.annotation.JsonProperty;
20  import java.util.Objects;
21  
22  /**
23   * <p>
24   * This class configures a encoding for a code to generate. The code may be separated into equal groups with a
25   * separation character.
26   * </p>
27   * @author Uwe Plonus &lt;u.plonus@gmail.com&gt;
28   */
29  public class EncodingConfig {
30  
31      /**
32       * <p>
33       * The type of the encoding.
34       * </p>
35       */
36      private final String type;
37  
38      /**
39       * <p>
40       * The optional count of characters until the next separation character.
41       * </p>
42       */
43      private final Integer sepCount;
44  
45      /**
46       * <p>
47       * The optional separation character.
48       * </p>
49       */
50      private final Character sepChar;
51  
52      /**
53       * <p>
54       * Create a new {@code EncodingConfig} with the given parameters.
55       * </p>
56       * @param type the type of the encoding.
57       * @param sepCount the number of characters after which a separation should occur. May be {@code null}.
58       * @param sepChar the separation character. May be {@code null}.
59       */
60      public EncodingConfig(
61              @JsonProperty("type") final String type,
62              @JsonProperty("sep-count") final Integer sepCount,
63              @JsonProperty("sep-char") final Character sepChar) {
64          if (type == null) {
65              throw new IllegalArgumentException(String.format("%s: Missing type", getClass().getSimpleName()));
66          }
67          this.type = type;
68          this.sepCount = sepCount;
69          this.sepChar = sepChar;
70      }
71  
72      public String getType() {
73          return type;
74      }
75  
76      public Integer getSepCount() {
77          return sepCount;
78      }
79  
80      public Character getSepChar() {
81          return sepChar;
82      }
83  
84      @Override
85      public int hashCode() {
86          int hash = 3;
87          hash = 19 * hash + Objects.hashCode(this.type);
88          hash = 19 * hash + Objects.hashCode(this.sepCount);
89          hash = 19 * hash + Objects.hashCode(this.sepChar);
90          return hash;
91      }
92  
93      @Override
94      public boolean equals(Object obj) {
95          if (this == obj) {
96              return true;
97          }
98          if (obj == null) {
99              return false;
100         }
101         if (getClass() != obj.getClass()) {
102             return false;
103         }
104         final EncodingConfig./../../../org/sw4j/tool/barcode/random/config/EncodingConfig.html#EncodingConfig">EncodingConfig other = (EncodingConfig) obj;
105         if (!Objects.equals(this.type, other.type)) {
106             return false;
107         }
108         if (!Objects.equals(this.sepCount, other.sepCount)) {
109             return false;
110         }
111         return Objects.equals(this.sepChar, other.sepChar);
112     }
113 
114     @Override
115     public String toString() {
116         StringBuilder sb = new StringBuilder();
117         sb.append(type);
118         if (sepCount != null) {
119             sb.append("(").append(sepCount).append("|").append(sepChar).append(")");
120         }
121         return sb.toString();
122     }
123 
124     public static Builder builder() {
125         return new Builder();
126     }
127 
128     public static class Builder {
129 
130         /**
131          * <p>
132          * The type of the encoding.
133          * </p>
134          */
135         private String type;
136 
137         /**
138          * <p>
139          * The optional count of characters until the next separation character.
140          * </p>
141          */
142         private Integer sepCount;
143 
144         /**
145          * <p>
146          * The optional separation character.
147          * </p>
148          */
149         private Character sepChar;
150 
151         public Builder() {
152         }
153 
154         public Builder setType(final String type) {
155             this.type = type;
156             return this;
157         }
158 
159         public Builder setSepCount(final Integer sepCount) {
160             this.sepCount = sepCount;
161             return this;
162         }
163 
164         public Builder setSepChar(final Character sepChar) {
165             this.sepChar = sepChar;
166             return this;
167         }
168 
169         public EncodingConfig build() {
170             return new EncodingConfig(type, sepCount, sepChar);
171         }
172 
173     }
174 
175 }