View Javadoc

1   /*
2    * Copyright 2001,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  package org.apache.commons.scaffold.util;
18  
19  
20  import java.util.ArrayList;
21  import java.util.Collection;
22  import java.util.Iterator;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.apache.commons.beanutils.BeanUtils;
27  
28  import org.apache.commons.scaffold.lang.ChainedException;
29  import org.apache.commons.scaffold.lang.Tokens;
30  
31  /**
32   * Concrete implementation of ResultList.
33   * @author Ted Husted
34   * @version $Revision: 155464 $ $Date: 2005-02-26 13:26:54 +0000 (Sat, 26 Feb 2005) $
35   */
36  public class ResultListBase implements ResultList {
37  
38  
39  // ----------------------------------------------------------- Properties
40  
41  
42      /**
43       * The result list [ArrayList].
44       */
45      protected List result = (List) new ArrayList();
46  
47  
48      public List getResult() {
49          return this.result;
50      }
51  
52  
53      public void setResult(List result) {
54          this.result = result;
55      }
56  
57  
58  
59      /**
60       * Our scroller object for paging through lists.
61       */
62      protected Scroller scroller = null;
63  
64  
65      public void setScroller(Scroller scroller){
66          this.scroller = scroller;
67      }
68  
69  
70      public Scroller getScroller() {
71          return this.scroller;
72      }
73  
74  
75      /**
76       * Our intial counter value [0].
77       */
78      private int counter = 0;
79  
80  
81      public int getCounter() {
82          return counter++;
83      }
84  
85  
86      public void setCounter(int counter) {
87             this.counter = counter;
88      }
89  
90  
91      /**
92       * A result code, if returned by the resource layer.
93       */
94      private Integer code = null;
95  
96  
97      public Integer getCode() {
98          return (this.code);
99      }
100 
101 
102     public void setCode(Integer code) {
103         this.code = code;
104     }
105 
106 
107     /**
108      * A phrase describing the result list.
109      * ie: field = value
110      */
111     private String legend = null;
112 
113 
114     public String getLegend() {
115         return (this.legend);
116     }
117 
118 
119     public void setLegend(String legend) {
120         this.legend = legend;
121     }
122 
123 
124     public void setLegend(String name, String value) {
125 
126         if ((null==value) || ("".equals(value))) {
127 
128             setLegend(name + Tokens.PARAM_EQUALS + Tokens.PARAM_ANY);
129 
130         }
131         else {
132 
133             setLegend(name + Tokens.PARAM_EQUALS + value);
134 
135         }
136     }
137 
138 
139     /**
140      * The displayName map, if any.
141      */
142     protected Map displayName = null;
143 
144 
145     public Map getDisplayName() {
146         return this.displayName;
147     }
148 
149 
150     public void setDisplayName(Map displayName) {
151         this.displayName = displayName;
152     }
153 
154 
155 
156 // ------------------------------------------------------------- List Methods
157 
158 
159     // ----- array operations -----
160 
161 
162     public Object[] toArray() {
163         return getResult().toArray();
164     }
165 
166 
167     public Object[] toArray(Object a[]) {
168         return getResult().toArray(a);
169     }
170 
171 
172     // ----- basic operations -----
173 
174 
175     public boolean isEmpty() {
176         return getResult().isEmpty();
177     }
178 
179 
180     public int size() {
181         return getResult().size();
182     }
183 
184 
185     public boolean contains(Object element) {
186         return getResult().contains(element);
187     }
188 
189 
190     public boolean add(Object o) {
191         return getResult().add(o);
192     }
193 
194 
195     public Iterator iterator() {
196         return getResult().iterator();
197     }
198 
199 
200     // ----- list operations -----
201 
202 
203     public Object get(int index) {
204         return getResult().get(index);
205     }
206 
207 
208     // ----- bulk operations ------
209 
210 
211     public boolean addAll(Collection c) {
212         return getResult().addAll(c);
213     }
214 
215 
216     public void clear() {
217         getResult().clear();
218     }
219 
220 
221     public boolean containsAll(Collection c) {
222         return getResult().containsAll(c);
223     }
224 
225 
226     public boolean remove(Object o) {
227         return getResult().remove(o);
228     }
229 
230 
231     public boolean removeAll(Collection c) {
232         return getResult().removeAll(c);
233     }
234 
235 
236     public boolean retainAll(Collection c) {
237         return getResult().retainAll(c);
238     }
239 
240 
241 
242 // ----------------------------------------------------------- Public Methods
243 
244 
245     public Object getElement(int index) {
246         return getResult().get(index);
247     }
248 
249 
250     public Iterator getIterator() {
251         return iterator();
252     }
253 
254 
255     public int getSize() {
256         return size();
257     }
258 
259 
260     public boolean populate(Object o, int index) throws Exception {
261         Object bean = null;
262         if (size()>index)
263             bean = get(index);
264         if (bean==null) return false;
265         try {
266             BeanUtils.copyProperties(o,bean);
267         } catch (Throwable t) {
268             throw new ChainedException(t);
269         }
270         return true;
271     }
272 
273 
274 // ----------------------------------------------------------- Constructors
275 
276 
277     /**
278      * Default constructor.
279      */
280     public ResultListBase() {
281         super();
282     }
283 
284 
285     /**
286      * Convenience constructor to populate result with an element.
287      */
288     public ResultListBase(Object o) {
289         super();
290         add(o);
291     }
292 
293 
294     /**
295      * Convenience constructor to populate result with a Collection.
296      */
297     public ResultListBase(Collection c) {
298         super();
299         addAll(c);
300     }
301 
302 }