ConvertingCollection.java

biz/hammurapi/util/ConvertingCollection.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 015 Do not change parameter value. For comprehensibility, formal parameters should be final 2 174:13
Java Inspector 049 Use a Collection instead of arrays Object[] 2 71:23
Java Inspector 089 Constructor is not properly documented 2 51:9
Java Inspector 089 Parameter master is not documented 2 51:9
Java Inspector 089 Parameter key2ValueConverter is not documented 2 51:9
Java Inspector 089 Parameter value2KeyConverter is not documented 2 51:9
Java Inspector 089 Undocumented method 2 58:9
Java Inspector 089 Undocumented method 2 62:9
Java Inspector 089 Undocumented method 2 66:9
Java Inspector 089 Undocumented method 2 70:9
Java Inspector 089 Undocumented method 2 78:9
Java Inspector 089 Undocumented method 2 86:9
Java Inspector 089 Undocumented method 2 94:9
Java Inspector 089 Undocumented method 2 102:9
Java Inspector 089 Undocumented method 2 115:9
Java Inspector 089 Undocumented method 2 128:9
Java Inspector 089 Undocumented method 2 141:9
Java Inspector 089 Undocumented method 2 154:9
Java Inspector 089 Undocumented method 2 158:25
Java Inspector 089 Undocumented method 2 162:25
Java Inspector 089 Undocumented method 2 166:25
Java Inspector 089 Undocumented method 2 172:9
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 80:57
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 88:57
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 96:57
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 104:57
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 117:57
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 130:57
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 143:57

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.util;
24
25import java.util.ArrayList;
26import java.util.Collection;
27import java.util.Iterator;
28
29import biz.hammurapi.convert.ConverterClosure;
30
31/**
32 * Collection that contains elements of one type but appears as
33 * containing elements of another type. Conversion is done by a
34 * Converter. Usage - keep collection of 'keys' and retrieve
35 * 'values' from secondary storage on demand.
36 * @author Pavel Vlasov
37 *
38 * @version $Revision: 1.3 $
39 */
40public class ConvertingCollection implements Collection {
41
42 private Collection master;
43 private ConverterClosure key2ValueConverter;
44 private ConverterClosure value2KeyConverter;
45
46 /**
47 * @param master
48 * @param key2ValueConverter
49 * @param value2KeyConverter
50 */
51 public ConvertingCollection(Collection master, ConverterClosure key2ValueConverter, ConverterClosure value2KeyConverter) {
52 super();
53 this.master = master;
54 this.key2ValueConverter = key2ValueConverter;
55 this.value2KeyConverter = value2KeyConverter;
56 }
57
58 public int size() {
59 return master.size();
60 }
61
62 public void clear() {
63 master.clear();
64 }
65
66 public boolean isEmpty() {
67 return master.isEmpty();
68 }
69
70 public Object[] toArray() {
71 Object[] ret = master.toArray();
72 for (int i=0; i<ret.length; i++) {
73 ret[i]=key2ValueConverter.convert(ret[i]);
74 }
75 return ret;
76 }
77
78 public boolean add(Object o) {
79 if (value2KeyConverter==null) {
80 throw new IllegalStateException("Cannot add - value2KeyConverter is null");
81 }
82
83 return master.add(value2KeyConverter.convert(o));
84 }
85
86 public boolean contains(Object o) {
87 if (value2KeyConverter==null) {
88 throw new IllegalStateException("Cannot execute - value2KeyConverter is null");
89 }
90
91 return master.contains(value2KeyConverter.convert(o));
92 }
93
94 public boolean remove(Object o) {
95 if (value2KeyConverter==null) {
96 throw new IllegalStateException("Cannot remove - value2KeyConverter is null");
97 }
98
99 return master.remove(value2KeyConverter.convert(o));
100 }
101
102 public boolean addAll(Collection c) {
103 if (value2KeyConverter==null) {
104 throw new IllegalStateException("Cannot add - value2KeyConverter is null");
105 }
106
107 Collection converted=new ArrayList();
108 Iterator it=c.iterator();
109 while (it.hasNext()) {
110 converted.add(value2KeyConverter.convert(it.next()));
111 }
112 return master.addAll(converted);
113 }
114
115 public boolean containsAll(Collection c) {
116 if (value2KeyConverter==null) {
117 throw new IllegalStateException("Cannot execute - value2KeyConverter is null");
118 }
119
120 Collection converted=new ArrayList();
121 Iterator it=c.iterator();
122 while (it.hasNext()) {
123 converted.add(value2KeyConverter.convert(it.next()));
124 }
125 return master.containsAll(converted);
126 }
127
128 public boolean removeAll(Collection c) {
129 if (value2KeyConverter==null) {
130 throw new IllegalStateException("Cannot remove - value2KeyConverter is null");
131 }
132
133 Collection converted=new ArrayList();
134 Iterator it=c.iterator();
135 while (it.hasNext()) {
136 converted.add(value2KeyConverter.convert(it.next()));
137 }
138 return master.removeAll(converted);
139 }
140
141 public boolean retainAll(Collection c) {
142 if (value2KeyConverter==null) {
143 throw new IllegalStateException("Cannot retain - value2KeyConverter is null");
144 }
145
146 Collection converted=new ArrayList();
147 Iterator it=c.iterator();
148 while (it.hasNext()) {
149 converted.add(value2KeyConverter.convert(it.next()));
150 }
151 return master.retainAll(converted);
152 }
153
154 public Iterator iterator() {
155 final Iterator masterIterator=master.iterator();
156 return new Iterator() {
157
158 public void remove() {
159 masterIterator.remove();
160 }
161
162 public boolean hasNext() {
163 return masterIterator.hasNext();
164 }
165
166 public Object next() {
167 return key2ValueConverter.convert(masterIterator.next());
168 }
169 };
170 }
171
172 public Object[] toArray(Object[] a) {
173 if (a.length < size()) {
174 a = (Object[]) java.lang.reflect.Array.newInstance(
175 a.getClass().getComponentType(), size());
176 }
177
178
179 Iterator it=master.iterator();
180 for (int i=0; it.hasNext(); i++) {
181 a[i]=key2ValueConverter.convert(it.next());
182 }
183
184 if (a.length > size()) {
185 a[size()] = null;
186 }
187
188 return a;
189 }
190
191}
192