KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > pluto > om > common > PreferenceImpl


1 /*
2  * Copyright 2004-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.pluto.om.common;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22
23 import org.apache.commons.lang.BooleanUtils;
24 import org.apache.pluto.om.common.Preference;
25 import org.apache.pluto.om.common.PreferenceCtrl;
26 import org.apache.pluto.util.StringUtils;
27
28 /**
29  *
30  *
31  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
32  *
33  * @version CVS $Id: PreferenceImpl.java 280338 2005-09-12 13:11:25Z cziegeler $
34  */

35 public class PreferenceImpl implements Preference, PreferenceCtrl, java.io.Serializable JavaDoc {
36     
37     private final static String JavaDoc NULL_VALUE = "#*!0_NULL_0!*#";
38     private final static String JavaDoc NULL_ARRAYENTRY = "#*!1_NULL_1!*#";
39
40     private String JavaDoc name;
41     private ArrayList JavaDoc value;
42     private Boolean JavaDoc readOnly;
43
44     public PreferenceImpl() {
45         // nothing to do
46
}
47
48     /**
49      * @see org.apache.pluto.om.common.Preference#getName()
50      */

51     public String JavaDoc getName() {
52         return name;
53     }
54
55     /**
56      * @see org.apache.pluto.om.common.Preference#getValues()
57      */

58     public Iterator JavaDoc getValues() {
59         // replace the NULL_VALUE String by NULL
60
if (value.contains(NULL_VALUE)) {
61             return null;
62         }
63
64         ArrayList JavaDoc returnValue = new ArrayList JavaDoc(value.size());
65         returnValue.addAll(value);
66
67         // replace all NULL_ARRAYENTRY Strings by NULL
68
for (int i = 0; i < returnValue.size(); i++) {
69             if (NULL_ARRAYENTRY.equals(returnValue.get(i))) {
70                 returnValue.set(i, null);
71             }
72         }
73
74         return returnValue.iterator();
75     }
76
77     /**
78      * @see org.apache.pluto.om.common.Preference#isReadOnly()
79      */

80     public boolean isReadOnly() {
81         if (readOnly == null) {
82             return false;
83         }
84         return readOnly.booleanValue();
85     }
86
87     /**
88      * @see org.apache.pluto.om.common.Preference#isValueSet()
89      */

90     public boolean isValueSet() {
91         return value != null;
92     }
93
94     // PreferenceCtrl implementation.
95

96     /**
97      * @see org.apache.pluto.om.common.PreferenceCtrl#setName(java.lang.String)
98      */

99     public void setName(String JavaDoc name) {
100         this.name = name;
101     }
102
103     /**
104      * @see org.apache.pluto.om.common.PreferenceCtrl#setValues(java.util.List)
105      */

106     public void setValues(java.util.List JavaDoc _value) {
107         if (this.value == null) {
108             this.value = new ArrayList JavaDoc();
109         } else {
110             this.value.clear();
111         }
112
113         List JavaDoc addValue = null;
114
115         // replace NULL by the NULL_VALUE String
116
if (_value == null) {
117             addValue = new ArrayList JavaDoc(1);
118             addValue.add(NULL_VALUE);
119         } else {
120             // replace all NULL by the NULL_ARRAYENTRY String
121
addValue = new ArrayList JavaDoc(_value.size());
122             addValue.addAll(_value);
123             for (int i=0;i<addValue.size();i++) {
124                 if (addValue.get(i) == null) {
125                     addValue.set(i, NULL_ARRAYENTRY);
126                 }
127             }
128         }
129
130         this.value.addAll(addValue);
131     }
132
133     /**
134      * @see org.apache.pluto.om.common.PreferenceCtrl#setReadOnly(java.lang.String)
135      */

136     public void setReadOnly(String JavaDoc readOnly) {
137         this.readOnly = BooleanUtils.toBooleanObject(readOnly);
138     }
139
140     // additional methods.
141

142     // internal methods only used by castor
143

144     public String JavaDoc getReadOnly() {
145         return BooleanUtils.toStringTrueFalse(BooleanUtils.toBoolean(readOnly));
146     }
147
148     public Collection JavaDoc getCastorValues() {
149         return value;
150     }
151
152     public void setCastorValues(Collection JavaDoc _value) {
153         if (value == null) {
154             value = new ArrayList JavaDoc();
155         } else {
156             value.clear();
157         }
158         value.addAll(_value);
159     }
160
161     protected List JavaDoc getClonedCastorValuesAsList() {
162         List JavaDoc returnValue = new ArrayList JavaDoc(value.size());
163
164         Iterator JavaDoc iter = value.iterator();
165         while (iter.hasNext()) {
166             String JavaDoc value = (String JavaDoc) iter.next();
167             returnValue.add(value);
168         }
169         return returnValue;
170     }
171
172     public String JavaDoc toString() {
173         return toString(0);
174     }
175
176     public String JavaDoc toString(int indent) {
177         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(50);
178         StringUtils.newLine(buffer,indent);
179         buffer.append(getClass().toString());
180         buffer.append(": name='");
181         buffer.append(name);
182         buffer.append("', value='");
183
184         if (value == null) {
185             buffer.append("null");
186         } else {
187             StringUtils.newLine(buffer,indent);
188             buffer.append("{");
189             Iterator JavaDoc iterator = value.iterator();
190             if (iterator.hasNext()) {
191                 StringUtils.newLine(buffer,indent);
192                 buffer.append((String JavaDoc)iterator.next());
193             }
194             while (iterator.hasNext()) {
195                 StringUtils.indent(buffer,indent+2);
196                 buffer.append((String JavaDoc)iterator.next());
197             }
198             StringUtils.newLine(buffer,indent);
199             buffer.append("}");
200         }
201
202         buffer.append("', isReadOnly='");
203         buffer.append(isReadOnly());
204         buffer.append("'");
205         return buffer.toString();
206     }
207
208 }
209
Popular Tags