View Javadoc

1   /* Type
2    *
3    * $Id: Type.java 4661 2006-09-25 23:11:16Z paul_jack $
4    *
5    * Created on Jan 8, 2004
6    *
7    * Copyright (C) 2004 Internet Archive.
8    *
9    * This file is part of the Heritrix web crawler (crawler.archive.org).
10   *
11   * Heritrix is free software; you can redistribute it and/or modify
12   * it under the terms of the GNU Lesser Public License as published by
13   * the Free Software Foundation; either version 2.1 of the License, or
14   * any later version.
15   *
16   * Heritrix is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU Lesser Public License for more details.
20   *
21   * You should have received a copy of the GNU Lesser Public License
22   * along with Heritrix; if not, write to the Free Software
23   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 }