All Downloads are FREE. Search and download functionalities are using the official Maven repository.

soot.util.IterableSet Maven / Gradle / Ivy

package soot.util;

/*-
 * #%L
 * Soot - a J*va Optimization Framework
 * %%
 * Copyright (C) 2002 Sable Research Group
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.util.Collection;
import java.util.Set;

public class IterableSet extends HashChain implements Set {
  public IterableSet(Collection c) {
    super();
    addAll(c);
  }

  public IterableSet() {
    super();
  }

  public boolean add(T o) {
    if (o == null) {
      throw new IllegalArgumentException("Cannot add \"null\" to an IterableSet.");
    }

    if (contains(o)) {
      return false;
    }

    return super.add(o);
  }

  public boolean remove(Object o) {
    if ((o == null) || (contains(o) == false)) {
      return false;
    }

    return super.remove(o);
  }

  public boolean equals(Object o) {
    if (o == null) {
      return false;
    }

    if (this == o) {
      return true;
    }

    if ((o instanceof IterableSet) == false) {
      return false;
    }

    @SuppressWarnings("unchecked")
    IterableSet other = (IterableSet) o;

    if (size() != other.size()) {
      return false;
    }

    for (T t : this) {
      if (!other.contains(t)) {
        return false;
      }
    }

    return true;
  }

  @Override
  public int hashCode() {
    int code = 23 * size();
    for (T t : this) {
      // use addition here to have hash code independent of order
      code += t.hashCode();
    }
    return code;
  }

  public Object clone() {
    IterableSet s = new IterableSet();
    s.addAll(this);
    return s;
  }

  public boolean isSubsetOf(IterableSet other) {
    if (other == null) {
      throw new IllegalArgumentException("Cannot set compare an IterableSet with \"null\".");
    }

    if (size() > other.size()) {
      return false;
    }

    for (T t : this) {
      if (!other.contains(t)) {
        return false;
      }
    }

    return true;
  }

  public boolean isSupersetOf(IterableSet other) {
    if (other == null) {
      throw new IllegalArgumentException("Cannot set compare an IterableSet with \"null\".");
    }

    if (size() < other.size()) {
      return false;
    }

    for (T t : other) {
      if (!contains(t)) {
        return false;
      }
    }

    return true;
  }

  public boolean isStrictSubsetOf(IterableSet other) {
    if (other == null) {
      throw new IllegalArgumentException("Cannot set compare an IterableSet with \"null\".");
    }

    if (size() >= other.size()) {
      return false;
    }

    return isSubsetOf(other);
  }

  public boolean isStrictSupersetOf(IterableSet other) {
    if (other == null) {
      throw new IllegalArgumentException("Cannot set compare an IterableSet with \"null\".");
    }

    if (size() <= other.size()) {
      return false;
    }

    return isSupersetOf(other);
  }

  public boolean intersects(IterableSet other) {
    if (other == null) {
      throw new IllegalArgumentException("Cannot set intersect an IterableSet with \"null\".");
    }

    if (other.size() < size()) {
      for (T t : other) {
        if (contains(t)) {
          return true;
        }
      }
    } else {
      for (T t : this) {
        if (other.contains(t)) {
          return true;
        }
      }
    }

    return false;
  }

  public IterableSet intersection(IterableSet other) {
    if (other == null) {
      throw new IllegalArgumentException("Cannot set intersect an IterableSet with \"null\".");
    }

    IterableSet c = new IterableSet();

    if (other.size() < size()) {
      for (T t : other) {
        if (contains(t)) {
          c.add(t);
        }
      }
    } else {
      for (T t : this) {
        if (other.contains(t)) {
          c.add(t);
        }
      }
    }
    return c;
  }

  public IterableSet union(IterableSet other) {
    if (other == null) {
      throw new IllegalArgumentException("Cannot set union an IterableSet with \"null\".");
    }

    IterableSet c = new IterableSet();

    c.addAll(this);
    c.addAll(other);

    return c;
  }

  public String toString() {
    StringBuffer b = new StringBuffer();

    for (T t : this) {
      b.append(t.toString());
      b.append("\n");
    }

    return b.toString();
  }

  public UnmodifiableIterableSet asUnmodifiable() {
    return new UnmodifiableIterableSet(this);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy