KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.mmbase.bridge.*;
14 import org.mmbase.util.Casting;
15
16 /**
17  * @javadoc
18  *
19  * @author Pierre van Rooden
20  * @author Michiel Meeuwissen
21  * @version $Id: ListDataType.java,v 1.20 2006/07/17 07:32:29 pierre Exp $
22  * @since MMBase-1.8
23  */

24 public class ListDataType extends AbstractLengthDataType {
25     private static final long serialVersionUID = 1L; // increase this if object serialization changes (which we shouldn't do!)
26

27     protected ItemRestriction itemRestriction = new ItemRestriction(Constants.DATATYPE_UNKNOWN);
28
29     /**
30      * Constructor for List field.
31      */

32     public ListDataType(String JavaDoc name) {
33         super(name, List.class);
34     }
35
36
37     protected void inheritRestrictions(BasicDataType origin) {
38         super.inheritRestrictions(origin);
39         if (origin instanceof ListDataType) {
40             ListDataType dataType = (ListDataType)origin;
41             itemRestriction.inherit(dataType.itemRestriction);
42          }
43     }
44     protected void cloneRestrictions(BasicDataType origin) {
45         super.cloneRestrictions(origin);
46         if (origin instanceof ListDataType) {
47             ListDataType dataType = (ListDataType)origin;
48             itemRestriction = new ItemRestriction(dataType.itemRestriction);
49          }
50     }
51
52     public long getLength(Object JavaDoc value) {
53         return ((Collection) value).size();
54     }
55
56     /**
57      * Returns the datatype of items in this list.
58      * @return the datatype as a DataType object, <code>null</code> if there are no restrictions
59      */

60     public DataType getItemDataType() {
61         return itemRestriction.getItemDataType();
62     }
63
64     /**
65      * Returns the 'itemDataType' restriction, containing the value, errormessages, and fixed status of this attribute.
66      * @return the restriction as a {@link DataType.Restriction}
67      */

68     public DataType.Restriction getItemDataTypeRestriction() {
69         return itemRestriction;
70     }
71
72     /**
73      * Sets the datatype of items in this list.
74      * @param value the datatype as a DataType object, <code>null</code> if there are no restrictions
75      */

76     public void setItemDataType(DataType value) {
77         itemRestriction.setValue(value);
78     }
79
80     protected Collection validateCastValue(Collection errors, Object JavaDoc castValue, Object JavaDoc value, Node node, Field field) {
81         errors = super.validateCastValue(errors, castValue, value, node, field);
82         errors = itemRestriction.validate(errors, castValue, node, field);
83         return errors;
84     }
85
86     protected StringBuffer JavaDoc toStringBuffer() {
87         StringBuffer JavaDoc buf = super.toStringBuffer();
88         buf.append("items: " + getItemDataType());
89         return buf;
90     }
91
92     protected class ItemRestriction extends AbstractRestriction {
93         ItemRestriction(ItemRestriction me) {
94             super(me);
95         }
96         ItemRestriction(DataType v) {
97             super("itemDataType", v);
98         }
99         DataType getItemDataType() {
100             return (DataType) value;
101         }
102
103         protected boolean simpleValid(Object JavaDoc v, Node node, Field field) {
104             DataType itemDataType = getItemDataType();
105             if (itemDataType == Constants.DATATYPE_UNKNOWN) return true;
106             List listValue = Casting.toList(v);
107             for (Iterator i = listValue.iterator(); i.hasNext(); ) {
108                 try {
109                     Collection col = itemDataType.validate(i.next());
110                     if (col != VALID) return false;
111                 } catch (ClassCastException JavaDoc cce) {
112                     return false;
113                 }
114             }
115             return true;
116         }
117
118         protected Collection validate(Collection errors, Object JavaDoc v, Node node, Field field) {
119             if (! enforce(node, field)) {
120                 return errors;
121             }
122             DataType itemDataType = getItemDataType();
123             if (itemDataType == Constants.DATATYPE_UNKNOWN) return errors;
124             List listValue = Casting.toList(v);
125             for (Iterator i = listValue.iterator(); i.hasNext(); ) {
126                 Collection col = itemDataType.validate(i.next());
127                 try {
128                     if (col != VALID) {
129                         if (errors == VALID) errors = new ArrayList();
130                         errors.addAll(col);
131                     }
132                 } catch (ClassCastException JavaDoc cce) {
133                     errors = addError(errors, v, node, field);
134                 }
135             }
136             return errors;
137         }
138
139     }
140
141 }
142
Popular Tags