/** * Return a fresh weak set. */ public static WeakSet createWeakSet() { WeakSet ret = new WeakSet(); if(DEBUG) addCollection(ret); return ret; }
/** * Return the size of the set. * @return The size of the set. */ public int size() { expungeStaleEntries(); return set.size(); }
/** * Convert the set to an array. */ // Overriden, because AbstractCollection implementation relies on constant size. public Object[] toArray() { Object[] result = new Object[size()]; int i = 0; Iterator it = iterator(); for(; it.hasNext(); i++) result[i] = it.next(); // Reallocate array, when some elements have been garbage collected (shouldn't happen often). if(i<result.length) { Object[] result2 = new Object[i]; System.arraycopy(result, 0, result2, 0, i); result = result2; } return result; }
/** * Create a blocksworld panel. * @param table The table. * @param imaginary Flag indicating that its not the real world. */ public BlocksworldPanel(Table table, boolean imaginary) { this.table = table; this.imaginary = imaginary; this.blocksize = 100; this.blocks = new WeakSet<Block>(); // Update gui when table changes. this.pcl = new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent pce) { // Update gui. BlocksworldPanel.this.invalidate(); BlocksworldPanel.this.repaint(); // Add listener for new blocks. BlocksworldPanel.this.observeNewBlocks(); } }; table.addPropertyChangeListener(pcl); // Add listener to blocks. observeNewBlocks(); }
/** * Returns true if this set contains no elements. * @return true if this set contains no elements. */ public boolean isEmpty() { expungeStaleEntries(); return set.isEmpty(); }
/** * Convert the set to an array. */ // Overriden, because AbstractCollection implementation relies on constant size. public Object[] toArray(Object result[]) { int size = size(); if(result.length<size) result = (Object[])java.lang.reflect.Array.newInstance(result.getClass().getComponentType(), size); int i = 0; Iterator it = iterator(); for(; it.hasNext(); i++) result[i] = it.next(); // Reallocate array, when some elements have been garbage collected (shouldn't happen often). if(i<result.length) { Object[] result2 = (Object[])java.lang.reflect.Array.newInstance(result.getClass().getComponentType(), i); System.arraycopy(result, 0, result2, 0, i); result = result2; } return result; } /**
/** * Create a blocksworld panel. * @param table The table. * @param imaginary Flag indicating that its not the real world. */ public BlocksworldPanel(Table table, boolean imaginary) { this.table = table; this.imaginary = imaginary; this.blocksize = 100; this.blocks = new WeakSet<Block>(); // Update gui when table changes. this.pcl = new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent pce) { // Update gui. BlocksworldPanel.this.invalidate(); BlocksworldPanel.this.repaint(); // Add listener for new blocks. BlocksworldPanel.this.observeNewBlocks(); } }; table.addPropertyChangeListener(pcl); // Add listener to blocks. observeNewBlocks(); }
/** * Returns a shallow copy of this WeakSet instance: the elements themselves are not cloned. * @return A shallow copy of this set. */ public Object clone() { expungeStaleEntries(); try { return super.clone(); } catch(CloneNotSupportedException e) { throw new InternalError(); } }
/** * Removes the given element from this set if it is present. * @param obj Object to be removed from this set, if present. * @return true if the set contained the specified element. */ public boolean remove(final Object obj) { if(obj==null) throw new IllegalArgumentException("Must not be null."); expungeStaleEntries(); return set.remove(new WeakObject(obj)); }
expungeStaleEntries(); return new Iterator()
/** * Add an element to the set. * @param obj Element to add to the set. * @return True if the element was added. */ public boolean add(final Object obj) { if(obj==null) throw new IllegalArgumentException("Must not be null."); expungeStaleEntries(); return set.add(new WeakObject(obj, queue)); }
/** * Returns true if this set contains the specified element. * @param obj Element whose presence in this set is to be tested. * @return true if this set contains the specified element. */ public boolean contains(final Object obj) { if(obj==null) throw new IllegalArgumentException("Must not be null."); expungeStaleEntries(); return set.contains(new WeakObject(obj)); }
/** * Perform special handling on serialization. */ protected Object writeReplace() throws ObjectStreamException { // Extract weak references as they are not serializable. expungeStaleEntries(); this.serialized_set = SCollection.createHashSet(); for(Iterator it=set.iterator(); it.hasNext(); ) { Object next = ((WeakObject)it.next()).get(); if(next!=null) serialized_set.add(next); } return this; }