001: /*
002: * hgcommons 7
003: * Hammurapi Group Common Library
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.util;
024:
025: import java.util.ArrayList;
026: import java.util.Collection;
027: import java.util.Iterator;
028:
029: import biz.hammurapi.convert.Converter;
030:
031: /**
032: * Collection that contains elements of one type but appears as
033: * containing elements of another type. Conversion is done by a
034: * Converter. Usage - keep collection of 'keys' and retrieve
035: * 'values' from secondary storage on demand.
036: * @author Pavel Vlasov
037: *
038: * @version $Revision: 1.3 $
039: */
040: public class ConvertingCollection implements Collection {
041:
042: private Collection master;
043: private Converter key2ValueConverter;
044: private Converter value2KeyConverter;
045:
046: /**
047: * @param master
048: * @param key2ValueConverter
049: * @param value2KeyConverter
050: */
051: public ConvertingCollection(Collection master,
052: Converter key2ValueConverter, Converter value2KeyConverter) {
053: super ();
054: this .master = master;
055: this .key2ValueConverter = key2ValueConverter;
056: this .value2KeyConverter = value2KeyConverter;
057: }
058:
059: public int size() {
060: return master.size();
061: }
062:
063: public void clear() {
064: master.clear();
065: }
066:
067: public boolean isEmpty() {
068: return master.isEmpty();
069: }
070:
071: public Object[] toArray() {
072: Object[] ret = master.toArray();
073: for (int i = 0; i < ret.length; i++) {
074: ret[i] = key2ValueConverter.convert(ret[i]);
075: }
076: return ret;
077: }
078:
079: public boolean add(Object o) {
080: if (value2KeyConverter == null) {
081: throw new IllegalStateException(
082: "Cannot add - value2KeyConverter is null");
083: }
084:
085: return master.add(value2KeyConverter.convert(o));
086: }
087:
088: public boolean contains(Object o) {
089: if (value2KeyConverter == null) {
090: throw new IllegalStateException(
091: "Cannot execute - value2KeyConverter is null");
092: }
093:
094: return master.contains(value2KeyConverter.convert(o));
095: }
096:
097: public boolean remove(Object o) {
098: if (value2KeyConverter == null) {
099: throw new IllegalStateException(
100: "Cannot remove - value2KeyConverter is null");
101: }
102:
103: return master.remove(value2KeyConverter.convert(o));
104: }
105:
106: public boolean addAll(Collection c) {
107: if (value2KeyConverter == null) {
108: throw new IllegalStateException(
109: "Cannot add - value2KeyConverter is null");
110: }
111:
112: Collection converted = new ArrayList();
113: Iterator it = c.iterator();
114: while (it.hasNext()) {
115: converted.add(value2KeyConverter.convert(it.next()));
116: }
117: return master.addAll(converted);
118: }
119:
120: public boolean containsAll(Collection c) {
121: if (value2KeyConverter == null) {
122: throw new IllegalStateException(
123: "Cannot execute - value2KeyConverter is null");
124: }
125:
126: Collection converted = new ArrayList();
127: Iterator it = c.iterator();
128: while (it.hasNext()) {
129: converted.add(value2KeyConverter.convert(it.next()));
130: }
131: return master.containsAll(converted);
132: }
133:
134: public boolean removeAll(Collection c) {
135: if (value2KeyConverter == null) {
136: throw new IllegalStateException(
137: "Cannot remove - value2KeyConverter is null");
138: }
139:
140: Collection converted = new ArrayList();
141: Iterator it = c.iterator();
142: while (it.hasNext()) {
143: converted.add(value2KeyConverter.convert(it.next()));
144: }
145: return master.removeAll(converted);
146: }
147:
148: public boolean retainAll(Collection c) {
149: if (value2KeyConverter == null) {
150: throw new IllegalStateException(
151: "Cannot retain - value2KeyConverter is null");
152: }
153:
154: Collection converted = new ArrayList();
155: Iterator it = c.iterator();
156: while (it.hasNext()) {
157: converted.add(value2KeyConverter.convert(it.next()));
158: }
159: return master.retainAll(converted);
160: }
161:
162: public Iterator iterator() {
163: final Iterator masterIterator = master.iterator();
164: return new Iterator() {
165:
166: public void remove() {
167: masterIterator.remove();
168: }
169:
170: public boolean hasNext() {
171: return masterIterator.hasNext();
172: }
173:
174: public Object next() {
175: return key2ValueConverter
176: .convert(masterIterator.next());
177: }
178: };
179: }
180:
181: public Object[] toArray(Object[] a) {
182: if (a.length < size()) {
183: a = (Object[]) java.lang.reflect.Array.newInstance(a
184: .getClass().getComponentType(), size());
185: }
186:
187: Iterator it = master.iterator();
188: for (int i = 0; it.hasNext(); i++) {
189: a[i] = key2ValueConverter.convert(it.next());
190: }
191:
192: if (a.length > size()) {
193: a[size()] = null;
194: }
195:
196: return a;
197: }
198:
199: }
|