/** * Adds an element to the list if it is not already present. * <p> * <i>(Violation)</i> * The <code>List</code> interface requires that this method returns * <code>true</code> always. However this class may return <code>false</code> * because of the <code>Set</code> behaviour. * * @param object the object to add * @return true if object was added */ public boolean add(Object object) { // gets initial size final int sizeBefore = size(); // adds element if unique add(size(), object); // compares sizes to detect if collection changed return (sizeBefore != size()); }
/** * Factory method to create a SetList using the supplied list to retain order. * <p> * If the list contains duplicates, these are removed (first indexed one kept). * A <code>HashSet</code> is used for the set behaviour. * * @param list the list to decorate, must not be null * @throws IllegalArgumentException if list is null */ public static SetUniqueList decorate(List list) { if (list == null) { throw new IllegalArgumentException("List must not be null"); } if (list.isEmpty()) { return new SetUniqueList(list, new HashSet()); } else { List temp = new ArrayList(list); list.clear(); SetUniqueList sl = new SetUniqueList(list, new HashSet()); sl.addAll(temp); return sl; } }
/** * Sets the value at the specified index avoiding duplicates. * <p> * The object is set into the specified index. * Afterwards, any previous duplicate is removed * If the object is not already in the list then a normal set occurs. * If it is present, then the old version is removed. * * @param index the index to insert at * @param object the object to set * @return the previous object */ public Object set(int index, Object object) { int pos = indexOf(object); Object removed = super.set(index, object); if (pos != -1 && pos != index) { // the object is already in the unique list // (and it hasn't been swapped with itself) super.remove(pos); // remove the duplicate by index } set.remove(removed); // remove the item deleted by the set set.add(object); // add the new item to the unique set return removed; // return the item deleted by the set }
/** * Adds an element to the end of the list if it is not already present. * <p> * <i>(Violation)</i> * The <code>List</code> interface makes the assumption that the element is * always inserted. This may not happen with this implementation. * * @param coll the collection to add */ public boolean addAll(Collection coll) { return addAll(size(), coll); }
public List subList(int fromIndex, int toIndex) { List superSubList = super.subList(fromIndex, toIndex); Set subSet = createSetBasedOnList(set, superSubList); return new SetUniqueList(superSubList, subSet); }
public void testSetCollections444() { final SetUniqueList lset = new SetUniqueList(new ArrayList(), new HashSet()); // Duplicate element final Integer obj1 = new Integer(1); final Integer obj2 = new Integer(2); lset.add(obj1); lset.add(obj2); lset.set(0, obj1); assertEquals(2, lset.size()); assertSame(obj1, lset.get(0)); assertSame(obj2, lset.get(1)); assertTrue(lset.contains(obj1)); assertTrue(lset.contains(obj2)); }
public void testSet() { final SetUniqueList lset = new SetUniqueList(new ArrayList(), new HashSet()); lset.add(obj1); lset.add(obj2); lset.set(0, obj1); assertEquals(2, lset.size()); assertSame(obj1, lset.get(0)); assertSame(obj2, lset.get(1)); lset.clear(); lset.add(obj1); lset.add(obj2); lset.set(0, obj2); assertEquals(1, lset.size()); assertSame(obj2, lset.get(0)); lset.clear(); lset.add(obj1); lset.add(obj2); lset.set(0, obj3); assertEquals(2, lset.size()); assertSame(obj3, lset.get(0)); assertSame(obj2, lset.get(1)); lset.clear(); lset.add(obj1); lset.add(obj2); lset.set(1, obj1); assertEquals(1, lset.size());
public void testListIterator() { final SetUniqueList lset = new SetUniqueList(new ArrayList(), new HashSet()); final Object obj1 = new Integer(1); final Object obj2 = new Integer(2); lset.add(obj1); lset.add(obj2); // Attempts to add a duplicate object for (final ListIterator it = lset.listIterator(); it.hasNext();) { it.next(); if (!it.hasNext()) { it.add(obj1); break; } } assertEquals("Duplicate element was added", 2, lset.size()); }
public void testAdd() { final SetUniqueList lset = new SetUniqueList(new ArrayList(), new HashSet()); // Duplicate element final Object obj = new Integer(1); lset.add(obj); lset.add(obj); assertEquals("Duplicate element was added.", 1, lset.size()); // Unique element lset.add(new Integer(2)); assertEquals("Unique element was not added.", 2, lset.size()); }
public void testAddAll() { final SetUniqueList lset = new SetUniqueList(new ArrayList(), new HashSet()); lset.addAll( Arrays.asList(new Integer[] { new Integer(1), new Integer(1)})); assertEquals("Duplicate element was added.", 1, lset.size()); }
public void testSetDownwardsInList() { /* * Checks the following semantics * [a,b] * set(0,b): [b]->a * So UniqList contains [b] and a is returned */ ArrayList l = new ArrayList(); HashSet s = new HashSet(); final SetUniqueList ul = new SetUniqueList(l, s); Object a = new Object(); Object b = new Object(); ul.add(a); ul.add(b); assertEquals(a, l.get(0)); assertEquals(b, l.get(1)); assertTrue(s.contains(a)); assertTrue(s.contains(b)); assertEquals(a, ul.set(0, b)); assertEquals(1, s.size()); assertEquals(1, l.size()); assertEquals(b, l.get(0)); assertTrue(s.contains(b)); assertFalse(s.contains(a)); }
public void testCollections304() { List list = new LinkedList(); SetUniqueList decoratedList = SetUniqueList.decorate(list); String s1 = "Apple"; String s2 = "Lemon"; String s3 = "Orange"; String s4 = "Strawberry"; decoratedList.add(s1); decoratedList.add(s2); decoratedList.add(s3); assertEquals(3, decoratedList.size()); decoratedList.set(1, s4); assertEquals(3, decoratedList.size()); decoratedList.add(1, s4); assertEquals(3, decoratedList.size()); decoratedList.add(1, s2); assertEquals(4, decoratedList.size()); }
public void testFactory() { Integer[] array = new Integer[] {new Integer(1), new Integer(2), new Integer(1)}; ArrayList list = new ArrayList(Arrays.asList(array)); final SetUniqueList lset = SetUniqueList.decorate(list); assertEquals("Duplicate element was added.", 2, lset.size()); assertEquals(new Integer(1), lset.get(0)); assertEquals(new Integer(2), lset.get(1)); assertEquals(new Integer(1), list.get(0)); assertEquals(new Integer(2), list.get(1)); }
public List makeEmptyList() { return new SetUniqueList(new ArrayList(), new HashSet()); }
public void testUniqueListReInsert() { List l = SetUniqueList.decorate(new LinkedList()); l.add(new Object()); l.add(new Object()); Object a = l.get(0); // duplicate is removed l.set(0, l.get(1)); assertEquals(1, l.size()); // old object is added back in l.add(1, a); assertEquals(2, l.size()); }
/** * Adds an element to the end of the list if it is not already present. * <p> * <i>(Violation)</i> * The <code>List</code> interface makes the assumption that the element is * always inserted. This may not happen with this implementation. * * @param coll the collection to add */ public boolean addAll(Collection coll) { return addAll(size(), coll); }
final SetUniqueList ul = new SetUniqueList(l, s); Object c = new Object(); ul.add(a); ul.add(b); ul.add(c); assertEquals(a, l.get(0)); assertEquals(b, l.get(1)); assertTrue(s.contains(c)); assertEquals(a, ul.set(0, b)); assertEquals(2, s.size()); assertEquals(2, l.size());
public List subList(int fromIndex, int toIndex) { List superSubList = super.subList(fromIndex, toIndex); Set subSet = createSetBasedOnList(set, superSubList); return new SetUniqueList(superSubList, subSet); }
public void testIntCollectionAddAll() { // make a SetUniqueList with one element List list = new SetUniqueList(new ArrayList(), new HashSet()); final Integer existingElement = new Integer(1); list.add(existingElement); // add two new unique elements at index 0 final Integer firstNewElement = new Integer(2); final Integer secondNewElement = new Integer(3); collection = Arrays.asList(new Integer[] { firstNewElement, secondNewElement }); list.addAll(0, collection); assertEquals("Unique elements should be added.", 3, list.size()); assertEquals("First new element should be at index 0", firstNewElement, list.get(0)); assertEquals("Second new element should be at index 1", secondNewElement, list.get(1)); assertEquals("Existing element should shift to index 2", existingElement, list.get(2)); // add a duplicate element and a unique element at index 0 final Integer thirdNewElement = new Integer(4); collection = Arrays.asList(new Integer[] { existingElement, thirdNewElement }); list.addAll(0, collection); assertEquals("Duplicate element should not be added, unique element should be added.", 4, list.size()); assertEquals("Third new element should be at index 0", thirdNewElement, list.get(0)); }