/** * 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; } }
/** * 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; } }
public List subList(int fromIndex, int toIndex) { List superSubList = super.subList(fromIndex, toIndex); Set subSet = createSetBasedOnList(set, superSubList); return new SetUniqueList(superSubList, subSet); }
public List subList(int fromIndex, int toIndex) { List superSubList = super.subList(fromIndex, toIndex); Set subSet = createSetBasedOnList(set, superSubList); return new SetUniqueList(superSubList, subSet); }
public List makeEmptyList() { return new SetUniqueList(new ArrayList(), new HashSet()); }
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 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)); }
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 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)); }
final SetUniqueList ul = new SetUniqueList(l, s);
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)); }
final SetUniqueList ul = new SetUniqueList(l, s);
public void testSet() { final SetUniqueList lset = new SetUniqueList(new ArrayList(), new HashSet());
public List subList(int fromIndex, int toIndex) { return new SetUniqueList(super.subList(fromIndex, toIndex), set); }
public List subList(int fromIndex, int toIndex) { return new SetUniqueList(super.subList(fromIndex, toIndex), set); }
/** * 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; } }
public List subList(int fromIndex, int toIndex) { List superSubList = super.subList(fromIndex, toIndex); Set subSet = createSetBasedOnList(set, superSubList); return new SetUniqueList(superSubList, subSet); }
public List subList(int fromIndex, int toIndex) { List superSubList = super.subList(fromIndex, toIndex); Set subSet = createSetBasedOnList(set, superSubList); return new SetUniqueList(superSubList, subSet); }
public List subList(int fromIndex, int toIndex) { List superSubList = super.subList(fromIndex, toIndex); Set subSet = createSetBasedOnList(set, superSubList); return new SetUniqueList(superSubList, subSet); }