KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > datatypes > AbstractLengthDataType


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.datatypes;
11
12 import java.util.*;
13
14 import org.mmbase.bridge.*;
15 import org.mmbase.util.Casting;
16 import org.mmbase.util.logging.*;
17 import org.w3c.dom.Element JavaDoc;
18
19 /**
20  * A LengthDataType is a datatype that defines a length for its values ({@link #getLength(Object)}) ,
21  * and restrictions on that (minimal an maximal length).
22  *
23  * @author Pierre van Rooden
24  * @author Michiel Meeuwissen
25  * @version $Id: AbstractLengthDataType.java,v 1.16 2006/07/18 12:58:40 michiel Exp $
26  * @since MMBase-1.8
27  */

28 abstract public class AbstractLengthDataType extends BasicDataType implements LengthDataType {
29     private static final Logger log = Logging.getLoggerInstance(LengthDataType.class);
30
31     protected MinRestriction minLengthRestriction = new MinRestriction(this, 0);
32     protected MaxRestriction maxLengthRestriction = new MaxRestriction(this, Long.MAX_VALUE);
33
34     /**
35      * Constructor for big data field.
36      * @param name the name of the data type
37      * @param classType the class of the data type's possible value
38      */

39     public AbstractLengthDataType(String JavaDoc name, Class JavaDoc classType) {
40         super(name, classType);
41         minLengthRestriction.setEnforceStrength(ENFORCE_ABSOLUTE);
42     }
43
44
45     protected void cloneRestrictions(BasicDataType origin) {
46         super.cloneRestrictions(origin);
47         if (origin instanceof AbstractLengthDataType) {
48             AbstractLengthDataType dataType = (AbstractLengthDataType)origin;
49             // make new instances because of this can be called from a clone .. We hate java.
50
minLengthRestriction = new MinRestriction(this, dataType.minLengthRestriction);
51             maxLengthRestriction = new MaxRestriction(this, dataType.maxLengthRestriction);
52         }
53     }
54
55     protected void inheritRestrictions(BasicDataType origin) {
56         super.inheritRestrictions(origin);
57         if (origin instanceof AbstractLengthDataType) {
58             AbstractLengthDataType dataType = (AbstractLengthDataType)origin;
59             // make new instances because of this can be called from a clone .. We hate java.
60
minLengthRestriction.inherit(dataType.minLengthRestriction);
61             maxLengthRestriction.inherit(dataType.maxLengthRestriction);
62         }
63     }
64
65     /**
66      * {@inheritDoc}
67      */

68     public abstract long getLength(Object JavaDoc value);
69
70     /**
71      * {@inheritDoc}
72      */

73     public long getMinLength() {
74         return Casting.toLong(minLengthRestriction.getValue());
75     }
76
77     /**
78      * {@inheritDoc}
79      */

80     public DataType.Restriction getMinLengthRestriction() {
81         return minLengthRestriction;
82     }
83
84     /**
85      * {@inheritDoc}
86      */

87     public void setMinLength(long value) {
88         getMinLengthRestriction().setValue(new Long JavaDoc(value));
89     }
90
91     /**
92      * {@inheritDoc}
93      */

94     public long getMaxLength() {
95         return Casting.toLong(getMaxLengthRestriction().getValue());
96     }
97
98     /**
99      * {@inheritDoc}
100      */

101     public DataType.Restriction getMaxLengthRestriction() {
102         return maxLengthRestriction;
103     }
104
105     /**
106      * Sets the maximum length of binary values for this datatype.
107      * @param value the maximum length as an <code>int</code>, or -1 if there is no maximum length.
108      * @throws Class Identifier: java.lang.UnsupportedOperationException if this datatype is finished
109      */

110     public void setMaxLength(long value) {
111         getMaxLengthRestriction().setValue(new Long JavaDoc(value));
112     }
113
114     protected Collection validateCastValue(Collection errors, Object JavaDoc castValue, Object JavaDoc value, Node node, Field field) {
115         errors = super.validateCastValue(errors, castValue, value, node, field);
116         errors = minLengthRestriction.validate(errors, castValue, node, field);
117         errors = maxLengthRestriction.validate(errors, castValue, node, field);
118         return errors;
119     }
120
121     public void toXml(Element JavaDoc parent) {
122         super.toXml(parent);
123         getElement(parent, "minLength", "description,class,property,default,unique,required,(minInclusive|minExclusive),(maxIncluse|maxExclusive),minLength").setAttribute("value", Casting.toString(minLengthRestriction.getValue()));
124         getElement(parent, "maxLength", "description,class,property,default,unique,required,(minInclusive|minExclusive),(maxIncluse|maxExclusive),minLength,maxLength").setAttribute("value", Casting.toString(maxLengthRestriction.getValue()));
125
126     }
127
128     protected StringBuffer JavaDoc toStringBuffer() {
129         StringBuffer JavaDoc buf = super.toStringBuffer();
130         if (getMinLength() > 0) {
131             buf.append("minLength:" + getMinLength() + " ");
132         }
133         if (getMaxLength() < Long.MAX_VALUE) {
134             buf.append("maxLength:" + getMaxLength() + " ");
135         }
136         return buf;
137     }
138
139     static class MinRestriction extends StaticAbstractRestriction {
140         MinRestriction(BasicDataType dt, MinRestriction source) {
141             super(dt, source);
142         }
143
144         MinRestriction(BasicDataType dt, long min) {
145             super(dt, "minLength", new Long JavaDoc(min));
146         }
147
148         protected boolean simpleValid(Object JavaDoc v, Node node, Field field) {
149             if (v == null) return true; // depends on 'required'
150
long min = Casting.toLong(getValue());
151             return ((LengthDataType) parent).getLength(v) >= min;
152         }
153     }
154
155     static class MaxRestriction extends StaticAbstractRestriction {
156         MaxRestriction(BasicDataType dt, MaxRestriction source) {
157             super(dt, source);
158         }
159
160         MaxRestriction(BasicDataType dt, long max) {
161             super(dt, "maxLength", new Long JavaDoc(max));
162         }
163
164         protected boolean simpleValid(Object JavaDoc v, Node node, Field field) {
165             if (v == null) return true; // depends on 'required'
166
long max = Casting.toLong(getValue());
167             long length = ((LengthDataType) parent).getLength(v);
168             return length <= max;
169         }
170     }
171
172 }
173
Popular Tags