Code Search for Developers
 
 
  

ComplementSet.java from DrJava at Krugle


Show ComplementSet.java syntax highlighted

package edu.rice.cs.plt.collect;

import java.util.Set;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Collection;
import edu.rice.cs.plt.iter.FilteredIterator;
import edu.rice.cs.plt.lambda.Predicate;
import edu.rice.cs.plt.lambda.LambdaUtil;

/**
 * The complement of a set {@code excluded} in a domain {@code domain} (alternatively,
 * {@code domain - excluded}), constructed lazily and updated dynamically.
 */
public class ComplementSet<E> extends AbstractSet<E> {
  
  private final Set<? extends E> _domain;
  private final Set<?> _excluded;
  
  public ComplementSet(Set<? extends E> domain, Set<?> excluded) {
    _domain = domain;
    _excluded = excluded;
  }
  
  /** Traversing is linear in the size of {@code domain}. */
  public Iterator<E> iterator() {
    Predicate<Object> filter = LambdaUtil.negate(CollectUtil.containsPredicate(_excluded));
    return new FilteredIterator<E>(_domain.iterator(), filter);
  }
  
  /** Linear in the size of {@code domain}. */
  public int size() {
    int result = 0;
    for (E elt : this) { result++; }
    return result;
  }
  
  /** Linear in the size of {@code domain}. */
  public boolean isEmpty() {
    if (_domain.isEmpty()) { return true; }
    else if (_excluded.isEmpty()) { return false; }
    else if (_domain == _excluded) { return true; }
    else { return _excluded.containsAll(_domain); }
  }
  
  public boolean contains(Object o) {
    return _domain.contains(o) && !(_excluded.contains(o));
  }
  
  public boolean containsAll(Collection<?> objs) {
    if (_domain.containsAll(objs)) {
      for (Object obj : objs) {
        if (_excluded.contains(obj)) { return false; }
      }
      return true;
    }
    else { return false; }
  }
  
}




See more files for this project here

DrJava

DrJava is a lightweight programming environment for Java designed to foster test-driven software development. It includes an intelligent program editor, an interactions pane for evaluating program text, a source level debugger, and a unit testing tool.

Project homepage: http://sourceforge.net/projects/drjava
Programming language(s): Java
License: other

  CollectUtil.java
  ComplementSet.java
  ComposedMap.java
  ConsList.java
  ConsListTest.java
  ConsVisitor.java
  DelegatedSet.java
  ExternallySortedMultiMap.java
  ExternallySortedSet.java
  HashMultiset.java
  HashRelation.java
  ImmutableMapEntry.java
  ImmutableRelation.java
  IntersectionSet.java
  KeyDrivenEntrySet.java
  Multiset.java
  OneToOneHashMap.java
  OneToOneHashMapTest.java
  OneToOneMap.java
  Relation.java
  TotalMap.java
  UnionSet.java
  WeakHashSet.java
  WeakHashSetTest.java
  package-info.java