Commit 6ba7ed48 authored by bryce's avatar bryce
Browse files

* Makefile.am: Add HashSet.java and java/lang/ref classes.

	Remove BasicMapEntry.java and Bucket.java.
	* Makefile.in: Rebuilt.
	* java/util/HashMap.java: Rewritten.
	* java/util/HashSet.java: Imported from classpath.
	* java/util/WeakHashMap.java: Imported from classpath.
	* java/util/Hashtable.java: Rewritten based on new HashMap code.
	* java/util/Bucket.java: Deleted.
	* java/util/BasicMapEntry.java: Deleted.
	* java/util/Collections.java (search): Use a for-loop, not iterator
	hasNext().
	(copy): Use a for-loop. Throw an IndexOutOfBoundsException if run out
	of elements in source.
	(max): Use a for-loop.
	(min): Ditto.
	(reverse): Keep track of positions instead of using Iterator's
	nextIndex() and previousIndex().
	(shuffle(List)): Initialize defaultRandom if required using
	double-check thread safety idiom. Call two-argument shuffle method
	using defaultRandom.
	(defaultRandom): New field.
	(shuffle(List, Random)): Use a for-loop. Keep track of pos instead
	of using previousIndex() and nextIndex().
	(singletonMap(iterator)): Use a HashMap.Entry, not BasicMapEntry.
	* java/util/AbstractCollection.java (toString): Use a StringBuffer.
	* java/util/AbstractMap.java (toString): Use StringBuffer.
	* java/lang/ref/PhantomReference.java: Imported from classpath.
	* java/lang/ref/SoftReference.java: Ditto.
	* java/lang/ref/Reference.java: Ditto.
	* java/lang/ref/WeakReference.java: Ditto.
	* java/lang/ref/ReferenceQueue.java: Ditto.


git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@38183 138bc75d-0d04-0410-961f-82ee72b054a4
parent ab28c005
2000-12-11 Bryce McKinlay <bryce@albatross.co.nz>
* Makefile.am: Add HashSet.java and java/lang/ref classes.
Remove BasicMapEntry.java and Bucket.java.
* Makefile.in: Rebuilt.
* java/util/HashMap.java: Rewritten.
* java/util/HashSet.java: Imported from classpath.
* java/util/WeakHashMap.java: Imported from classpath.
* java/util/Hashtable.java: Rewritten based on new HashMap code.
* java/util/Bucket.java: Deleted.
* java/util/BasicMapEntry.java: Deleted.
* java/util/Collections.java (search): Use a for-loop, not iterator
hasNext().
(copy): Use a for-loop. Throw an IndexOutOfBoundsException if run out
of elements in source.
(max): Use a for-loop.
(min): Ditto.
(reverse): Keep track of positions instead of using Iterator's
nextIndex() and previousIndex().
(shuffle(List)): Initialize defaultRandom if required using
double-check thread safety idiom. Call two-argument shuffle method
using defaultRandom.
(defaultRandom): New field.
(shuffle(List, Random)): Use a for-loop. Keep track of pos instead of
using previousIndex() and nextIndex().
(singletonMap(iterator)): Use a HashMap.Entry, not BasicMapEntry.
* java/util/AbstractCollection.java (toString): Use a StringBuffer.
* java/util/AbstractMap.java (toString): Use StringBuffer.
* java/lang/ref/PhantomReference.java: Imported from classpath.
* java/lang/ref/SoftReference.java: Ditto.
* java/lang/ref/Reference.java: Ditto.
* java/lang/ref/WeakReference.java: Ditto.
* java/lang/ref/ReferenceQueue.java: Ditto.
2000-12-10 Richard Henderson <rth@redhat.com>
* configure.host: Recognize alpha*-*, not alphaev6-*.
......
......@@ -843,15 +843,6 @@ java/lang/UnsupportedOperationException.java \
java/lang/VerifyError.java \
java/lang/VirtualMachineError.java \
java/lang/Void.java \
java/lang/reflect/AccessibleObject.java \
java/lang/reflect/Array.java \
java/lang/reflect/Constructor.java \
java/lang/reflect/Field.java \
java/lang/reflect/InvocationTargetException.java \
java/lang/reflect/Member.java \
java/lang/reflect/Method.java \
java/lang/reflect/Modifier.java \
java/lang/reflect/ReflectPermission.java \
java/io/BlockDataException.java \
java/io/BufferedInputStream.java \
java/io/BufferedOutputStream.java \
......@@ -934,9 +925,7 @@ java/util/AbstractSequentialList.java \
java/util/AbstractSet.java \
java/util/ArrayList.java \
java/util/Arrays.java \
java/util/BasicMapEntry.java \
java/util/BitSet.java \
java/util/Bucket.java \
java/util/Calendar.java \
java/util/Collection.java \
java/util/Collections.java \
......@@ -950,6 +939,7 @@ java/util/EventListener.java \
java/util/EventObject.java \
java/util/GregorianCalendar.java \
java/util/HashMap.java \
java/util/HashSet.java \
java/util/Hashtable.java \
java/util/Iterator.java \
java/util/LinkedList.java \
......@@ -978,6 +968,7 @@ java/util/Timer.java \
java/util/TimerTask.java \
java/util/TooManyListenersException.java \
java/util/Vector.java
#java/util/WeakHashmap.java
## List of all .java files to be compiled. Please keep this list
......@@ -1023,6 +1014,20 @@ gnu/java/locale/Calendar_nl.java \
gnu/java/security/provider/Gnu.java \
gnu/java/security/provider/SHA.java \
gnu/java/security/provider/SHA1PRNG.java \
java/lang/ref/PhantomReference.java \
java/lang/ref/Reference.java \
java/lang/ref/ReferenceQueue.java \
java/lang/ref/SoftReference.java \
java/lang/ref/WeakReference.java \
java/lang/reflect/AccessibleObject.java \
java/lang/reflect/Array.java \
java/lang/reflect/Constructor.java \
java/lang/reflect/Field.java \
java/lang/reflect/InvocationTargetException.java \
java/lang/reflect/Member.java \
java/lang/reflect/Method.java \
java/lang/reflect/Modifier.java \
java/lang/reflect/ReflectPermission.java \
java/math/BigDecimal.java \
java/math/BigInteger.java \
java/net/BindException.java \
......
......@@ -120,43 +120,29 @@ here = @here@
libgcj_basedir = @libgcj_basedir@
AUTOMAKE_OPTIONS = foreign no-installinfo
@TESTSUBDIR_TRUE@SUBDIRS = \
@TESTSUBDIR_TRUE@$(DIRLTDL) testsuite gcj include
@TESTSUBDIR_FALSE@SUBDIRS = \
@TESTSUBDIR_FALSE@$(DIRLTDL) gcj include
@USE_LIBDIR_TRUE@toolexeclibdir = \
@USE_LIBDIR_TRUE@$(libdir)$(MULTISUBDIR)
@USE_LIBDIR_FALSE@toolexeclibdir = \
@USE_LIBDIR_FALSE@$(toolexecdir)/lib$(MULTISUBDIR)
@USE_LIBDIR_FALSE@toolexecdir = \
@USE_LIBDIR_FALSE@$(exec_prefix)/$(target_alias)
@NO_X_TRUE@cond_x_ltlibrary = \
@NO_X_FALSE@cond_x_ltlibrary = \
@NO_X_FALSE@libgcjx.la
@TESTSUBDIR_TRUE@SUBDIRS = @TESTSUBDIR_TRUE@$(DIRLTDL) testsuite gcj include
@TESTSUBDIR_FALSE@SUBDIRS = @TESTSUBDIR_FALSE@$(DIRLTDL) gcj include
@USE_LIBDIR_TRUE@toolexeclibdir = @USE_LIBDIR_TRUE@$(libdir)$(MULTISUBDIR)
@USE_LIBDIR_FALSE@toolexeclibdir = @USE_LIBDIR_FALSE@$(toolexecdir)/lib$(MULTISUBDIR)
@USE_LIBDIR_FALSE@toolexecdir = @USE_LIBDIR_FALSE@$(exec_prefix)/$(target_alias)
@NO_X_TRUE@cond_x_ltlibrary =
@NO_X_FALSE@cond_x_ltlibrary = @NO_X_FALSE@libgcjx.la
toolexeclib_LTLIBRARIES = libgcj.la $(cond_x_ltlibrary)
toolexeclib_DATA = libgcj.spec
data_DATA = libgcj.jar
@NEEDS_DATA_START_TRUE@toolexeclib_LIBRARIES = \
@NEEDS_DATA_START_TRUE@libgcjdata.a
@NEEDS_DATA_START_TRUE@libgcjdata_a_SOURCES = \
@NEEDS_DATA_START_TRUE@libgcjdata.c
@NEEDS_DATA_START_TRUE@toolexeclib_LIBRARIES = @NEEDS_DATA_START_TRUE@libgcjdata.a
@NEEDS_DATA_START_TRUE@libgcjdata_a_SOURCES = @NEEDS_DATA_START_TRUE@libgcjdata.c
@NATIVE_TRUE@bin_PROGRAMS = \
@NATIVE_TRUE@jv-convert gij
@NATIVE_TRUE@bin_PROGRAMS = @NATIVE_TRUE@jv-convert gij
bin_SCRIPTS = addr2name.awk
@CANADIAN_TRUE@@NULL_TARGET_TRUE@ZIP = \
@CANADIAN_TRUE@@NULL_TARGET_TRUE@$(MULTIBUILDTOP)../$(COMPPATH)/fastjar/fastjar$(EXEEXT)
@CANADIAN_TRUE@@NULL_TARGET_FALSE@ZIP = \
@CANADIAN_TRUE@@NULL_TARGET_FALSE@fastjar
@CANADIAN_FALSE@ZIP = \
@CANADIAN_FALSE@$(MULTIBUILDTOP)../$(COMPPATH)/fastjar/fastjar$(EXEEXT)
@CANADIAN_TRUE@GCJH = \
@CANADIAN_TRUE@gcjh
@CANADIAN_FALSE@GCJH = \
@CANADIAN_FALSE@$(MULTIBUILDTOP)../$(COMPPATH)/gcc/gcjh$(EXEEXT)
@CANADIAN_TRUE@@NULL_TARGET_TRUE@ZIP = @CANADIAN_TRUE@@NULL_TARGET_TRUE@$(MULTIBUILDTOP)../$(COMPPATH)/fastjar/fastjar$(EXEEXT)
@CANADIAN_TRUE@@NULL_TARGET_FALSE@ZIP = @CANADIAN_TRUE@@NULL_TARGET_FALSE@fastjar
@CANADIAN_FALSE@ZIP = @CANADIAN_FALSE@$(MULTIBUILDTOP)../$(COMPPATH)/fastjar/fastjar$(EXEEXT)
@CANADIAN_TRUE@GCJH = @CANADIAN_TRUE@gcjh
@CANADIAN_FALSE@GCJH = @CANADIAN_FALSE@$(MULTIBUILDTOP)../$(COMPPATH)/gcc/gcjh$(EXEEXT)
GCJCOMPILE = $(LIBTOOL) --tag=GCJ --mode=compile $(GCJ) -fassume-compiled -fclasspath=$(here) -L$(here) $(JC1FLAGS) -MD -MT $@ -MF $(@:.lo=.d) -c
GCJLINK = $(LIBTOOL) --mode=link $(GCJ) -L$(here) $(JC1FLAGS) $(LDFLAGS) -o $@
......@@ -171,10 +157,8 @@ AM_CXXFLAGS = -fno-rtti -fvtable-thunks -fasynchronous-exceptions \
-fdollars-in-identifiers \
@LIBGCJ_CXXFLAGS@ @EXCEPTIONSPEC@ @X_CFLAGS@ $(WARNINGS) -D_GNU_SOURCE
@USING_GCC_TRUE@AM_CFLAGS = \
@USING_GCC_TRUE@@LIBGCJ_CFLAGS@ $(WARNINGS)
@USING_GCC_FALSE@AM_CFLAGS = \
@USING_GCC_FALSE@@LIBGCJ_CFLAGS@
@USING_GCC_TRUE@AM_CFLAGS = @USING_GCC_TRUE@@LIBGCJ_CFLAGS@ $(WARNINGS)
@USING_GCC_FALSE@AM_CFLAGS = @USING_GCC_FALSE@@LIBGCJ_CFLAGS@
JCFLAGS = -g
JC1FLAGS = -g @LIBGCJ_JAVAFLAGS@
......@@ -243,8 +227,7 @@ extra_headers = java/lang/Object.h java/lang/Class.h
NM = nm
@NATIVE_TRUE@@MAINTAINER_MODE_TRUE@noinst_PROGRAMS = \
@NATIVE_TRUE@@MAINTAINER_MODE_TRUE@gen-from-JIS
@NATIVE_TRUE@@MAINTAINER_MODE_TRUE@noinst_PROGRAMS = @NATIVE_TRUE@@MAINTAINER_MODE_TRUE@gen-from-JIS
CONVERT_DIR = gnu/gcj/convert
......@@ -611,15 +594,6 @@ java/lang/UnsupportedOperationException.java \
java/lang/VerifyError.java \
java/lang/VirtualMachineError.java \
java/lang/Void.java \
java/lang/reflect/AccessibleObject.java \
java/lang/reflect/Array.java \
java/lang/reflect/Constructor.java \
java/lang/reflect/Field.java \
java/lang/reflect/InvocationTargetException.java \
java/lang/reflect/Member.java \
java/lang/reflect/Method.java \
java/lang/reflect/Modifier.java \
java/lang/reflect/ReflectPermission.java \
java/io/BlockDataException.java \
java/io/BufferedInputStream.java \
java/io/BufferedOutputStream.java \
......@@ -702,9 +676,7 @@ java/util/AbstractSequentialList.java \
java/util/AbstractSet.java \
java/util/ArrayList.java \
java/util/Arrays.java \
java/util/BasicMapEntry.java \
java/util/BitSet.java \
java/util/Bucket.java \
java/util/Calendar.java \
java/util/Collection.java \
java/util/Collections.java \
......@@ -718,6 +690,7 @@ java/util/EventListener.java \
java/util/EventObject.java \
java/util/GregorianCalendar.java \
java/util/HashMap.java \
java/util/HashSet.java \
java/util/Hashtable.java \
java/util/Iterator.java \
java/util/LinkedList.java \
......@@ -747,6 +720,7 @@ java/util/TimerTask.java \
java/util/TooManyListenersException.java \
java/util/Vector.java
#java/util/WeakHashmap.java
ordinary_java_source_files = $(core_java_source_files) \
gnu/gcj/RawData.java \
......@@ -785,6 +759,20 @@ gnu/java/locale/Calendar_nl.java \
gnu/java/security/provider/Gnu.java \
gnu/java/security/provider/SHA.java \
gnu/java/security/provider/SHA1PRNG.java \
java/lang/ref/PhantomReference.java \
java/lang/ref/Reference.java \
java/lang/ref/ReferenceQueue.java \
java/lang/ref/SoftReference.java \
java/lang/ref/WeakReference.java \
java/lang/reflect/AccessibleObject.java \
java/lang/reflect/Array.java \
java/lang/reflect/Constructor.java \
java/lang/reflect/Field.java \
java/lang/reflect/InvocationTargetException.java \
java/lang/reflect/Member.java \
java/lang/reflect/Method.java \
java/lang/reflect/Modifier.java \
java/lang/reflect/ReflectPermission.java \
java/math/BigDecimal.java \
java/math/BigInteger.java \
java/net/BindException.java \
......@@ -1203,7 +1191,7 @@ libgcj-test.spec.in libgcj.spec.in
DISTFILES = $(DIST_COMMON) $(SOURCES) $(HEADERS) $(TEXINFOS) $(EXTRA_DIST)
TAR = tar
TAR = gtar
GZIP_ENV = --best
DIST_SUBDIRS = @DIRLTDL@ testsuite gcj include @DIRLTDL@ gcj include
DEP_FILES = .deps/$(srcdir)/$(CONVERT_DIR)/gen-from-JIS.P \
......@@ -1562,7 +1550,9 @@ DEP_FILES = .deps/$(srcdir)/$(CONVERT_DIR)/gen-from-JIS.P \
.deps/java/lang/natMath.P .deps/java/lang/natObject.P \
.deps/java/lang/natRuntime.P .deps/java/lang/natString.P \
.deps/java/lang/natSystem.P .deps/java/lang/natThread.P \
.deps/java/lang/natThrowable.P \
.deps/java/lang/natThrowable.P .deps/java/lang/ref/PhantomReference.P \
.deps/java/lang/ref/Reference.P .deps/java/lang/ref/ReferenceQueue.P \
.deps/java/lang/ref/SoftReference.P .deps/java/lang/ref/WeakReference.P \
.deps/java/lang/reflect/AccessibleObject.P \
.deps/java/lang/reflect/Array.P .deps/java/lang/reflect/Constructor.P \
.deps/java/lang/reflect/Field.P \
......@@ -1682,8 +1672,7 @@ DEP_FILES = .deps/$(srcdir)/$(CONVERT_DIR)/gen-from-JIS.P \
.deps/java/util/AbstractCollection.P .deps/java/util/AbstractList.P \
.deps/java/util/AbstractMap.P .deps/java/util/AbstractSequentialList.P \
.deps/java/util/AbstractSet.P .deps/java/util/ArrayList.P \
.deps/java/util/Arrays.P .deps/java/util/BasicMapEntry.P \
.deps/java/util/BitSet.P .deps/java/util/Bucket.P \
.deps/java/util/Arrays.P .deps/java/util/BitSet.P \
.deps/java/util/Calendar.P .deps/java/util/Collection.P \
.deps/java/util/Collections.P .deps/java/util/Comparator.P \
.deps/java/util/ConcurrentModificationException.P \
......@@ -1691,11 +1680,11 @@ DEP_FILES = .deps/$(srcdir)/$(CONVERT_DIR)/gen-from-JIS.P \
.deps/java/util/EmptyStackException.P .deps/java/util/Enumeration.P \
.deps/java/util/EventListener.P .deps/java/util/EventObject.P \
.deps/java/util/GregorianCalendar.P .deps/java/util/HashMap.P \
.deps/java/util/Hashtable.P .deps/java/util/Iterator.P \
.deps/java/util/LinkedList.P .deps/java/util/List.P \
.deps/java/util/ListIterator.P .deps/java/util/ListResourceBundle.P \
.deps/java/util/Locale.P .deps/java/util/Map.P \
.deps/java/util/MissingResourceException.P \
.deps/java/util/HashSet.P .deps/java/util/Hashtable.P \
.deps/java/util/Iterator.P .deps/java/util/LinkedList.P \
.deps/java/util/List.P .deps/java/util/ListIterator.P \
.deps/java/util/ListResourceBundle.P .deps/java/util/Locale.P \
.deps/java/util/Map.P .deps/java/util/MissingResourceException.P \
.deps/java/util/NoSuchElementException.P .deps/java/util/Observable.P \
.deps/java/util/Observer.P .deps/java/util/Properties.P \
.deps/java/util/PropertyPermission.P \
......@@ -2103,7 +2092,7 @@ distdir: $(DISTFILES)
@for file in $(DISTFILES); do \
d=$(srcdir); \
if test -d $$d/$$file; then \
cp -pr $$/$$file $(distdir)/$$file; \
cp -pr $$d/$$file $(distdir)/$$file; \
else \
test -f $(distdir)/$$file \
|| ln $$d/$$file $(distdir)/$$file 2> /dev/null \
......
/* java.lang.ref.PhantomReference
Copyright (C) 1999 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
As a special exception, if you link this library with other files to
produce an executable, this library does not by itself cause the
resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why the
executable file might be covered by the GNU General Public License. */
package java.lang.ref;
/**
* A phantom reference is useful, to get notified, when an object got
* finalized. You can't access that object though, since it is
* finalized. This is the reason, why <code>get()</code> always
* returns null.
*
* @author Jochen Hoenicke
*/
public class PhantomReference
extends Reference
{
/**
* Creates a new phantom reference.
* @param referent the object that should be watched.
* @param q the queue that should be notified, if the referent was
* finalized. This mustn't be <code>null</code>.
* @exception NullPointerException if q is null.
*/
public PhantomReference(Object referent, ReferenceQueue q)
{
super(referent, q);
}
/**
* Returns the object, this reference refers to.
* @return <code>null</code>, since the refered object may be
* finalized and thus not accessible.
*/
public Object get()
{
return null;
}
}
/* java.lang.ref.Reference
Copyright (C) 1999 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
As a special exception, if you link this library with other files to
produce an executable, this library does not by itself cause the
resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why the
executable file might be covered by the GNU General Public License. */
package java.lang.ref;
/**
* This is the base class of all references. A reference allows
* refering to an object without preventing the garbage collection to
* collect it. The only way to get the referred object is via the
* <code>get()</code>-method. This method will return
* <code>null</code> if the object was collected. <br>
*
* A reference may be registered with a queue. When a referred
* element gets collected the reference will be put on the queue, so
* that you will be notified. <br>
*
* There are currently three types of references: soft reference,
* weak reference and phantom reference. <br>
*
* Soft references will be cleared if the garbage collection is told
* to free some memory and there are no unreferenced or weakly referenced
* objects. It is useful for caches. <br>
*
* Weak references will be cleared as soon as the garbage collection
* determines that the refered object is only weakly reachable. They
* are useful as keys in hashtables (see <code>WeakHashtable</code>) as
* you get notified when nobody has the key anymore.
*
* Phantom references don't prevent finalization. If an object is only
* phantom reachable, it will be finalized, and the reference will be
* enqueued, but not cleared. Since you mustn't access an finalized
* object, the <code>get</code> method of a phantom reference will never
* work. It is useful to keep track, when an object is finalized.
*
* @author Jochen Hoenicke
* @see java.util.WeakHashtable
*/
public abstract class Reference
{
/**
* The underlying object. This field is handled in a special way by
* the garbage collection.
*/
Object referent;
/**
* The queue this reference is registered on. This is null, if this
* wasn't registered to any queue or reference was already enqueued.
*/
ReferenceQueue queue;
/**
* Link to the next entry on the queue. If this is null, this
* reference is not enqueued. Otherwise it points to the next
* reference. The last reference on a queue will point to itself
* (not to null, that value is used to mark a not enqueued
* reference).
*/
Reference nextOnQueue;
/**
* This lock should be taken by the garbage collection, before
* determining reachability. It will prevent the get()-method to
* return the reference so that reachability doesn't change.
*/
static Object lock = new Object();
/**
* Creates a new reference that is not registered to any queue.
* Since it is package private, it is not possible to overload this
* class in a different package.
* @param referent the object we refer to.
*/
Reference(Object ref)
{
referent = ref;
}
/**
* Creates a reference that is registered to a queue. Since this is
* package private, it is not possible to overload this class in a
* different package.
* @param referent the object we refer to.
* @param q the reference queue to register on.
* @exception NullPointerException if q is null.
*/
Reference(Object ref, ReferenceQueue q)
{
if (q == null)
throw new NullPointerException();
referent = ref;
queue = q;
}
/**
* Returns the object, this reference refers to.
* @return the object, this reference refers to, or null if the
* reference was cleared.
*/
public Object get()
{
synchronized(lock)
{
return referent;
}
}
/**
* Clears the reference, so that it doesn't refer to its object
* anymore. For soft and weak references this is called by the
* garbage collection. For phantom references you should call
* this when enqueuing the reference.
*/
public void clear()
{
referent = null;
}
/**
* Tells if the object is enqueued on a reference queue.
* @return true if it is enqueued, false otherwise.
*/
public boolean isEnqueued()
{
return nextOnQueue != null;
}
/**
* Enqueue an object on a reference queue. This is normally executed
* by the garbage collection.
*/
public boolean enqueue()
{
if (queue != null)
{
queue.enqueue(this);
queue = null;
return true;
}
return false;
}
}
/* java.lang.ref.ReferenceQueue
Copyright (C) 1999 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
As a special exception, if you link this library with other files to
produce an executable, this library does not by itself cause the
resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why the
executable file might be covered by the GNU General Public License. */
package java.lang.ref;
/**
* This is the queue, where references can enqueue themselve on. Each
* reference may be registered to a queue at initialization time and
* will be appended to the queue, when the enqueue method is called.
*
* The enqueue method may be automatically called by the garbage
* collector if it detects, that the object is only reachable through
* the Reference objects.
*
* @author Jochen Hoenicke
* @see Reference#enqueue()
*/
public class ReferenceQueue
{
/**
* This is a linked list of references. If this is null, the list is
* empty. Otherwise this points to the first reference on the queue.
* The first reference will point to the next reference via the
* <code>nextOnQueue</code> field. The last reference will point to
* itself (not to null, since <code>nextOnQueue</code> is used to
* determine if a reference is enqueued).
*/
private Reference first;
/**
* Creates a new empty reference queue.
*/
public ReferenceQueue()
{
}
/**
* Checks if there is a reference on the queue, returning it
* immediately. The reference will be dequeued.
*
* @return a reference on the queue, if there is one,
* <code>null</code> otherwise.
*/
public synchronized Reference poll()
{
return dequeue();
}
/**
* This is called by reference to enqueue itself on this queue.
* @param ref the reference that should be enqueued.
*/
synchronized void enqueue(Reference ref)
{
/* last reference will point to itself */
ref.nextOnQueue = first == null ? ref : first;
first = ref;
/* this wakes only one remove thread. */
notify();
}
/**
* Remove a reference from the queue, if there is one.
* @return the first element of the queue, or null if there isn't any.
*/
private Reference dequeue()
{
if (first == null)
return null;
Reference result = first;
first = (first == first.nextOnQueue) ? null : first.nextOnQueue;
result.nextOnQueue = null;
return result;
}
/**
* Removes a reference from the queue, blocking for <code>timeout</code>
* until a reference is enqueued.
* @param timeout the timeout period in milliseconds, <code>0</code> means
* wait forever.
* @return the reference removed from the queue, or
* <code>null</code> if timeout period expired.
* @exception InterruptedException if the wait was interrupted.
*/
public synchronized Reference remove(long timeout)
throws InterruptedException
{
if (first == null)
{
wait(timeout);
}
return dequeue();
}
/**
* Removes a reference from the queue, blocking until a reference is
* enqueued.
*
* @return the reference removed from the queue.
* @exception InterruptedException if the wait was interrupted.
*/
public Reference remove()
throws InterruptedException
{
return remove(0L);
}
}
/* java.lang.ref.SoftReference
Copyright (C) 1999 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
As a special exception, if you link this library with other files to
produce an executable, this library does not by itself cause the
resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why the
executable file might be covered by the GNU General Public License. */
package java.lang.ref;
/**
* A soft reference will be cleared, if the object is only softly
* reachable and the garbage collection needs more memory. The garbage
* collection will use an intelligent strategy to determine which soft
* references it should clear. This makes a soft reference ideal for
* caches.<br>
*
* @author Jochen Hoenicke
*/
public class SoftReference
extends Reference
{
/**
* Create a new soft reference, that is not registered to any queue.
* @param referent the object we refer to.
*/
public SoftReference(Object referent)
{
super(referent);
}
/**
* Create a new soft reference.
* @param referent the object we refer to.
* @param q the reference queue to register on.
* @exception NullPointerException if q is null.
*/
public SoftReference(Object referent, ReferenceQueue q)
{
super(referent, q);
}
/**
* Returns the object, this reference refers to.
* @return the object, this reference refers to, or null if the
* reference was cleared.
*/
public Object get()
{
/* Why is this overloaded???
* Maybe for a kind of LRU strategy. */
return super.get();
}
}
/* java.lang.ref.WeakReference
Copyright (C) 1999 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
As a special exception, if you link this library with other files to
produce an executable, this library does not by itself cause the
resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why the
executable file might be covered by the GNU General Public License. */
package java.lang.ref;
/**
* A weak reference will be cleared, if the object is only weakly
* reachable. It is useful for lookup tables, where you aren't
* interested in an entry, if the key isn't reachable anymore.
* <code>WeakHashtable</code> is a complete implementation of such a
* table. <br>
*
* It is also useful to make objects unique: You create a set of weak
* references to those objects, and when you create a new object you
* look in this set, if the object already exists and return it. If
* an object is not referenced anymore, the reference will
* automatically cleared, and you may remove it from the set. <br>
*
* @author Jochen Hoenicke
* @see java.util.WeakHashtable
*/
public class WeakReference
extends Reference
{
/**
* Create a new weak reference, that is not registered to any queue.
* @param referent the object we refer to.
*/
public WeakReference(Object referent)
{
super(referent);
}
/**
* Create a new weak reference.
* @param referent the object we refer to.
* @param q the reference queue to register on.
* @exception NullPointerException if q is null.
*/
public WeakReference(Object referent, ReferenceQueue q)
{
super(referent, q);
}
}
......@@ -332,14 +332,14 @@ public abstract class AbstractCollection implements Collection
{
Iterator itr = iterator();
int size = size();
String r = "[";
StringBuffer r = new StringBuffer("[");
for (int pos = 0; pos < size; pos++)
{
r += itr.next();
r.append(itr.next());
if (pos < size - 1)
r += ", ";
r.append(", ");
}
r += "]";
return r;
r.append("]");
return r.toString();
}
}
......@@ -227,15 +227,18 @@ public abstract class AbstractMap implements Map
{
Iterator entries = entrySet().iterator();
int size = size();
String r = "{";
StringBuffer r = new StringBuffer("{");
for (int pos = 0; pos < size; pos++)
{
r += entries.next();
// Append the toString value of the entries rather than calling
// getKey/getValue. This is more efficient and it matches the JDK
// behaviour.
r.append(entries.next());
if (pos < size - 1)
r += ", ";
r.append(", ");
}
r += "}";
return r;
r.append("}");
return r.toString();
}
public Collection values()
......
......@@ -38,7 +38,6 @@ package java.util;
*/
public abstract class AbstractSet extends AbstractCollection implements Set
{
/**
* Tests whether the given object is equal to this Set. This implementation
* first checks whether this set <em>is</em> the given object, and returns
......
/* BasicMapEntry.java -- a class providing a plain-vanilla implementation of
the Map.Entry interface; could be used anywhere in java.util
Copyright (C) 1998 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
As a special exception, if you link this library with other files to
produce an executable, this library does not by itself cause the
resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why the
executable file might be covered by the GNU General Public License. */
package java.util;
/**
* a class which implements Map.Entry
*
* @author Jon Zeppieri
* @version $Revision: 1.3 $
* @modified $Id: BasicMapEntry.java,v 1.3 2000/03/15 21:59:07 rao Exp $
*/
class BasicMapEntry implements Map.Entry
{
/** the key */
Object key;
/** the value */
Object value;
/**
* construct a new BasicMapEntry with the given key and value
*
* @param newKey the key of this Entry
* @param newValue the value of this Entry
*/
BasicMapEntry(Object newKey, Object newValue)
{
key = newKey;
value = newValue;
}
/**
* returns true if <pre>o</pre> is a Map.Entry and
* <pre>
* (((o.getKey == null) ? (key == null) :
* o.getKey().equals(key)) &&
* ((o.getValue() == null) ? (value == null) :
* o.getValue().equals(value)))
* </pre>
*
* NOTE: the calls to getKey() and getValue() in this implementation
* are <i>NOT</i> superfluous and should not be removed. They insure
* that subclasses such as HashMapEntry work correctly
*
* @param o the Object being tested for equality
*/
public boolean equals(Object o)
{
Map.Entry tester;
Object oTestingKey, oTestingValue;
Object oKey, oValue;
if (o instanceof Map.Entry)
{
tester = (Map.Entry) o;
oKey = getKey();
oValue = getValue();
oTestingKey = tester.getKey();
oTestingValue = tester.getValue();
return (((oTestingKey == null) ? (oKey == null) :
oTestingKey.equals(oKey)) &&
((oTestingValue == null) ? (oValue == null) :
oTestingValue.equals(oValue)));
}
return false;
}
/** returns the key */
public Object getKey()
{
return key;
}
/** returns the value */
public Object getValue()
{
return value;
}
/** the hashCode() for a Map.Entry is
* <pre>
* ((getKey() == null) ? 0 : getKey().hashCode()) ^
* ((getValue() == null) ? 0 : getValue().hashCode());
* </pre>
*
* NOTE: the calls to getKey() and getValue() in this implementation
* are <i>NOT</i> superfluous and should not be removed. They insure
* that subclasses such as HashMapEntry work correctly
*/
public int hashCode()
{
Object oKey = getKey();
Object oValue = getValue();
return ((oKey == null) ? 0 : oKey.hashCode()) ^
((oValue == null) ? 0 : oValue.hashCode());
}
/**
* sets the value of this Map.Entry
*
* @param newValue the new value of this Map.Entry
*/
public Object setValue(Object newValue)
throws java.lang.UnsupportedOperationException, ClassCastException,
IllegalArgumentException, NullPointerException
{
Object oVal = value;
value = newValue;
return oVal;
}
}
/* Bucket.java -- a class providing a hash-bucket data structure
(a lightweight linked list)
Copyright (C) 1998, 2000 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
As a special exception, if you link this library with other files to
produce an executable, this library does not by itself cause the
resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why the
executable file might be covered by the GNU General Public License. */
package java.util;
/**
* a class representing a simple, lightweight linked-list, using Node
* objects as its linked nodes; this is used by Hashtable and HashMap
*
* @author Jon Zeppieri
* @version $Revision: 1.3 $
* @modified $Id: Bucket.java,v 1.3 2000/03/15 21:59:08 rao Exp $
*/
class Bucket
{
/** the first node of the lined list, originally null */
Node first;
/** trivial constructor for a Bucket */
Bucket()
{
}
/** add this key / value pair to the list
*
* @param newNode a Node object to be added to this list
* @return the old value mapped to the key if there was one,
* otherwise null.
*/
Object add(Node newNode)
{
Object oKey;
Object oTestKey = newNode.getKey();
Node it = first;
Node prev = null;
if (it == null) // if the list is empty (the ideal case), we make a new single-node list
{
first = newNode;
return null;
}
else // otherwise try to find where this key already exists in the list,
{// and if it does, replace the value with the new one (and return the old one)
while (it != null)
{
oKey = it.getKey();
if ((oKey == null) ? (oTestKey == null) :
oKey.equals(oTestKey))
{
Object oldValue = it.value;
it.value = newNode.getValue();
return oldValue;
}
prev = it;
it = it.next;
}
prev.next = newNode; // otherwise, just stick this at the
return null; // end of the list
}
}
/**
* remove a Map.Entry in this list with the supplied key and return its value,
* if it exists, else return null
*
* @param key the key we are looking for in this list
*/
Object removeByKey(Object key)
{
Object oEntryKey;
Node prev = null;
Node it = first;
while (it != null)
{
oEntryKey = it.getKey();
if ((oEntryKey == null) ? (key == null) : oEntryKey.equals(key))
{
if (prev == null) // we are removing the first element
first = it.next;
else
prev.next = it.next;
return it.getValue();
}
else
{
prev = it;
it = it.next;
}
}
return null;
}
/**
* return the value which the supplied key maps to, if it maps to anything in this list,
* otherwise, return null
*
* @param key the key mapping to a value that we are looking for
*/
Object getValueByKey(Object key)
{
Node entry = getEntryByKey(key);
return (entry == null) ? null : entry.getValue();
}
/**
* return the Map.Entry which the supplied key is a part of, if such a Map.Entry exists,
* null otherwise
*
* this method is important for HashMap, which can hold null values and the null key
*
* @param key the key for which we are finding the corresponding Map.Entry
*/
Node getEntryByKey(Object key)
{
Object oEntryKey;
Node it = first;
while (it != null)
{
oEntryKey = it.getKey();
if ((oEntryKey == null) ? (key == null) : oEntryKey.equals(key))
return it;
it = it.next;
}
return null;
}
/**
* return true if this list has a Map.Entry whose value equals() the supplied value
*
* @param value the value we are looking to match in this list
*/
boolean containsValue(Object value)
{
Object oEntryValue;
Node it = first;
while (it != null)
{
oEntryValue = it.getValue();
if ((oEntryValue == null) ? (value == null) : oEntryValue.equals(value))
return true;
it = it.next;
}
return false;
}
// INNSER CLASSES ----------------------------------------------------------
/**
* a class represnting a node in our lightweight linked-list
* that we use for hash buckets; a Node object contains a Map.Entry as its
* <pre>value</pre> property and a reference (possibly, even hopefully, null)
* to another Node as its <pre>next</pre> property.
*
* There <i>is</i> a reason for not using a highly generic "LinkedNode" type
* class: we want to eliminate runtime typechecks.
*
* @author Jon Zeppieri
* @version $Revision: 1.3 $
* @modified $Id: Bucket.java,v 1.3 2000/03/15 21:59:08 rao Exp $
*/
static class Node extends BasicMapEntry implements Map.Entry
{
/** a reference to the next node in the linked list */
Node next;
/** non-trivial contructor -- sets the <pre>value</pre> of the Bucket upon instantiation */
Node(Object key, Object value)
{
super(key, value);
}
}
// EOF ------------------------------------------------------------------------
}
......@@ -149,10 +149,10 @@ public class Collections
// is sequential-access.
if (l instanceof AbstractSequentialList)
{
ListIterator i = l.listIterator();
while (i.hasNext())
ListIterator itr = l.listIterator();
for (int i = l.size() - 1; i >= 0; --i)
{
final int d = compare(key, i.next(), c);
final int d = compare(key, itr.next(), c);
if (d == 0)
{
return pos;
......@@ -264,10 +264,18 @@ public class Collections
{
Iterator i1 = source.iterator();
ListIterator i2 = dest.listIterator();
while (i1.hasNext())
try
{
for (int i = source.size() - 1; i >= 0; --i)
{
i2.next();
i2.set(i1.next());
}
}
catch (NoSuchElementException x)
{
i2.next();
i2.set(i1.next());
throw new IndexOutOfBoundsException("Source doesn't fit in dest.");
}
}
......@@ -305,11 +313,11 @@ public class Collections
*/
public static void fill(List l, Object val)
{
ListIterator i = l.listIterator();
while (i.hasNext())
ListIterator itr = l.listIterator();
for (int i = l.size() - 1; i >= 0; --i)
{
i.next();
i.set(val);
itr.next();
itr.set(val);
}
}
......@@ -326,11 +334,12 @@ public class Collections
*/
public static Object max(Collection c)
{
Iterator i = c.iterator();
Comparable max = (Comparable) i.next(); // throws NoSuchElementException
while (i.hasNext())
Iterator itr = c.iterator();
Comparable max = (Comparable) itr.next(); // throws NoSuchElementException
int csize = c.size();
for (int i = 1; i < csize; i++)
{
Object o = i.next();
Object o = itr.next();
if (max.compareTo(o) < 0)
{
max = (Comparable) o;
......@@ -352,15 +361,14 @@ public class Collections
*/
public static Object max(Collection c, Comparator order)
{
Iterator i = c.iterator();
Object max = i.next(); // throws NoSuchElementException
while (i.hasNext())
Iterator itr = c.iterator();
Object max = itr.next(); // throws NoSuchElementException
int csize = c.size();
for (int i = 1; i < csize; i++)
{
Object o = i.next();
Object o = itr.next();
if (order.compare(max, o) < 0)
{
max = o;
}
max = o;
}
return max;
}
......@@ -378,15 +386,14 @@ public class Collections
*/
public static Object min(Collection c)
{
Iterator i = c.iterator();
Comparable min = (Comparable) i.next(); // throws NoSuchElementException
while (i.hasNext())
Iterator itr = c.iterator();
Comparable min = (Comparable) itr.next(); // throws NoSuchElementException
int csize = c.size();
for (int i = 1; i < csize; i++)
{
Object o = i.next();
Object o = itr.next();
if (min.compareTo(o) > 0)
{
min = (Comparable) o;
}
min = (Comparable) o;
}
return min;
}
......@@ -404,15 +411,14 @@ public class Collections
*/
public static Object min(Collection c, Comparator order)
{
Iterator i = c.iterator();
Object min = i.next(); // throws NoSuchElementExcception
while (i.hasNext())
Iterator itr = c.iterator();
Object min = itr.next(); // throws NoSuchElementExcception
int csize = c.size();
for (int i = 1; i < csize; i++)
{
Object o = i.next();
Object o = itr.next();
if (order.compare(min, o) > 0)
{
min = o;
}
min = o;
}
return min;
}
......@@ -468,12 +474,16 @@ public class Collections
public static void reverse(List l)
{
ListIterator i1 = l.listIterator();
ListIterator i2 = l.listIterator(l.size());
while (i1.nextIndex() < i2.previousIndex())
int pos1 = 0;
int pos2 = l.size();
ListIterator i2 = l.listIterator(pos2);
while (pos1 < pos2)
{
Object o = i1.next();
i1.set(i2.previous());
i2.set(o);
++pos1;
--pos2;
}
}
......@@ -513,9 +523,24 @@ public class Collections
*/
public static void shuffle(List l)
{
shuffle(l, new Random());
if (defaultRandom == null)
{
synchronized (Collections.class)
{
if (defaultRandom == null)
defaultRandom = new Random();
}
}
shuffle(l, defaultRandom);
}
/** Cache a single Random object for use by shuffle(List). This improves
* performance as well as ensuring that sequential calls to shuffle() will
* not result in the same shuffle order occuring: the resolution of
* System.currentTimeMillis() is not sufficient to guarantee a unique seed.
*/
private static Random defaultRandom = null;
/**
* Shuffle a list according to a given source of randomness. The algorithm
* used iterates backwards over the list, swapping each element with an
......@@ -541,20 +566,21 @@ public class Collections
public static void shuffle(List l, Random r)
{
Object[] a = l.toArray(); // Dump l into an array
ListIterator i = l.listIterator(l.size());
int lsize = l.size();
ListIterator i = l.listIterator(lsize);
// Iterate backwards over l
while (i.hasPrevious())
for (int pos = lsize - 1; pos >= 0; --pos)
{
// Obtain a random position to swap with. nextIndex is used so that the
// Obtain a random position to swap with. pos + 1 is used so that the
// range of the random number includes the current position.
int swap = r.nextInt(i.nextIndex());
int swap = r.nextInt(pos + 1);
// Swap the swapth element of the array with the next element of the
// list.
Object o = a[swap];
a[swap] = a[i.previousIndex()];
a[i.previousIndex()] = o;
a[swap] = a[pos];
a[pos] = o;
// Set the element in the original list accordingly.
i.previous();
......@@ -658,7 +684,7 @@ public class Collections
{
public Set entrySet()
{
return singleton(new BasicMapEntry(key, value));
return singleton(new HashMap.Entry(key, value));
}
};
}
......
This diff is collapsed.
/* HashSet.java -- a class providing a HashMap-backet Set
Copyright (C) 1998, 1999 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
As a special exception, if you link this library with other files to
produce an executable, this library does not by itself cause the
resulting executable to be covered by the GNU General Public License.
This exception does not however invalidate any other reasons why the
executable file might be covered by the GNU General Public License. */
package java.util;
import java.io.IOException;
import java.io.Serializable;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* This class provides a HashMap-backed implementation of the
* Set interface.
*
* Each element in the Set is a key in the backing HashMap; each key
* maps to a static token, denoting that the key does, in fact, exist.
*
* Most operations are O(1), assuming no hash collisions. In the worst
* case (where all hases collide), operations are O(n).
*
* HashSet is a part of the JDK1.2 Collections API.
*
* @author Jon Zeppieri
* @version $Revision: 1.5 $
* @modified $Id: HashSet.java,v 1.5 2000/10/26 10:19:00 bryce Exp $
*/
public class HashSet extends AbstractSet
implements Set, Cloneable, Serializable
{
/** the HashMap which backs this Set */
transient HashMap map;
static final long serialVersionUID = -5024744406713321676L;
/**
* construct a new, empty HashSet whose backing HashMap has the default
* capacity and loadFacor
*/
public HashSet()
{
map = new HashMap();
}
/**
* construct a new, empty HashSet whose backing HashMap has the supplied
* capacity and the default load factor
*
* @param initialCapacity the initial capacity of the backing
* HashMap
*/
public HashSet(int initialCapacity)
{
map = new HashMap(initialCapacity);
}
/**
* construct a new, empty HashSet whose backing HashMap has the supplied
* capacity and load factor
*
* @param initialCapacity the initial capacity of the backing
* HashMap
* @param loadFactor the load factor of the backing HashMap
*/
public HashSet(int initialCapacity, float loadFactor)
{
map = new HashMap(initialCapacity, loadFactor);
}
/**
* construct a new HashSet with the same elements as are in the supplied
* collection (eliminating any duplicates, of course; the backing HashMap
* will have the default capacity and load factor
*
* @param c a collection containing the elements with
* which this set will be initialized
*/
public HashSet(Collection c)
{
map = new HashMap();
addAll(c);
}
/**
* adds the given Object to the set if it is not already in the Set,
* returns true if teh element was added, false otherwise
*
* @param o the Object to add to this Set
*/
public boolean add(Object o)
{
return (map.put(o, Boolean.TRUE) == null);
}
/**
* empties this Set of all elements; this is a fast operation [O(1)]
*/
public void clear()
{
map.clear();
}
/**
* returns a shallow copy of this Set (the Set itself is cloned; its
* elements are not)
*/
public Object clone()
{
HashSet copy = null;
try
{
copy = (HashSet) super.clone();
copy.map = (HashMap) map.clone();
}
catch (CloneNotSupportedException ex)
{
}
return copy;
}
/**
* returns true if the supplied element is in this Set, false otherwise
*
* @param o the Object whose presence in this Set we are testing for
*/
public boolean contains(Object o)
{
return map.containsKey(o);
}
/**
* returns true if this set has no elements in it (size() == 0)
*/
public boolean isEmpty()
{
return map.isEmpty();
}
/**
* returns an Iterator over the elements of this Set; the Iterator allows
* removal of elements
*/
public Iterator iterator()
{
return map.keySet().iterator();
}
/**
* removes the supplied Object from this Set if it is in the Set; returns
* true if an element was removed, false otherwise
*/
public boolean remove(Object o)
{
return (map.remove(o) != null);
}
/**
* returns the number of elements in this Set
*/
public int size()
{
return map.size();
}
/** Serialize this Object in a manner which is binary-compatible with the
* JDK */
private void writeObject(ObjectOutputStream s) throws IOException
{
Iterator it = iterator();
s.writeInt(map.buckets.length);
s.writeFloat(map.loadFactor);
s.writeInt(map.size);
while (it.hasNext())
s.writeObject(it.next());
}
/** Deserialize this Object in a manner which is binary-compatible with
* the JDK */
private void readObject(ObjectInputStream s) throws IOException,
ClassNotFoundException
{
int i, size, capacity;
float loadFactor;
Object element;
capacity = s.readInt();
loadFactor = s.readFloat();
size = s.readInt();
map = new HashMap(capacity, loadFactor);
for (i = 0; i < size; i++)
{
element = s.readObject();
map.put(element, Boolean.TRUE);
}
}
}
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment