package edu.cornell.cs.cs2110;

/**
 * Implements a set of limited size.
 */
public class SmallSet {
	private int _size = 0;
	public static final int MAX = 10;
	private Object _items[] = new Object[MAX];
	
	/**
	 * Returns the number of elements in this set.
	 * @return
	 * 		the number of elements in this set.
	 */
	public int size() {
		return _size;
	}

	/**
	 * Adds an item to this set.
	 * @param o
	 * 		Object to be added.
	 * @throws SmallSetFullException
	 * 		if this set is at maximum capacity.
	 */
	public void add(Object o) throws SmallSetFullException {
		if (!inSet(o)) {
			if (_size >= MAX) {
				throw new SmallSetFullException();
			}
			_items[_size] = o;
			++_size;
		}
	}

	/**
	 * Returns whether the given Object is in this set.
	 * @param o
	 * 		Object to check whether in this set.
	 * @return
	 * 		<code>true</code> if the given Object is
	 * 		in this set, <code>false</code> otherwise.
	 */
	private boolean inSet(Object o) {
		for (int i=0; i < MAX; i++) {
			if (_items[i] == o) {
				return true;
			}
		}
		
		return false;
	}
}
