001 /*
002 @license.text@
003 */
004 package biz.hammurapi.util;
005
006 import java.util.ArrayList;
007 import java.util.Collection;
008 import java.util.Iterator;
009
010 import biz.hammurapi.convert.Converter;
011
012 /**
013 * Collection that contains elements of one type but appears as
014 * containing elements of another type. Conversion is done by a
015 * Converter. Usage - keep collection of 'keys' and retrieve
016 * 'values' from secondary storage on demand.
017 * @author Pavel Vlasov
018 *
019 * @version $Revision: 1.3 $
020 */
021 public class ConvertingCollection implements Collection {
022
023 private Collection master;
024 private Converter key2ValueConverter;
025 private Converter value2KeyConverter;
026
027 /**
028 * @param master
029 * @param key2ValueConverter
030 * @param value2KeyConverter
031 */
032 public ConvertingCollection(Collection master, Converter key2ValueConverter, Converter value2KeyConverter) {
033 super();
034 this.master = master;
035 this.key2ValueConverter = key2ValueConverter;
036 this.value2KeyConverter = value2KeyConverter;
037 }
038
039 public int size() {
040 return master.size();
041 }
042
043 public void clear() {
044 master.clear();
045 }
046
047 public boolean isEmpty() {
048 return master.isEmpty();
049 }
050
051 public Object[] toArray() {
052 Object[] ret = master.toArray();
053 for (int i=0; i<ret.length; i++) {
054 ret[i]=key2ValueConverter.convert(ret[i]);
055 }
056 return ret;
057 }
058
059 public boolean add(Object o) {
060 if (value2KeyConverter==null) {
061 throw new IllegalStateException("Cannot add - value2KeyConverter is null");
062 }
063
064 return master.add(value2KeyConverter.convert(o));
065 }
066
067 public boolean contains(Object o) {
068 if (value2KeyConverter==null) {
069 throw new IllegalStateException("Cannot execute - value2KeyConverter is null");
070 }
071
072 return master.contains(value2KeyConverter.convert(o));
073 }
074
075 public boolean remove(Object o) {
076 if (value2KeyConverter==null) {
077 throw new IllegalStateException("Cannot remove - value2KeyConverter is null");
078 }
079
080 return master.remove(value2KeyConverter.convert(o));
081 }
082
083 public boolean addAll(Collection c) {
084 if (value2KeyConverter==null) {
085 throw new IllegalStateException("Cannot add - value2KeyConverter is null");
086 }
087
088 Collection converted=new ArrayList();
089 Iterator it=c.iterator();
090 while (it.hasNext()) {
091 converted.add(value2KeyConverter.convert(it.next()));
092 }
093 return master.addAll(converted);
094 }
095
096 public boolean containsAll(Collection c) {
097 if (value2KeyConverter==null) {
098 throw new IllegalStateException("Cannot execute - value2KeyConverter is null");
099 }
100
101 Collection converted=new ArrayList();
102 Iterator it=c.iterator();
103 while (it.hasNext()) {
104 converted.add(value2KeyConverter.convert(it.next()));
105 }
106 return master.containsAll(converted);
107 }
108
109 public boolean removeAll(Collection c) {
110 if (value2KeyConverter==null) {
111 throw new IllegalStateException("Cannot remove - value2KeyConverter is null");
112 }
113
114 Collection converted=new ArrayList();
115 Iterator it=c.iterator();
116 while (it.hasNext()) {
117 converted.add(value2KeyConverter.convert(it.next()));
118 }
119 return master.removeAll(converted);
120 }
121
122 public boolean retainAll(Collection c) {
123 if (value2KeyConverter==null) {
124 throw new IllegalStateException("Cannot retain - value2KeyConverter is null");
125 }
126
127 Collection converted=new ArrayList();
128 Iterator it=c.iterator();
129 while (it.hasNext()) {
130 converted.add(value2KeyConverter.convert(it.next()));
131 }
132 return master.retainAll(converted);
133 }
134
135 public Iterator iterator() {
136 final Iterator masterIterator=master.iterator();
137 return new Iterator() {
138
139 public void remove() {
140 masterIterator.remove();
141 }
142
143 public boolean hasNext() {
144 return masterIterator.hasNext();
145 }
146
147 public Object next() {
148 return key2ValueConverter.convert(masterIterator.next());
149 }
150 };
151 }
152
153 public Object[] toArray(Object[] a) {
154 if (a.length < size()) {
155 a = (Object[]) java.lang.reflect.Array.newInstance(
156 a.getClass().getComponentType(), size());
157 }
158
159
160 Iterator it=master.iterator();
161 for (int i=0; it.hasNext(); i++) {
162 a[i]=key2ValueConverter.convert(it.next());
163 }
164
165 if (a.length > size()) {
166 a[size()] = null;
167 }
168
169 return a;
170 }
171
172 }