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

net.sf.gluebooster.java.booster.basic.container.BoostedHashSet Maven / Gradle / Ivy

Go to download

Basic classes to support the development of applications. There should be as few dependencies on other frameworks as possible.

The newest version!
package net.sf.gluebooster.java.booster.basic.container;

import java.awt.event.ActionEvent;
import java.util.Collection;
import java.util.HashSet;

import net.sf.gluebooster.java.booster.basic.transformation.CallableByFactory;
import net.sf.gluebooster.java.booster.essentials.eventsCommands.Callable;
import net.sf.gluebooster.java.booster.essentials.eventsCommands.MultiListener;
import net.sf.gluebooster.java.booster.essentials.math.Condition;

/**
 * A hash set with more functionality.
 * 
 * @author CBauer
 * 
 * @param 
 *            the class of the elements of the hash set.
 * @defaultParamText elementClass the class of the elements
 */
public class BoostedHashSet extends HashSet implements
		Callable {

	private static final long serialVersionUID = 3162544981905924703L;

	/**
	 * The factory to create the object instances.
	 */
	private Callable factory;

	/**
	 * Listeners that should be informed.
	 */
	// private EventMulticaster listeners = new EventMulticaster(this);
	private MultiListener listeners = new MultiListener();

	/**
	 * Are the listeners to be informed when this set becomes empty.
	 */
	private boolean informListenerWhenEmpty = false;

	/**
	 * Should an exception be thrown if the last element is removed.
	 */
	private boolean throwExceptionWhenEmptied = false;

	public BoostedHashSet(Class elementClass) {
		init(elementClass);
	}

	public BoostedHashSet() {
		init(null);
	}

	/**
	 * Constructor with elements.
	 * 
	 * @param elements
	 *            the initial elements
	 */
	public BoostedHashSet(Collection elements) {
		super(elements);
		init(null);
	}

	/**
	 * Invokes the parent constructor.
	 * 
	 * @param initialCapacity
	 *            see the parent constructor
	 * @param loadFactor
	 *            see the parent constructor
	 */
	public BoostedHashSet(int initialCapacity, float loadFactor) {
		super(initialCapacity, loadFactor);
		init(null);
	}

	/**
	 * Invokes the parent constructor.
	 * 
	 * @param initialCapacity
	 *            see the parent constructor
	 */
	public BoostedHashSet(int initialCapacity) {
		super(initialCapacity);
		init(null);
	}

	/**
	 * Initializes the factory to create elements of the elementClass.
	 * 
	 */
	private void init(Class elementClass) {
		if (elementClass != null)
			factory = CallableByFactory.createNewInstance("hashSetInstances", elementClass);
		else {
			// nothing defined yet.
		}
	}

	// @Override
	// public Object getObjectInstance(Object template, Name name,
	// Context context,
	// Hashtable environment) throws Exception {
	// return call(template);
	// }

	public boolean isInformListenerWhenEmpty() {
		return informListenerWhenEmpty;
	}

	public MultiListener /* EventMulticaster */ getListeners() {
		return listeners;
	}

	public void setInformListenerWhenEmpty(boolean informListenerWhenEmpty) {
		this.informListenerWhenEmpty = informListenerWhenEmpty;
	}

	/**
	 * Informs all listeners that a command has occured.
	 * 
	 * @param command
	 *            the event.
	 */
	private void informActionListeners(String command) {
		ActionEvent event = new ActionEvent(this, ActionEvent.ACTION_PERFORMED, command);
		listeners.actionPerformed(event);
		// TODO refactor listeners, so that only the command is a parameter
	}

	/**
	 * Checks whether this set is empty. Informs the listeners if empty.
	 */
	private void checkEmpty() {
		if (informListenerWhenEmpty && isEmpty()) {
			informActionListeners("isEmpty");
		}

		if (throwExceptionWhenEmptied && isEmpty()) {
			throw new IllegalStateException("set must not be empty");
		}
	}

	@Override
	public boolean remove(Object o) {
		boolean result = super.remove(o);
		checkEmpty();
		return result;
	}

	@Override
	public void clear() {
		super.clear();
		checkEmpty();

	}

	public boolean isThrowExceptionWhenEmptied() {
		return throwExceptionWhenEmptied;
	}

	public void setThrowExceptionWhenEmptied(boolean throwExceptionWhenEmptied) {
		this.throwExceptionWhenEmptied = throwExceptionWhenEmptied;
	}

	@Override
	public  Name getName() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setName(Object newName) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public Element call(Object... parameters) throws Exception {
		return factory.call(parameters);
	}

	@Override
	public Condition getPrecondition() throws Exception {
		return null;
	}

	@Override
	public Condition getPostcondition(Condition optionalPrecondition) throws Exception {
		return null;
	}

	@Override
	public Class getCallParameterClass() {
		return null;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy