Source Code Cross Referenced for ConvertingCollection.java in  » J2EE » hgcommons » biz » hammurapi » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » J2EE » hgcommons » biz.hammurapi.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.