KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > common > util > PartialCollection


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
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 com.blandware.atleap.common.util;
17
18 import org.apache.commons.lang.builder.ToStringBuilder;
19 import org.apache.commons.lang.builder.ToStringStyle;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  * <p>This collection contains part of some collection of entities and
29  * total number of elements of initial collection
30  * </p>
31  *
32  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
33  * @version $Revision: 1.8 $ $Date: 2005/08/02 14:53:33 $
34  */

35 public class PartialCollection implements Collection JavaDoc, Serializable JavaDoc {
36
37     /**
38      * Collection of entities (part of some another collection)
39      */

40     private Collection JavaDoc collection;
41
42     /**
43      * Total number of elements in collection this collection is part of
44      */

45     private Integer JavaDoc total;
46
47     /**
48      * Creates new instance of PartialCollection with specified collection and total
49      *
50      * @param collection Part of some collection of entities
51      * @param total Total size of collection, which part is contained in this instance
52      */

53     public PartialCollection(Collection JavaDoc collection, int total) {
54         this(collection, new Integer JavaDoc(total));
55     }
56
57     /**
58      * Creates an empty instance of PartialCollection
59      */

60     public PartialCollection() {
61         this(new ArrayList JavaDoc(), 0);
62     }
63
64     /**
65      * Creates new instance of PartialCollection with specified collection and total
66      *
67      * @param collection Part of some collection of entities
68      * @param total Total size of collection, which part is contained in this instance
69      */

70     public PartialCollection(Collection JavaDoc collection, Integer JavaDoc total) {
71         this.collection = collection;
72         this.total = total;
73     }
74
75     /**
76      * Represents this collection as list
77      *
78      * @return list representing this collection
79      */

80     public List JavaDoc asList() {
81         List JavaDoc result = null;
82         if ( collection != null ) {
83             if ( !(collection instanceof List JavaDoc) ) {
84                 result = new ArrayList JavaDoc(collection);
85             } else {
86                 result = (List JavaDoc) collection;
87             }
88         }
89         return result;
90     }
91
92     /**
93      * Gets the size of part of initial collection that is contained here
94      *
95      * @return number of elements in partial collection
96      */

97     public int size() {
98         return collection.size();
99     }
100
101     /**
102      * Clears the partial collection
103      */

104     public void clear() {
105         collection.clear();
106     }
107
108     /**
109      * Figures out is partial collection empty
110      *
111      * @return <code>true</code> if this collection is empty
112      */

113     public boolean isEmpty() {
114         return collection.isEmpty();
115     }
116
117     public Object JavaDoc[] toArray() {
118         return collection.toArray();
119     }
120
121     public boolean add(Object JavaDoc o) {
122         return collection.add(o);
123     }
124
125     public boolean contains(Object JavaDoc o) {
126         return collection.contains(o);
127     }
128
129     public boolean remove(Object JavaDoc o) {
130         return collection.remove(o);
131     }
132
133     public boolean addAll(Collection JavaDoc c) {
134         return collection.addAll(c);
135     }
136
137     public boolean containsAll(Collection JavaDoc c) {
138         return collection.containsAll(c);
139     }
140
141     public boolean removeAll(Collection JavaDoc c) {
142         return collection.removeAll(c);
143     }
144
145     public boolean retainAll(Collection JavaDoc c) {
146         return collection.retainAll(c);
147     }
148
149     public Iterator JavaDoc iterator() {
150         return collection.iterator();
151     }
152
153     public Object JavaDoc[] toArray(Object JavaDoc[] a) {
154         return collection.toArray(a);
155     }
156
157     /**
158      * Gets total number of elements in initial collection
159      *
160      * @return total number of elements
161      */

162     public Integer JavaDoc getTotal() {
163         return total;
164     }
165
166 // public String toString() {
167
// StringBuffer sb = new StringBuffer("[rows=[");
168
// for ( Iterator i = collection.iterator(); i.hasNext(); ) {
169
// sb.append(i.next());
170
// if ( i.hasNext() ) {
171
// sb.append(", ");
172
// }
173
// }
174
// sb.append("];\n");
175
// sb.append("]");
176
// return sb.toString();
177
// }
178

179     public String JavaDoc toString() {
180         return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
181     }
182 }
183
Popular Tags