View Javadoc
1   /*
2    * Copyright (C) 2019 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.JsonCreator;
20  import com.fasterxml.jackson.annotation.JsonProperty;
21  import java.util.Collections;
22  import java.util.List;
23  
24  /**
25   * <p>
26   * This class configures the generation and presentation.
27   * </p>
28   * <p>
29   * To create a new instance programmatically use the
30   * {@link org.sw4j.tool.barcode.random.config.OutputConfig.Builder Builder} which can be obtained by the method
31   * {@link #builder()}.
32   * </p>
33   * <p>
34   * This class is immutable.
35   * </p>
36   * @author Uwe Plonus &lt;u.plonus@gmail.com&gt;
37   */
38  public class GenerationConfig {
39  
40      /**
41       * <p>
42       * The size (in bits) of the random number to create.
43       * </p>
44       */
45      private final int size;
46  
47      /**
48       * <p>
49       * The encoding of the predefined codes.
50       * </p>
51       */
52      private final EncodingConfig encoding;
53  
54      /**
55       * <p>
56       * The configurations of the generated barcodes.
57       * </p>
58       */
59      private final List<CodeConfig> codes;
60  
61      /**
62       * <p>
63       * The configuration of the additional encoded random numbers.
64       * </p>
65       */
66      private final List<EncodingConfig> encodings;
67  
68      /**
69       * <p>
70       * Create a new {@code GenerationConfig} with the given random size (in bits) or source encoding, barcode formats
71       * and additional representations. If the encoding is set the random size is ignored.
72       * </p>
73       * <p>
74       * This constructor is annotated so that a YAML or JSON parsed with jackson fasterxml can create an instance
75       * directly.
76       * </p>
77       * @param size the size of the random number generated (must be positive and a multiple of 8).
78       * @param encoding the encoding of the predefined codes.
79       * @param codes the configuration of the barcodes generated.
80       * @param encodings the configuration of the additional encodings generated.
81       * @throws IllegalArgumentException if {@code size} is either negative or not a multiple of 8, or {@code code} and
82       *   {@code encodings} is both {@code null}.
83       */
84      @JsonCreator
85      public GenerationConfig(
86              @JsonProperty("size") final int size,
87              @JsonProperty("encoding") final EncodingConfig encoding,
88              @JsonProperty("codes") final List<CodeConfig> codes,
89              @JsonProperty("encodings") final List<EncodingConfig> encodings) {
90          if (encoding == null) {
91              if (size < 0) {
92                  throw new IllegalArgumentException(String.format("%s: size is negative",
93                          getClass().getSimpleName()));
94              }
95              if (size % 8 != 0) {
96                  throw new IllegalArgumentException(String.format("%s: size is not a multiple of 8",
97                          getClass().getSimpleName()));
98              }
99          }
100         if ((codes == null || codes.isEmpty()) && (encodings == null || encodings.isEmpty())) {
101             throw new IllegalArgumentException(String.format("%s: either codes or encodings is needed",
102                     getClass().getSimpleName()));
103         }
104         this.size = size;
105         this.codes = codes;
106         this.encodings = encodings;
107         this.encoding = encoding;
108     }
109 
110     /**
111      * <p>
112      * Return the size of the random number to be created.
113      * </p>
114      * @return the size of the random number.
115      */
116     public int getSize() {
117         return size;
118     }
119 
120     /**
121      * <p>
122      * Return the encoding of the predefined values.
123      * </p>
124      * @return the encoding.
125      */
126     public EncodingConfig getEncoding() {
127         return encoding;
128     }
129 
130     /**
131      * <p>
132      * Return the configurations of the barcodes to create.
133      * </p>
134      * @return the configuration of the barcodes to create.
135      */
136     public List<CodeConfig> getCodes() {
137         return Collections.unmodifiableList(codes);
138     }
139 
140     /**
141      * <p>
142      * Return the configurations of the additional encoding of the random number to create.
143      * </p>
144      * @return the configuration of the additional encodings.
145      */
146     public List<EncodingConfig> getEncodings() {
147         return Collections.unmodifiableList(encodings);
148     }
149 
150     /**
151      * <p>
152      * Return a {@link org.sw4j.tool.barcode.random.config.RandomConfig.Builder Builder} that can be used to build a
153      * config.
154      * </p>
155      * @return a {@link org.sw4j.tool.barcode.random.config.RandomConfig.Builder Builder}.
156      */
157     public static Builder builder() {
158         return new Builder();
159     }
160 
161 
162     /**
163      * <p>
164      * This class is a Builder to build a {@link org.sw4j.tool.barcode.random.config.GenerationConfig RandomConfig}
165      * programmatically. All methods to set the values are fluent to ease the building of a configuration.
166      * <p>
167      * This class is not thread save.
168      */
169     public static class Builder {
170 
171         /**
172          * <p>
173          * The size (in bits) of the random number to create.
174          * </p>
175          */
176         private int size;
177 
178         /**
179          * <p>
180          * The encoding of the predefined values.
181          * </p>
182          */
183         private EncodingConfig encoding;
184 
185         /**
186          * <p>
187          * The configurations of the generated barcodes.
188          * </p>
189          */
190         private List<CodeConfig> codes;
191 
192         /**
193          * <p>
194          * The configuration of the additional encoded random numbers.
195          * </p>
196          */
197         private List<EncodingConfig> encodings;
198 
199         /**
200          * <p>
201          * Create a new {@code Builder}.
202          * </p>
203          */
204         public Builder() {
205         }
206 
207         /**
208          * <p>
209          * Set the size (in bits) of the random number generator.
210          * </p>
211          * @param size the size of the random number generator.
212          * @return the builder for a fluent interface.
213          */
214         public Builder setSize(final int size) {
215             this.size = size;
216             return this;
217         }
218 
219         /**
220          * <p>
221          * Set the encoding of the predefined values.
222          * </p>
223          * @param encoding the encoding.
224          * @return the builder for a fluent interface.
225          */
226         public Builder setEncoding(EncodingConfig encoding) {
227             this.encoding = encoding;
228             return this;
229         }
230 
231         /**
232          * <p>
233          * Set the configurations of the barcodes generated.
234          * </p>
235          * @param codes the configuration of the barcodes.
236          * @return the builder for a fluent interface.
237          */
238         public Builder setCodes(final List<CodeConfig> codes) {
239             this.codes = codes;
240             return this;
241         }
242 
243         /**
244          * <p>
245          * Set the configurations of the additional encodings generated.
246          * </p>
247          * @param encodings  the configuration of the encodings.
248          * @return the builder for a fluent interface.
249          */
250         public Builder setEncodings(final List<EncodingConfig> encodings) {
251             this.encodings = encodings;
252             return this;
253         }
254 
255         /**
256          * <p>
257          * Build a new {@link org.sw4j.tool.barcode.random.config.GenerationConfig RandomConfig} with the parameters set.
258          * </p>
259          * @return the new created {@link org.sw4j.tool.barcode.random.config.GenerationConfig RandomConfig}.
260          * @throws IllegalArgumentException if {@code size} is either negative or not a multiple of 8, or {@code code}
261          *   and {@code encodings} is both {@code null}.
262          */
263         public GenerationConfig build() {
264             return new GenerationConfig(size, encoding, codes, encodings);
265         }
266 
267     }
268 
269 }