public class BoundedBitSet extends BitSet {
  public BoundedBitSet(int capacity) {
    super(capacity);
  }

  private boolean checkIndex(int e) {
    int index = e / INT_BITS;
    return index < 1 + capacity / INT_BITS;
  }

  public void add(Integer e) {
    if (checkIndex(e)) {
      super.add(e);
    } else {
      System.err.println("Attempt to insert " + e + " in set of capacity " +
          capacity);
    }
  }

  public void del(Integer e) {
    if (checkIndex(e)) {
      super.del(e);
    } else {
      System.err.println("Attempt to remove " + e + " from set of capacity " +
          capacity);
    }
  }

  public boolean contains(Integer e) {
    if (checkIndex(e)) {
      return super.contains(e);
    } else {
      System.err.println("Attempt to access " + e + " in set of capacity " +
          capacity);
      return false;
    }
  }

  public static void fill(Set<Integer> set, int b, int e, int s) {
    for (int i = b; i < e; i += s) {
      set.add(i);
    }
  }

  public static void main(String args[]) {
    Set<Integer> s = new BoundedBitSet(15);
    s.add(14);
    System.out.println("Contains 14? " + s.contains(14));
    s.add(15);
    System.out.println("Contains 15? " + s.contains(15));
    s.add(16);
    System.out.println("Contains 16? " + s.contains(16));
    s.add(31);
    System.out.println("Contains 31? " + s.contains(31));
    s.add(32);
    System.out.println("Contains 32? " + s.contains(32));
  }

}
