001: /* Type
002: *
003: * $Id: Type.java 4661 2006-09-25 23:11:16Z paul_jack $
004: *
005: * Created on Jan 8, 2004
006: *
007: * Copyright (C) 2004 Internet Archive.
008: *
009: * This file is part of the Heritrix web crawler (crawler.archive.org).
010: *
011: * Heritrix is free software; you can redistribute it and/or modify
012: * it under the terms of the GNU Lesser Public License as published by
013: * the Free Software Foundation; either version 2.1 of the License, or
014: * any later version.
015: *
016: * Heritrix is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: * GNU Lesser Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser Public License
022: * along with Heritrix; if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: */
025: package org.archive.crawler.settings;
026:
027: import java.util.ArrayList;
028: import java.util.Collections;
029: import java.util.List;
030:
031: import javax.management.Attribute;
032:
033: /**
034: * Interface implemented by all element types.
035: *
036: * @author John Erik Halse
037: */
038: public abstract class Type extends Attribute {
039: /** Should this Type be serialized to persistent storage */
040: private boolean isTransient = false;
041: /** True if this Type can be overridden */
042: private boolean overrideable = true;
043: /** True if this Type should only show up in expert mode in UI */
044: private boolean isExpertSetting = false;
045: /** List of constraint that apply for the values of this type */
046: private List<Constraint> constraints = new ArrayList<Constraint>();
047: /** The class the value of this type must be an instance of (or instance of
048: * a subclass.
049: */
050: private Class legalValueType;
051:
052: /** Creates a new instance of Type.
053: *
054: * @param name
055: * @param value
056: */
057: public Type(String name, Object value) {
058: super (name.intern(), value);
059: legalValueType = value != null ? value.getClass() : this
060: .getClass();
061: constraints.add(new LegalValueTypeConstraint());
062: }
063:
064: /** Get the description of this type
065: *
066: * The description should be suitable for showing in a user interface.
067: *
068: * @return this type's description
069: */
070: abstract String getDescription();
071:
072: /** The default value for this type
073: *
074: * @return this type's default value
075: */
076: abstract Object getDefaultValue();
077:
078: /** Get the legal values for this type.
079: *
080: * @return the legal values for this type or null if there are no
081: * restrictions.
082: */
083: abstract Object[] getLegalValues();
084:
085: /** Is this an 'overrideable' setting. All settings are overrideable by
086: * default.
087: *
088: * @return True if this is an an overrideable setting.
089: */
090: public boolean isOverrideable() {
091: return overrideable;
092: }
093:
094: /** Set if this Type should be overideable.
095: *
096: * @param b true if this Type should be overideable.
097: */
098: public void setOverrideable(boolean b) {
099: overrideable = b;
100: }
101:
102: /** Returns true if this ComplexType should be saved to persistent storage.
103: *
104: * @return true if this ComplexType should be saved to persistent storage.
105: */
106: public boolean isTransient() {
107: return isTransient;
108: }
109:
110: /** Set to false if this attribute should not be serialized to persistent
111: * storage.
112: *
113: * @param b if false this complexType will not be saved to persistent
114: * storage.
115: */
116: public void setTransient(boolean b) {
117: isTransient = b;
118: }
119:
120: /** Returns true if this Type should only show up in expert mode in UI.
121: *
122: * @return true if this Type should only show up in expert mode in UI.
123: */
124: public boolean isExpertSetting() {
125: return isExpertSetting;
126: }
127:
128: /** Set if this Type should only show up in expert mode in UI.
129: *
130: * @param isExpertSetting true if this Type should only show up in
131: * expert mode in UI.
132: */
133: public void setExpertSetting(boolean isExpertSetting) {
134: this .isExpertSetting = isExpertSetting;
135: }
136:
137: /** Returns a list of constraints for the value of this type.
138: *
139: * @return Returns the constraints or null if there aren't any.
140: */
141: public List getConstraints() {
142: return constraints;
143: }
144:
145: /** Add a constraint to this type.
146: *
147: * Every constraint must be fulfilled for a value of this type to be valid.
148: *
149: * @param constraint the constraint to add.
150: */
151: public void addConstraint(Constraint constraint) {
152: constraints.add(constraint);
153: Collections.sort(constraints);
154: }
155:
156: /**
157: * Get the class values of this Type must be an instance of.
158: *
159: * @return Returns the legalValueType.
160: */
161: public Class getLegalValueType() {
162: return legalValueType;
163: }
164:
165: /**
166: * Set the class values of this Type must be an instance of.
167: *
168: * @param legalValueType The legalValueType to set.
169: */
170: public void setLegalValueType(Class legalValueType) {
171: this .legalValueType = legalValueType;
172: }
173:
174: /**
175: * The implementation of equals consider to Types as equal if name and
176: * value are equal. Description is allowed to differ.
177: */
178: public boolean equals(Object o) {
179: return this == o
180: || (o instanceof Type
181: && this .getName().equals(((Type) o).getName()) && this
182: .getValue().equals(((Type) o).getValue()));
183: }
184: }
|