1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package org.archive.crawler.settings;
26
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
30
31 import javax.management.Attribute;
32
33 /***
34 * Interface implemented by all element types.
35 *
36 * @author John Erik Halse
37 */
38 public abstract class Type extends Attribute {
39 /*** Should this Type be serialized to persistent storage */
40 private boolean isTransient = false;
41 /*** True if this Type can be overridden */
42 private boolean overrideable = true;
43 /*** True if this Type should only show up in expert mode in UI */
44 private boolean isExpertSetting = false;
45 /*** List of constraint that apply for the values of this type */
46 private List<Constraint> constraints = new ArrayList<Constraint>();
47 /*** The class the value of this type must be an instance of (or instance of
48 * a subclass.
49 */
50 private Class legalValueType;
51
52 /*** Creates a new instance of Type.
53 *
54 * @param name
55 * @param value
56 */
57 public Type(String name, Object value) {
58 super(name.intern(), value);
59 legalValueType = value != null ? value.getClass() : this.getClass();
60 constraints.add(new LegalValueTypeConstraint());
61 }
62
63 /*** Get the description of this type
64 *
65 * The description should be suitable for showing in a user interface.
66 *
67 * @return this type's description
68 */
69 abstract String getDescription();
70
71 /*** The default value for this type
72 *
73 * @return this type's default value
74 */
75 abstract Object getDefaultValue();
76
77 /*** Get the legal values for this type.
78 *
79 * @return the legal values for this type or null if there are no
80 * restrictions.
81 */
82 abstract Object[] getLegalValues();
83
84 /*** Is this an 'overrideable' setting. All settings are overrideable by
85 * default.
86 *
87 * @return True if this is an an overrideable setting.
88 */
89 public boolean isOverrideable() {
90 return overrideable;
91 }
92
93 /*** Set if this Type should be overideable.
94 *
95 * @param b true if this Type should be overideable.
96 */
97 public void setOverrideable(boolean b) {
98 overrideable = b;
99 }
100
101 /*** Returns true if this ComplexType should be saved to persistent storage.
102 *
103 * @return true if this ComplexType should be saved to persistent storage.
104 */
105 public boolean isTransient() {
106 return isTransient;
107 }
108
109 /*** Set to false if this attribute should not be serialized to persistent
110 * storage.
111 *
112 * @param b if false this complexType will not be saved to persistent
113 * storage.
114 */
115 public void setTransient(boolean b) {
116 isTransient = b;
117 }
118
119 /*** Returns true if this Type should only show up in expert mode in UI.
120 *
121 * @return true if this Type should only show up in expert mode in UI.
122 */
123 public boolean isExpertSetting() {
124 return isExpertSetting;
125 }
126
127 /*** Set if this Type should only show up in expert mode in UI.
128 *
129 * @param isExpertSetting true if this Type should only show up in
130 * expert mode in UI.
131 */
132 public void setExpertSetting(boolean isExpertSetting) {
133 this.isExpertSetting = isExpertSetting;
134 }
135
136 /*** Returns a list of constraints for the value of this type.
137 *
138 * @return Returns the constraints or null if there aren't any.
139 */
140 public List getConstraints() {
141 return constraints;
142 }
143
144 /*** Add a constraint to this type.
145 *
146 * Every constraint must be fulfilled for a value of this type to be valid.
147 *
148 * @param constraint the constraint to add.
149 */
150 public void addConstraint(Constraint constraint) {
151 constraints.add(constraint);
152 Collections.sort(constraints);
153 }
154
155 /***
156 * Get the class values of this Type must be an instance of.
157 *
158 * @return Returns the legalValueType.
159 */
160 public Class getLegalValueType() {
161 return legalValueType;
162 }
163
164 /***
165 * Set the class values of this Type must be an instance of.
166 *
167 * @param legalValueType The legalValueType to set.
168 */
169 public void setLegalValueType(Class legalValueType) {
170 this.legalValueType = legalValueType;
171 }
172
173 /***
174 * The implementation of equals consider to Types as equal if name and
175 * value are equal. Description is allowed to differ.
176 */
177 public boolean equals(Object o) {
178 return this == o
179 || (o instanceof Type
180 && this.getName().equals(((Type) o).getName()) && this
181 .getValue().equals(((Type) o).getValue()));
182 }
183 }