Apache Nutch is an extensible and scalable web crawler
Remove unused utility whose unit test fails.
Douglass Cutting1f1648985541
diff --git a/src/java/org/apache/nutch/util/SoftHashMap.java b/src/java/org/apache/nutch/util/SoftHashMap.javadeleted file mode 100644index f15ed7962..000000000--- a/src/java/org/apache/nutch/util/SoftHashMap.java+++ /dev/null@@ -1,222 +0,0 @@-/**- * Copyright 2005 The Apache Software Foundation- *- * Licensed under the Apache License, Version 2.0 (the "License");- * you may not use this file except in compliance with the License.- * You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- */--package org.apache.nutch.util;--import java.lang.ref.SoftReference;-import java.util.AbstractMap;-import java.util.ArrayList;-import java.util.Collections;-import java.util.Collection;-import java.util.HashMap;-import java.util.Map;-import java.util.Set;--/**- * A Map which uses {@link SoftReference}s to keep track of values.- *- * <p>- *- * This class is suitable for use as a best-effort caching mechanism.- * Under the hood, it is simply a {@link HashMap}, which wraps it's- * values with SoftReference</code>s. Unlike <code>HashMap</code>,- * however, null values should not be used- they will be- * indistinguisable from values which have been garbage collected.- *- * <p>- *- * Values should implement {@link SoftHashMap.FinalizationNotifier}, so keys- * can be cleaned up as values are garbage collected. If values are added- * which do not support this interface, the associated keys will continue to be- * referenced by this data structure until explicitly removed.- *- * <p>- *- * Expiration policy is determined by the JVM's implementation of- * <code>SoftReference</code>.- */-public class SoftHashMap extends AbstractMap implements Map {- Map hashMap;- ArrayList keysToDelete;-- /**- * An interface for Object which accept notification when an another- * Object is finalized.- */- public interface FinalizationListener {- /**- * This method will be called when a {@link- * SoftHashMap.FinalizationNotifier} this Object is registered with is- * being finalized. <em>Note</em> that this method is not passed a- * reference to the Object which is undergoing finalization, since creating- * a new reference to that object may block finalization.- */- public void finalizationOccurring();- }-- /**- * An interface for a Objects which can notify an object when they are- * finalized. Upon finalization, Objects which implement this interface will- * call the <code>finalizationOccurring</code> method of all {@link- * SoftHashMap.FinalizationListener}s that have registered with it.- */- public interface FinalizationNotifier {- /**- * Registers a {@link SoftHashMap.FinalizationListener} for this object.- */- public void addFinalizationListener(FinalizationListener listener);- }-- private class MyFinalizationListener implements FinalizationListener {- Object key;-- MyFinalizationListener(Object key, FinalizationNotifier value) {- this.key= key;- value.addFinalizationListener(this);- }-- public void finalizationOccurring() {- SoftHashMap.this.queueKeyForDeletion(key);- }-- }-- public SoftHashMap() {- hashMap= Collections.synchronizedMap(new HashMap());- keysToDelete= new ArrayList(128);- }-- public void clear() {- hashMap.clear();- }-- // queues a key for deletion- called by FinalizationListener which- // is listening for a value's expiration- protected void queueKeyForDeletion(Object key) {- synchronized (keysToDelete) {- purgeQueuedKeys();- keysToDelete.add(key);- }- }-- // purges keys listed in keysToDelete from map- protected void purgeQueuedKeys() {- synchronized (keysToDelete) {- for (int i= keysToDelete.size() - 1; i >= 0 ; i--) {- remove(keysToDelete.get(i));- }- keysToDelete.clear();- }- }-- /**- * Returns true if this map contains a mapping for the specified key.- *- * <em>Note</em> that this method can return true if the value has- * been garbage collected, but the key has not been cleared.- * Additionally, the finalizer may invalidate the result of this- * operation before a subsequent <code>get()</code> can be issued.- */- public boolean containsKey(Object key) {- return hashMap.containsKey(key);- }-- /**- * Not Implemented- *- * <em>Note</em> that the finalizer may invalidate the result an- * implementation would return.- */- public boolean containsValue(Object value)- throws UnsupportedOperationException {- throw new UnsupportedOperationException("SoftHashMap.containsValue is "- + "not implemented");- }-- /**- * Not Implemented- */- public Set entrySet() throws UnsupportedOperationException {- throw new- UnsupportedOperationException("SoftHashMap.entrySet() not implemented");- }-- public Object get(Object key) {- SoftReference ref= (SoftReference) hashMap.get(key);- if (ref == null) {- return null;- }- return ref.get();- }-- public boolean isEmpty() {- purgeQueuedKeys();- return hashMap.isEmpty();- }-- public Set keySet() {- purgeQueuedKeys();- return hashMap.keySet();- }--- /**- * Associates the specified value with the specified key in this- * map. If the map previously contained a mapping for this key, the- * old value is replaced.- *- * <p>- *- * <em>Note</em>: <code>value<code> must implemnt FinalizationNotifier- * for keys to be freed properly when values are garbage collected.- */- public Object put(Object key, Object value) {- purgeQueuedKeys();- SoftReference oldRef=- (SoftReference) hashMap.put(key, new SoftReference(value));-- try {- new MyFinalizationListener(key, (FinalizationNotifier) value);- } catch (ClassCastException e) {- // fixme: throw an exception? warn?- }-- if (oldRef == null)- return null;- return oldRef.get();- }-- public Object remove(Object key) {- SoftReference ref= (SoftReference) hashMap.remove(key);- if (ref == null)- return null;- return ref.get();- }-- public int size() {- purgeQueuedKeys();- return hashMap.size();- }-- /**- * Not Implemented- */- public Collection values() throws UnsupportedOperationException {- throw new- UnsupportedOperationException("SoftHashMap: values() not implemnted");- }--}diff --git a/src/test/org/apache/nutch/util/TestSoftHashMap.java b/src/test/org/apache/nutch/util/TestSoftHashMap.javadeleted file mode 100644index 06d78b8a8..000000000--- a/src/test/org/apache/nutch/util/TestSoftHashMap.java+++ /dev/null@@ -1,223 +0,0 @@-/**- * Copyright 2005 The Apache Software Foundation- *- * Licensed under the Apache License, Version 2.0 (the "License");- * you may not use this file except in compliance with the License.- * You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- */--package org.apache.nutch.util;--import junit.framework.TestCase;--import java.util.ArrayList;-import java.util.Iterator;--/**- * Unit tests for SoftHashMap.- */-public class TestSoftHashMap extends TestCase {-- // set to true to get flood of status messages on stderr- useful- // for seeing when JVM is collecting everything.- private static final boolean verbose= false;-- // 1kB for int[]- private static final int TEST_VALUE_ARRAY_SIZE= 1024 / 4;-- private static final int BASIC_OPS_SIZE= 10;-- private boolean keyHasBeenFinalized;- private boolean valHasBeenFinalized;-- private class TestKey {- Integer key;- boolean notify;-- TestKey(Integer key, boolean notify) {- this.key= key;- this.notify= notify;- }-- protected void finalize() {- if (notify)- TestSoftHashMap.this.keyFinalized(key);- }-- public int hashCode() {- return key.hashCode();- }-- public boolean equals(Object o) {- if (o == null)- return false;- if ( !(o instanceof TestKey) )- return false;- TestKey other= (TestKey) o;- return (this.key.equals(other.key));- }-- public String toString() {- return "Key:"+key;- }-- }-- private class TestValue implements SoftHashMap.FinalizationNotifier {- int[] val;- boolean notify;- ArrayList finalizationListeners;-- TestValue(int key, boolean notify) {- this.val= new int[TEST_VALUE_ARRAY_SIZE];- this.val[0]= key;- this.notify= notify;- this.finalizationListeners= new ArrayList();- }-- public void addFinalizationListener(SoftHashMap.FinalizationListener- listener) {- finalizationListeners.add(listener);- }-- protected void finalize() {- if (notify)- TestSoftHashMap.this.valFinalized(val[0]);- for (Iterator iter= finalizationListeners.iterator();- iter.hasNext() ; ) {- SoftHashMap.FinalizationListener l=- (SoftHashMap.FinalizationListener) iter.next();- l.finalizationOccurring();- }- }-- boolean isMyKey(int key) {- return key == val[0];- }-- public String toString() {- return "Val:"+val[0];- }-- }-- public TestSoftHashMap(String name) {- super(name);- }-- public void testBasicOps() {- SoftHashMap shm= new SoftHashMap();-- // cache keys & vals so they don't go away- TestKey[] keys= new TestKey[BASIC_OPS_SIZE];- TestValue[] vals= new TestValue[BASIC_OPS_SIZE];-- for (int i= 0; i < BASIC_OPS_SIZE; i++) {- keys[i]= new TestKey(new Integer(i), false);- vals[i]= new TestValue(i, false);- shm.put(keys[i], vals[i]);- }-- for (int i= 0; i < BASIC_OPS_SIZE; i++) {- TestValue v= (TestValue) shm.get(new TestKey(new Integer(i), false));- assertTrue("got back null, expecting value! (key= "+i+")", v != null);- assertTrue("got back wrong value (isMyKey())!", v.isMyKey(i));- assertTrue("got back wrong value (!=)!", v == vals[i]);- assertTrue("contains key doesn't have " + i,- shm.containsKey(new TestKey(new Integer(i), false)));- assertTrue("isEmpty returns true when it shouldn't",- !shm.isEmpty());- }-- Object removed= shm.remove(- new TestKey(new Integer(BASIC_OPS_SIZE - 1), false));- if (verbose)- System.err.println("removed: " + removed);- TestValue v= (TestValue)- shm.get(new TestKey(new Integer(BASIC_OPS_SIZE), false));- assertTrue("got back val after delete!", v == null);-- int size= shm.size();- assertTrue("got bad value from size(); returned " + size,- size == (BASIC_OPS_SIZE - 1) );-- shm.clear();- assertTrue("isEmpty returns false when it shouldn't",- shm.isEmpty());- }-- public void testExpiry() {- if (verbose)- System.err.println("entering testExpiry()");- SoftHashMap shm= new SoftHashMap();-- valHasBeenFinalized= false;- keyHasBeenFinalized= false;- int i= 0;-- try {- while (!valHasBeenFinalized) {- if (verbose)- System.err.println("(!v) trying to put " + i);- shm.put(new TestKey(new Integer(i), true), new TestValue(i, true));- i++;- if (verbose)- System.err.println("after adding " + i- + " items, size is " + shm.size());- }- while (!keyHasBeenFinalized) {- if (verbose)- System.err.println("(!k) trying to put " + i);- shm.put(new TestKey(new Integer(i), true), new TestValue(i, true));- i++;- }-- // sleep and busy loop to see if JVM goes on collecting stuff...- if (verbose)- System.err.println("sleeping... ");- Thread.sleep(20 * 1000);- if (verbose)- System.err.println("busy looping...");- int j;- for (j= 0; j < 2000000; j++) {- i+= j;- }- if (verbose)- System.err.println("done, j=" + j);-- } catch (Exception e) {- System.err.println("caught exception");- e.printStackTrace();- } finally {- if (verbose)- System.err.println("out of put loops");- }-- }-- void keyFinalized(Integer key) {- if (verbose)- System.err.println("notified of finalized key: " + key);- keyHasBeenFinalized= true;- }-- void valFinalized(int key) {- if (verbose)- System.err.println("notified of finalized value for: " + key);- valHasBeenFinalized= true;- }-- public static final void main(String[] a) {- TestSoftHashMap t= new TestSoftHashMap("test");- t.testExpiry();- }--}