View Javadoc

1   /*
2    * Copyright 2003,2004 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  /* 
17  
18   */
19  
20  package org.apache.pluto.portalImpl.om.common.impl;
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Iterator;
25  import java.util.List;
26  
27  import org.apache.pluto.om.common.Preference;
28  import org.apache.pluto.om.common.PreferenceCtrl;
29  import org.apache.pluto.util.StringUtils;
30  
31  public class PreferenceImpl implements Preference, PreferenceCtrl, java.io.Serializable {
32      private final static String NULL_VALUE = "#*!0_NULL_0!*#";
33      private final static String NULL_ARRAYENTRY = "#*!1_NULL_1!*#";
34  
35      private String name;
36      private ArrayList value;
37      private Boolean readOnly;
38  
39      public PreferenceImpl()
40      {
41      }
42  
43      // Preference implementation.
44  
45      public String getName()
46      {
47          return name;
48      }
49  
50      public Iterator getValues()
51      {
52          // replace the NULL_VALUE String by NULL
53          if (value.contains(NULL_VALUE)) {
54              return null;
55          }
56  
57          ArrayList returnValue = new ArrayList(value.size());
58          returnValue.addAll(value);
59  
60          // replace all NULL_ARRAYENTRY Strings by NULL
61          for (int i = 0; i < returnValue.size(); i++) {
62              if (NULL_ARRAYENTRY.equals(returnValue.get(i))) {
63                  returnValue.set(i, null);
64              }
65          }
66  
67          return returnValue.iterator();
68      }
69  
70      public boolean isReadOnly()
71      {
72          if (readOnly == null) {
73              return false;
74          }
75          return readOnly.booleanValue();
76      }
77  
78      public boolean isValueSet() {
79          return value != null;
80      }
81  
82      // PreferenceCtrl implementation.
83      
84      public void setName(String name)
85      {
86          this.name = name;
87      }
88  
89      public void setValues(java.util.List _value)
90      {
91          if (this.value == null) {
92              this.value = new ArrayList();
93          } else {
94              this.value.clear();
95          }
96  
97          List addValue = null;
98  
99          // replace NULL by the NULL_VALUE String
100         if (_value == null) {
101             addValue = new ArrayList(1);
102             addValue.add(NULL_VALUE); 
103         } else {
104             // replace all NULL by the NULL_ARRAYENTRY String
105             addValue = new ArrayList(_value.size());
106             addValue.addAll(_value);
107             for (int i=0;i<addValue.size();i++) {
108                 if (addValue.get(i) == null) {
109                     addValue.set(i, NULL_ARRAYENTRY);
110                 }
111             }
112         }
113 
114         this.value.addAll(addValue);
115     }
116 
117     public void setReadOnly(String readOnly)
118     {
119         this.readOnly = new Boolean(readOnly);
120     }
121 
122     // additional methods.
123     
124     // internal methods only used by castor
125 
126     public String getReadOnly()
127     {
128         if (readOnly == null) {
129             return Boolean.FALSE.toString();
130         }
131         return readOnly.toString();
132     }
133 
134     public Collection getCastorValues() 
135     {
136         return value;
137     }
138 
139     public void setCastorValues(Collection _value) 
140     {
141         if (value == null) {
142             value = new ArrayList();
143         } else {
144             value.clear();
145         }
146         value.addAll(_value);
147     }
148 
149     protected List getClonedCastorValuesAsList()
150     {
151         List returnValue = new ArrayList(value.size());
152 
153         Iterator iter = value.iterator();
154         while (iter.hasNext()) {
155             String value = (String) iter.next();
156             returnValue.add(value);
157         }
158         return returnValue;
159     }
160 
161     public String toString()
162     {
163         return toString(0);
164     }
165 
166     public String toString(int indent)
167     {
168         StringBuffer buffer = new StringBuffer(50);
169         StringUtils.newLine(buffer,indent);
170         buffer.append(getClass().toString());
171         buffer.append(": name='");
172         buffer.append(name);
173         buffer.append("', value='");
174 
175         if (value == null) {
176             buffer.append("null");
177         } else {
178             StringUtils.newLine(buffer,indent);
179             buffer.append("{");
180             Iterator iterator = value.iterator();
181             if (iterator.hasNext()) {
182                 StringUtils.newLine(buffer,indent);
183                 buffer.append((String)iterator.next());
184             }
185             while (iterator.hasNext()) {
186                 StringUtils.indent(buffer,indent+2);
187                 buffer.append((String)iterator.next());
188             }
189             StringUtils.newLine(buffer,indent);
190             buffer.append("}");
191         }
192 
193         buffer.append("', isReadOnly='");
194         buffer.append(isReadOnly());
195         buffer.append("'");
196         return buffer.toString();
197     }
198 
199 }