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.model.data;
28  
29  import com.rapiddweller.common.Escalator;
30  import com.rapiddweller.common.LoggerEscalator;
31  import com.rapiddweller.common.NullSafeComparator;
32  import com.rapiddweller.common.Operation;
33  import com.rapiddweller.common.operation.FirstArgSelector;
34  
35  /**
36   * A FeatureDescriptor is composed og FeatureDetails, which have name, value and type.<br/>
37   * <br/>
38   * Created: 03.08.2007 06:57:42
39   *
40   * @param <E> the type parameter
41   * @author Volker Bergmann
42   */
43  public class FeatureDetail<E> {
44  
45    private static final Escalator escalator = new LoggerEscalator();
46  
47    // properties ------------------------------------------------------------------------------------------------------
48  
49    private final String name;
50    private final Class<E> type;
51    private final Operation<E, E> combinator;
52    private final boolean constraint;
53    private final boolean deprecated;
54    private E value;
55  
56    // constructors ----------------------------------------------------------------------------------------------------
57  
58    /**
59     * Instantiates a new Feature detail.
60     *
61     * @param name       the name
62     * @param type       the type
63     * @param constraint the constraint
64     */
65    public FeatureDetail(String name, Class<E> type, boolean constraint) {
66      this(name, type, constraint, new FirstArgSelector<>());
67    }
68  
69    /**
70     * Instantiates a new Feature detail.
71     *
72     * @param name       the name
73     * @param type       the type
74     * @param constraint the constraint
75     * @param combinator the combinator
76     */
77    public FeatureDetail(String name, Class<E> type, boolean constraint,
78                         Operation<E, E> combinator) {
79      this(name, type, constraint, combinator, false);
80    }
81  
82    /**
83     * Instantiates a new Feature detail.
84     *
85     * @param name       the name
86     * @param type       the type
87     * @param constraint the constraint
88     * @param combinator the combinator
89     * @param deprecated the deprecated
90     */
91    public FeatureDetail(String name, Class<E> type, boolean constraint,
92                         Operation<E, E> combinator, boolean deprecated) {
93      this.name = name;
94      this.type = type;
95      this.value = null;
96      this.constraint = constraint;
97      this.combinator = combinator;
98      this.deprecated = deprecated;
99    }
100 
101   // interface -------------------------------------------------------------------------------------------------------
102 
103   /**
104    * Gets name.
105    *
106    * @return the name
107    */
108   public String getName() {
109     return name;
110   }
111 
112   /**
113    * Gets type.
114    *
115    * @return the type
116    */
117   public Class<E> getType() {
118     return type;
119   }
120 
121   /**
122    * Gets value.
123    *
124    * @return the value
125    */
126   public E getValue() {
127     return value;
128   }
129 
130   /**
131    * Sets value.
132    *
133    * @param value the value
134    */
135   public void setValue(E value) {
136     if (deprecated && value != null) {
137       escalator.escalate("Feature '" + name + "' is deprecated",
138           getClass(), value);
139     }
140     if (value != null && !(type.isAssignableFrom(value.getClass()))) {
141       throw new IllegalArgumentException(
142           "Tried to assign a value of type '" +
143               value.getClass().getName()
144               + "'to detail '" + name + "' of type '" + type +
145               "'");
146     }
147     this.value = value;
148   }
149 
150   /**
151    * Combine with e.
152    *
153    * @param otherValue the other value
154    * @return the e
155    */
156   @SuppressWarnings("unchecked")
157   public E combineWith(E otherValue) {
158     return combinator.perform(this.value, otherValue);
159   }
160 
161   /**
162    * Is constraint boolean.
163    *
164    * @return the boolean
165    */
166   public boolean isConstraint() {
167     return constraint;
168   }
169 
170   /**
171    * Gets description.
172    *
173    * @return the description
174    */
175   public String getDescription() {
176     return name + '=' + value + " (" + type + ')';
177   }
178 
179 
180   // java.lang.Object overrides --------------------------------------------------------------------------------------
181 
182   /**
183    * Is deprecated boolean.
184    *
185    * @return the boolean
186    */
187   public boolean isDeprecated() {
188     return deprecated;
189   }
190 
191   @Override
192   public String toString() {
193     return name + '=' + value;
194   }
195 
196   @SuppressWarnings("unchecked")
197   @Override
198   public boolean equals(Object o) {
199     if (this == o) {
200       return true;
201     }
202     if (o == null || getClass() != o.getClass()) {
203       return false;
204     }
205     final FeatureDetail<E> that = (FeatureDetail<E>) o;
206     return (name.equals(that.name)
207         && NullSafeComparator.equals(this.value, that.value));
208   }
209 
210   @Override
211   public int hashCode() {
212     int result;
213     result = name.hashCode();
214     result = 29 * result + (value != null ? value.hashCode() : 0);
215     return result;
216   }
217 
218 }