View Javadoc
1   /*
2    * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, is permitted under the terms of the
6    * GNU General Public License.
7    *
8    * For redistributing this software or a derivative work under a license other
9    * than the GPL-compatible Free Software License as defined by the Free
10   * Software Foundation or approved by OSI, you must first obtain a commercial
11   * license to this software product from rapiddweller GmbH & Volker Bergmann.
12   *
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14   * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
15   * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
17   * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
25   */
26  
27  package com.rapiddweller.benerator.primitive;
28  
29  import com.rapiddweller.benerator.Generator;
30  import com.rapiddweller.benerator.GeneratorContext;
31  import com.rapiddweller.benerator.wrapper.MultiGeneratorWrapper;
32  import com.rapiddweller.benerator.wrapper.ProductWrapper;
33  import com.rapiddweller.common.CharSet;
34  
35  import java.util.ArrayList;
36  import java.util.List;
37  import java.util.Set;
38  
39  /**
40   * Generates unique strings of variable length.<br/>
41   * <br/>
42   * Created: 16.11.2007 11:56:15
43   *
44   * @author Volker Bergmann
45   */
46  public class UniqueScrambledStringGenerator extends MultiGeneratorWrapper<String, String> implements VarLengthStringGenerator {
47  
48    private int minLength;
49    private int maxLength;
50    private final Set<Character> chars;
51  
52    // constructors ----------------------------------------------------------------------------------------------------
53  
54    /**
55     * Instantiates a new Unique scrambled string generator.
56     */
57    public UniqueScrambledStringGenerator() {
58      this(new CharSet('A', 'Z').getSet(), 4, 8);
59    }
60  
61    /**
62     * Instantiates a new Unique scrambled string generator.
63     *
64     * @param chars     the chars
65     * @param minLength the min length
66     * @param maxLength the max length
67     */
68    public UniqueScrambledStringGenerator(Set<Character> chars, int minLength, int maxLength) {
69      super(String.class);
70      this.minLength = minLength;
71      this.maxLength = maxLength;
72      this.chars = chars;
73    }
74  
75    // properties ------------------------------------------------------------------------------------------------------
76  
77    /**
78     * Gets min length.
79     *
80     * @return the min length
81     */
82    public int getMinLength() {
83      return minLength;
84    }
85  
86    /**
87     * Sets min length.
88     *
89     * @param minLength the min length
90     */
91    public void setMinLength(int minLength) {
92      this.minLength = minLength;
93    }
94  
95    /**
96     * Gets max length.
97     *
98     * @return the max length
99     */
100   public int getMaxLength() {
101     return maxLength;
102   }
103 
104   /**
105    * Sets max length.
106    *
107    * @param maxLength the max length
108    */
109   public void setMaxLength(int maxLength) {
110     this.maxLength = maxLength;
111   }
112 
113   // Generator interface ---------------------------------------------------------------------------------------------
114 
115   @Override
116   public void init(GeneratorContext context) {
117     assertNotInitialized();
118     // create sub generators
119     List<Generator<? extends String>> subGens = new ArrayList<>(maxLength - minLength + 1);
120     for (int i = minLength; i <= maxLength; i++) {
121       subGens.add(new UniqueFixedLengthStringGenerator(chars, i, false));
122     }
123     setSources(subGens);
124     super.init(context);
125   }
126 
127   @Override
128   public String generateWithLength(int length) {
129     ProductWrapper<String> wrapper = generateFromSource(length - minLength, getSourceWrapper());
130     return (wrapper != null ? wrapper.unwrap() : null);
131   }
132 
133   @Override
134   public ProductWrapper<String> generate(ProductWrapper<String> wrapper) {
135     assertInitialized();
136     return generateFromRandomSource(wrapper);
137   }
138 
139   @Override
140   public String generate() {
141     ProductWrapper<String> wrapper = generate(getResultWrapper());
142     return (wrapper != null ? wrapper.unwrap() : null);
143   }
144 
145   // java.lang.Object overrides --------------------------------------------------------------------------------------
146 
147   @Override
148   public String toString() {
149     return getClass().getSimpleName() + '[' + minLength + "<=length<=" + maxLength + ", " +
150         "charSet=" + chars + "]";
151   }
152 
153 }