@Override public boolean addAll(Collection<? extends E> c) { boolean modified = false; for(E elem : c) if(add(elem)) modified = true; return modified; }
@Override @SuppressWarnings("unchecked") public boolean contains(Object o) { if(o==null) return false; // TODO: How can we check if the passed-in object is of an unrelated, unexpected class // TODO: without passing around Class objects? // TODO: with equals - just like ArraySet. return binarySearch((E)o) >= 0; }
@Override public boolean containsAll(Collection<?> c) { for(Object o : c) if(!contains(o)) return false; return true; }
@Override @SuppressWarnings("unchecked") public boolean remove(Object o) { int size = elements.size(); if(size==0) return false; // Shortcut for removing last element E lastElem = elements.get(size-1); // TODO: How can we check if the passed-in object is of an unrelated, unexpected class // TODO: without passing around Class objects? if(compare(lastElem, (E)o)==0) { elements.remove(size-1); return true; } else { if(contains(o)) throw new UnsupportedOperationException("May only remove the last element."); return false; } }
@Override public boolean add(E e) { int size = elements.size(); if(size==0) { elements.add(e); return true; } else { // Shortcut for adding last element E last = elements.get(size-1); int diff = compare(e, last); if(diff>0) { elements.add(e); return true; } else if(diff==0) { // Already in set return false; } else { int index = binarySearch(e); if(index>=0) { // Already in set return false; } else { throw new UnsupportedOperationException("May only add the last element."); } } } }
@Override public boolean removeAll(Collection<?> c) { boolean modified = false; for(Object o : c) if(remove(o)) modified = true; return modified; }
public ArraySortedSet(Collection<? extends E> c) { this.comparator = null; this.elements = new ArrayList<E>(c.size()); addAll(c); }