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 com.fasterxml.jackson.dataformat.yaml.YAMLMapper; 22 import java.io.File; 23 import java.io.IOException; 24 25 /** 26 * <p> 27 * The top level class for configuring the whole application. This class includes the configuration for the input file, 28 * the output file(s) and the random number generation and presentation. 29 * </p> 30 * <p> 31 * To create a new instance programmatically use the {@link org.sw4j.tool.barcode.random.config.Config.Builder Builder} 32 * which can be obtained by the method {@link #builder()}. 33 * </p> 34 * <p> 35 * This class is immutable. 36 * </p> 37 * @author Uwe Plonus <u.plonus@gmail.com> 38 */ 39 public class Config { 40 41 /** 42 * <p> 43 * The configuration for the input file. 44 * </p> 45 */ 46 private final FileConfig input; 47 48 /** 49 * <p> 50 * The configuration for the output file(s). 51 * </p> 52 */ 53 private final OutputConfig output; 54 55 /** 56 * <p> 57 * The configuration for the random number generation and presentation. 58 * </p> 59 */ 60 private final GenerationConfig random; 61 62 /** 63 * <p> 64 * The configuration for the predefined presentation. 65 * </p> 66 */ 67 private final GenerationConfig predefined; 68 69 /** 70 * <p> 71 * Create a new {@code Config} with the given part configurations. 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 input the input file configuration. 78 * @param output the output file(s) configuration. 79 * @param random the random generation and presentation configuration. 80 * @param predefined the predefined presentation configuration. 81 * @throws IllegalArgumentException if either {@code input}, {@code output} or {@code random} is {@code null}. 82 */ 83 @JsonCreator 84 public Config( 85 @JsonProperty("input") final FileConfig input, 86 @JsonProperty("output") final OutputConfig output, 87 @JsonProperty("random") final GenerationConfig random, 88 @JsonProperty("predefined") final GenerationConfig predefined) { 89 if (input == null) { 90 throw new IllegalArgumentException(String.format("%s: Missing input", getClass().getSimpleName())); 91 } 92 if (output == null) { 93 throw new IllegalArgumentException(String.format("%s: Missing output", getClass().getSimpleName())); 94 } 95 if (random == null && predefined == null) { 96 throw new IllegalArgumentException(String.format("%s: Missing random or predefined", 97 getClass().getSimpleName())); 98 } 99 this.input = input; 100 this.output = output; 101 this.random = random; 102 this.predefined = predefined; 103 } 104 105 /** 106 * <p> 107 * Return the input file configuration. 108 * </p> 109 * @return the input file configuration. 110 */ 111 public FileConfig getInput() { 112 return input; 113 } 114 115 /** 116 * <p> 117 * Return the output file(s) configuration. 118 * </p> 119 * @return the output file(s) configuration. 120 */ 121 public OutputConfig getOutput() { 122 return output; 123 } 124 125 /** 126 * <p> 127 * Return the random generation and presentation configuration. 128 * </p> 129 * @return the random generation and presentation configuration. 130 */ 131 public GenerationConfig getRandom() { 132 return random; 133 } 134 135 /** 136 * <p> 137 * Return the predefined presentation configuration. 138 * </p> 139 * @return the predefined presentation configuration. 140 */ 141 public GenerationConfig getPredefined() { 142 return predefined; 143 } 144 145 /** 146 * <p> 147 * A factory method to create a configuration from a YAML file. 148 * </p> 149 * @param configFile the YAML file to read. 150 * @return the parsed configuration. 151 * @throws IOException if the reading of the file fails. 152 */ 153 public static Config readYamlConfig(final File configFile) throws IOException { 154 YAMLMapper mapper = new YAMLMapper(); 155 return mapper.readValue(configFile, Config.class); 156 } 157 158 /** 159 * <p> 160 * Return a {@link org.sw4j.tool.barcode.random.config.Config.Builder Builder} that can be used to build a config. 161 * </p> 162 * @return a {@link org.sw4j.tool.barcode.random.config.Config.Builder Builder}. 163 */ 164 public static Builder builder() { 165 return new Builder(); 166 } 167 168 169 /** 170 * <p> 171 * This class is a Builder to build a {@link org.sw4j.tool.barcode.random.config.Config Config} programmatically. 172 * All methods to set the values are fluent to ease the building of a configuration. 173 * </p> 174 * <p> 175 * This class is not thread save. 176 * </p> 177 * @author Uwe Plonus <u.plonus@gmail.com> 178 */ 179 public static class Builder { 180 181 /** 182 * <p> 183 * The input file configuration. 184 * </p> 185 */ 186 private FileConfig input; 187 188 /** 189 * <p> 190 * The output file(s) configuration. 191 * </p> 192 */ 193 private OutputConfig output; 194 195 /** 196 * <p> 197 * The random number generation and presentation configuration. 198 * </p> 199 */ 200 private GenerationConfig random; 201 202 /** 203 * <p> 204 * The predefined presentation configuration. 205 * </p> 206 */ 207 private GenerationConfig predefined; 208 209 /** 210 * <p> 211 * Create a new {@code Builder}. 212 * </p> 213 */ 214 public Builder() { 215 } 216 217 /** 218 * <p> 219 * Set the input file configuration. 220 * </p> 221 * @param input the input file configuration. 222 * @return the builder for a fluent interface. 223 */ 224 public Builder setInput(final FileConfig input) { 225 this.input = input; 226 return this; 227 } 228 229 /** 230 * <p> 231 * Set the output file(s) configuration. 232 * </p> 233 * @param output the output file(s) configuration. 234 * @return the builder for a fluent interface. 235 */ 236 public Builder setOutput(final OutputConfig output) { 237 this.output = output; 238 return this; 239 } 240 241 /** 242 * <p> 243 * Set the random generation and presentation configuration. 244 * </p> 245 * @param random the random generation and presentation configuration. 246 * @return the builder for a fluent interface. 247 */ 248 public Builder setRandom(final GenerationConfig random) { 249 this.random = random; 250 return this; 251 } 252 253 /** 254 * <p> 255 * Build a new {@link org.sw4j.tool.barcode.random.config.Config Config} with the parameters set. 256 * </p> 257 * @return the new created {@link org.sw4j.tool.barcode.random.config.Config Config}. 258 * @throws IllegalArgumentException if either {@code input}, {@code output} or either {@code random} or 259 * {@code predefined} is {@code null}. 260 */ 261 public Config build() { 262 return new Config(input, output, random, predefined); 263 } 264 265 } 266 267 }