[concurrency-interest] ConcurrentHashmap.putIfAbsent oddities

Gregg Wonderly gergg at cox.net
Mon Dec 11 09:29:32 EST 2006


Hanson Char wrote:
> Can you provide the implementation of the equals() and hashCode() 
> methods of LoaderKey ?  The usual suspects.

     private class LoaderKey extends WeakReference {
		private final URL[] urls;
		private final boolean nullParent;
		private final int hashValue;

		public String toString() {
			String str = "LoaderKey: (parentnull="+nullParent+") "+Arrays.toString( urls );
			if( logger.isLoggable(Level.FINER) )
				str += ": loader="+get();
			return str;
		}

		public LoaderKey(URL[] urls, ClassLoader parent) {
			super(parent, refQueue);
			this.urls = urls;
			nullParent = (parent == null);

			int h = nullParent ? 0 : parent.hashCode();
			for (int i = 0; i < urls.length; i++) {
				h ^= urls[i].hashCode();
			}
			hashValue = h;
		}

		public int hashCode() {
			return hashValue;
		}

		public boolean equals(Object obj) {
			if (obj == this) {
				return true;
			} else if (!(obj instanceof LoaderKey)) {
				return false;
			}
			LoaderKey other = (LoaderKey) obj;
			ClassLoader parent = (ClassLoader) get();
			return (nullParent ? other.nullParent
				 : ( parent != null &&
						parent == other.get() ) )
						&& Arrays.equals(urls, other.urls);
		}
     }


More information about the Concurrency-interest mailing list