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.sample;
28  
29  import com.rapiddweller.benerator.util.ThreadSafeGenerator;
30  import com.rapiddweller.benerator.wrapper.ProductWrapper;
31  
32  /**
33   * Generator implementation that always returns the same value.<br/>
34   * <br/>
35   * Created: 08.06.2006 20:26:22
36   *
37   * @param <E> the type parameter
38   * @author Volker Bergmann
39   * @since 0.1
40   */
41  public class ConstantGenerator<E> extends ThreadSafeGenerator<E> {
42  
43    /**
44     * The value to return
45     */
46    private E value;
47  
48    private final Class<E> generatedType;
49  
50    // constructors ----------------------------------------------------------------------------------------------------
51  
52    /**
53     * Instantiates a new Constant generator.
54     */
55    public ConstantGenerator() {
56      this(null);
57    }
58  
59    /**
60     * Initializes the generator to generate the specified value
61     *
62     * @param value the value
63     */
64    @SuppressWarnings("unchecked")
65    public ConstantGenerator(E value) {
66      this(value, (Class<E>) (value != null ? value.getClass() : Object.class));
67    }
68  
69    /**
70     * Instantiates a new Constant generator.
71     *
72     * @param value         the value
73     * @param generatedType the generated type
74     */
75    public ConstantGenerator(E value, Class<E> generatedType) {
76      this.value = value;
77      this.generatedType = generatedType;
78    }
79  
80    // config properties -----------------------------------------------------------------------------------------------
81  
82    /**
83     * Returns the property 'value'
84     *
85     * @return the value
86     */
87    public E getValue() {
88      return value;
89    }
90  
91    /**
92     * Sets the property 'value'
93     *
94     * @param value the value
95     */
96    public void setValue(E value) {
97      this.value = value;
98    }
99  
100   // Generator implementation ----------------------------------------------------------------------------------------
101 
102   @Override
103   @SuppressWarnings("unchecked")
104   public Class<E> getGeneratedType() {
105     return (generatedType != null ?
106         generatedType :
107         (value != null ? (Class<E>) value.getClass() : (Class<E>) Object.class)
108     );
109   }
110 
111   /**
112    * Returns the value of property 'value'
113    */
114   @Override
115   public ProductWrapper<E> generate(ProductWrapper<E> wrapper) {
116     return wrapper.wrap(value);
117   }
118 
119   // java.lang.Object overrides --------------------------------------------------------------------------------------
120 
121   @Override
122   public String toString() {
123     return getClass().getSimpleName() + '[' + value + ']';
124   }
125 
126 }