Source Code Cross Referenced for WeakHashSet.java in  » Development » OVal » net » sf » oval » internal » 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 » Development » OVal » net.sf.oval.internal.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*******************************************************************************
002:         * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
003:         * Thomschke.
004:         * 
005:         * All Rights Reserved. This program and the accompanying materials
006:         * are made available under the terms of the Eclipse Public License v1.0
007:         * which accompanies this distribution, and is available at
008:         * http://www.eclipse.org/legal/epl-v10.html
009:         * 
010:         * Contributors:
011:         *     Sebastian Thomschke - initial implementation.
012:         *******************************************************************************/package net.sf.oval.internal.util;
013:
014:        import java.io.ObjectInputStream;
015:        import java.io.ObjectOutputStream;
016:        import java.io.Serializable;
017:        import java.util.Collection;
018:        import java.util.Iterator;
019:        import java.util.Set;
020:        import java.util.WeakHashMap;
021:
022:        /**
023:         * @author Sebastian Thomschke
024:         */
025:        public class WeakHashSet<E> implements  Set<E>, Serializable {
026:            private static final long serialVersionUID = 1L;
027:
028:            private transient WeakHashMap<E, Object> map;
029:
030:            /**
031:             * Constructs a new, empty <tt>WeakHashSet</tt>; the backing <tt>WeakHashMap</tt> instance has
032:             * default initial capacity (16) and load factor (0.75).
033:             */
034:            public WeakHashSet() {
035:                map = new WeakHashMap<E, Object>();
036:            }
037:
038:            /**
039:             * Constructs a new, empty <tt>WeakHashSet</tt>; the backing <tt>WeakHashMap</tt> instance has
040:             * the given initial capacity and the default load factor (0.75).
041:             */
042:            public WeakHashSet(final int initialCapacity) {
043:                map = new WeakHashMap<E, Object>(initialCapacity);
044:            }
045:
046:            public boolean add(final E o) {
047:                return map.put(o, Boolean.TRUE) == null;
048:            }
049:
050:            public boolean addAll(final Collection<? extends E> c) {
051:                int count = 0;
052:                for (final E e : c) {
053:                    if (add(e))
054:                        count++;
055:                }
056:                return count > 0;
057:            }
058:
059:            public void clear() {
060:                map.clear();
061:            }
062:
063:            public boolean contains(final Object o) {
064:                return map.containsKey(o);
065:            }
066:
067:            public boolean containsAll(final Collection<?> c) {
068:                return map.keySet().containsAll(c);
069:            }
070:
071:            @Override
072:            public boolean equals(Object o) {
073:                if (o == this )
074:                    return true;
075:
076:                if (!(o instanceof  Set))
077:                    return false;
078:
079:                if (((Set) o).size() != size())
080:                    return false;
081:
082:                return containsAll((Set) o);
083:            }
084:
085:            @Override
086:            public int hashCode() {
087:                int hash = 0;
088:                for (final E e : map.keySet()) {
089:                    if (e != null)
090:                        hash += e.hashCode();
091:                }
092:                return hash;
093:            }
094:
095:            public boolean isEmpty() {
096:                return map.isEmpty();
097:            }
098:
099:            public Iterator<E> iterator() {
100:                return map.keySet().iterator();
101:            }
102:
103:            /**
104:             * Reconstitute the <tt>WeakHashSet</tt> instance from a stream (that is,
105:             * deserialize it).
106:             */
107:            @SuppressWarnings("unchecked")
108:            private void readObject(final ObjectInputStream ois)
109:                    throws java.io.IOException, ClassNotFoundException {
110:                // materialize any hidden serialization magic
111:                ois.defaultReadObject();
112:
113:                // materialize the size
114:                final int size = ois.readInt();
115:
116:                // materialize the elements
117:                map = new WeakHashMap<E, Object>(size);
118:                for (int i = 0; i < size; i++) {
119:                    map.put((E) ois.readObject(), Boolean.TRUE);
120:                }
121:            }
122:
123:            public boolean remove(final Object o) {
124:                return map.remove(o) == Boolean.TRUE;
125:            }
126:
127:            public boolean removeAll(final Collection<?> c) {
128:                return map.keySet().removeAll(c);
129:            }
130:
131:            public boolean retainAll(final Collection<?> c) {
132:                return map.keySet().retainAll(c);
133:            }
134:
135:            public int size() {
136:                return map.size();
137:            }
138:
139:            public Object[] toArray() {
140:                return map.keySet().toArray();
141:            }
142:
143:            public <T> T[] toArray(final T[] a) {
144:                return map.keySet().toArray(a);
145:            }
146:
147:            /**
148:             * Save the state of this <tt>WeakHashSet</tt> instance to a stream (that is,
149:             * serialize this set).
150:             */
151:            private void writeObject(final ObjectOutputStream oos)
152:                    throws java.io.IOException {
153:                // serialize any hidden serialization magic
154:                oos.defaultWriteObject();
155:
156:                // serialize the set's size
157:                oos.writeInt(map.size());
158:
159:                // serialize the set's elements
160:                for (final E e : map.keySet()) {
161:                    oos.writeObject(e);
162:                }
163:            }
164:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.