/** * IDList.java 1.01 97/15/09 Merlin Hughes * * Copyright (c) 1997 Merlin Hughes. All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * for non-commercial purposes and without fee is hereby granted * provided that this copyright notice appears in all copies. * * http://merlin.org/ merlin@merlin.org */ package org.merlin.step.dec; import java.io.*; import java.util.*; public class IDList implements Serializable, Cloneable { protected Vector list = new Vector (); protected Vector ids = new Vector (); public synchronized Object addElement (Object element) { Object id = new Integer (System.identityHashCode (element)); addElementWithID (id, element); return id; } public synchronized void addElementWithID (Object id, Object element) { ++ updateCount; ids.addElement (id); list.addElement (element); } public synchronized Object replaceElement (Object oldID, Object element) { ++ updateCount; int idx = ids.indexOf (oldID); if (idx >= 0) { ids.removeElementAt (idx); list.removeElementAt (idx); return addElement (element); } else { return null; } } public synchronized boolean replaceElementWithID (Object oldID, Object id, Object element) { ++ updateCount; int idx = ids.indexOf (oldID); if (idx >= 0) { ids.removeElementAt (idx); list.removeElementAt (idx); ids.addElement (id); list.addElement (element); return true; } else { return false; } } public synchronized Object getIDOfElement (Object element) { int idx = list.indexOf (element); return (idx >= 0) ? ids.elementAt (idx) : null; } public synchronized Enumeration elements () { return ((Vector) list.clone ()).elements (); } public synchronized Object clone () { try { return super.clone (); } catch (CloneNotSupportedException ex) { return null; } } int updateCount; public int getUpdateCount () { return updateCount; } public int size () { return list.size (); } public synchronized Object removeFirstElement () { if (list.size () > 0) { Object id = ids.elementAt (0); list.removeElementAt (0); ids.removeElementAt (0); return id; } else { return null; } } public synchronized boolean removeElement (Object id) { int idx = ids.indexOf (id); if (idx == -1) { return false; } else { list.removeElementAt (idx); ids.removeElementAt (idx); return true; } } }