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

util.Stdlib Maven / Gradle / Ivy

The newest version!
package util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/** Class ...
 */
public class Stdlib {



	/** Implements the equals operation for bags
	 * 
	 * @param source 
	 * @param arg 
	 */
	static public  boolean bagEquals(List source, List arg) {
		/* make copy of arguments in order to manipulate the collection*/
		if ( source.size() != arg.size() ) {
			return false;
		}
		List myArg = new ArrayList( arg );
		for ( T elem : source ) {
			if ( myArg.contains(elem) ) {
				myArg.remove(elem);
			} else {
				return false;
			}
		}
		return myArg.isEmpty();
	}
	
	/** Implements the standard flatten operation on Bags.
	 * Because Java generic types are not checked during run-time this operation 
	 * cannot be implemented without compiler warnings.
	 * 
	 * @param source 
	 */
	@SuppressWarnings("unchecked")
	static public List bagFlatten(Object source) {
		List result = new ArrayList();
		if ( source instanceof Collection ) {
			Iterator it = ((Collection)source).iterator();
			while ( it.hasNext() ) {
				Object elem = it.next();
				if ( elem instanceof Collection ) {
					result.addAll( bagFlatten(elem));
				} else {
					result.add(elem);
				}
			}
		}
		return result;
	}
	
	static public List boolAsBag(boolean myBool) {
		List result = new ArrayList();
		result.add( new Boolean(myBool) );
		return result;
	}
	
	static public List boolAsOrderedSet(boolean myBool) {
		List result = new ArrayList();
		result.add( new Boolean(myBool) );
		return result;
	}
	
	static public List boolAsSequence(boolean myBool) {
		List result = new ArrayList();
		result.add( new Boolean(myBool) );
		return result;
	}
	
	static public Set boolAsSet(boolean myBool) {
		Set result = new HashSet();
		result.add( new Boolean(myBool) );
		return result;
	}
	
	static public  List collectionAsBag(Collection myCollection) {
		List result = new ArrayList();
		if ( myCollection != null ) {
			result.addAll( myCollection );
		}
		return result;
	}
	
	static public  List collectionAsOrderedSet(Collection myCollection) {
		List result = new ArrayList();
		if ( myCollection != null ) {
			result.addAll( myCollection );
		}
		return result;
	}
	
	static public  List collectionAsSequence(Collection myCollection) {
		List result = new ArrayList();
		if ( myCollection != null ) {
			result.addAll( myCollection );
		}
		return result;
	}
	
	static public  Set collectionAsSet(Collection myCollection) {
		Set result = new HashSet();
		if ( myCollection != null ) {
			result.addAll( myCollection );
		}
		return result;
	}
	
	/** Implements the excludesAll operation for sets
	 * 
	 * @param source 
	 * @param arg 
	 */
	static public boolean excludesAll(Collection source, Collection arg) {
		Iterator it = arg.iterator();
		while ( it.hasNext() ) {
			Object elem = it.next();
			if ( source.contains(elem) ) {
				return false;
			}
		}
		return true;
	}
	
	static public  Set excluding(Set mySource, T myElem) {
		Set result = new HashSet(mySource);
		if ( myElem != null ) {
			result.remove(myElem);
		}
		return result;
	}
	
	static public  List excluding(List mySource, T myElem) {
		List result = new ArrayList(mySource);
		if ( myElem != null ) {
			result.remove(myElem);
		}
		return result;
	}
	
	static public  Set including(Set mySource, T myElem) {
		Set result = new HashSet(mySource);
		if ( myElem != null ) {
			result.add(myElem);
		}
		return result;
	}
	
	static public  List including(List mySource, T myElem) {
		List result = new ArrayList(mySource);
		if ( myElem != null ) {
			result.add(myElem);
		}
		return result;
	}
	
	static public  List insertAt(List mySource, int myIndex, T myElem) {
		if ( mySource == null ) {
			return null;
		}
		if ( myElem != null ) {
			mySource.add(myIndex, myElem);
		}
		return mySource;
	}
	
	static public List intAsBag(int myInt) {
		List result = new ArrayList();
		result.add( new Integer(myInt) );
		return result;
	}
	
	static public List intAsOrderedSet(int myInt) {
		List result = new ArrayList();
		result.add( new Integer(myInt) );
		return result;
	}
	
	static public List intAsSequence(int myInt) {
		List result = new ArrayList();
		result.add( new Integer(myInt) );
		return result;
	}
	
	static public Set intAsSet(int myInt) {
		Set result = new HashSet();
		result.add( new Integer(myInt) );
		return result;
	}
	
	static public  Set intersection(Collection mySource, Collection myElem) {
		Set result = new TreeSet(mySource);
		if ( myElem != null ) {
			result.retainAll(myElem);
		}
		return result;
	}
	
	static public  List objectAsBag(E myObject) {
		List result = new ArrayList();
		if ( myObject != null ) {
			result.add( myObject );
		}
		return (List)result;
	}
	
	static public  List objectAsOrderedSet(E myObject) {
		List result = new ArrayList();
		if ( myObject != null ) {
			result.add( myObject );
		}
		return (List)result;
	}
	
	static public  List objectAsSequence(E myObject) {
		List result = new ArrayList();
		if ( myObject != null ) {
			result.add( myObject );
		}
		return (List)result;
	}
	
	static public  Set objectAsSet(E myObject) {
		Set result = new HashSet();
		if ( myObject != null ) {
			result.add( myObject );
		}
		return (Set)result;
	}
	
	/** Implements the equals operation for orderedsets
	 * 
	 * @param source 
	 * @param arg 
	 */
	static public boolean orderedsetEquals(List source, List arg) {
		if ( source.size() != arg.size() ) {
			return false;
		}
		Iterator it1 = source.iterator();
		Iterator it2 = arg.iterator();
		while ( it1.hasNext() ) {
			Object elem1 = it1.next();
			Object elem2 = it2.next();
			if ( elem1 instanceof Integer ) {
				if ( ((Integer)elem1).intValue() != ((Integer)elem2).intValue() ) {
					return false;
				}
			} else {
				if ( elem1 instanceof Float ) {
					if ( ((Float)elem1).floatValue() != ((Float)elem2).floatValue() ) {
						return false;
					}
				} else {
					if ( elem1 instanceof Boolean ) {
						if ( ((Boolean)elem1).booleanValue() != ((Boolean)elem2).booleanValue() ) {
							return false;
						}
					} else {
						if ( !elem1.equals(elem2) ) {
							return false;
						}
					}
				}
			}
		}
		return true;
	}
	
	/** Implements the standard flatten operation on OrderedSets.
	 * Because Java generic types are not checked during run-time this operation 
	 * cannot be implemented without compiler warnings.
	 * 
	 * @param source 
	 */
	@SuppressWarnings("unchecked")
	static public List orderedsetFlatten(Object source) {
		List result = new ArrayList();
		if ( source instanceof Collection ) {
			Iterator it = ((Collection)source).iterator();
			while ( it.hasNext() ) {
				Object elem = it.next();
				if ( elem instanceof Collection ) {
					result.addAll( orderedsetFlatten(elem));
				} else {
					result.add(elem);
				}
			}
		}
		return result;
	}
	
	static public List realAsBag(double myReal) {
		List result = new ArrayList();
		result.add( new Double(myReal) );
		return result;
	}
	
	static public List realAsOrderedSet(double myReal) {
		List result = new ArrayList();
		result.add( new Double(myReal) );
		return result;
	}
	
	static public List realAsSequence(double myReal) {
		List result = new ArrayList();
		result.add( new Double(myReal) );
		return result;
	}
	
	static public Set realAsSet(double myReal) {
		Set result = new HashSet();
		result.add( new Double(myReal) );
		return result;
	}
	
	/** Implements the equals operation for sequences
	 * 
	 * @param source 
	 * @param arg 
	 */
	static public boolean sequenceEquals(List source, List arg) {
		if ( source.size() != arg.size() ) {
			return false;
		}
		Iterator it1 = source.iterator();
		Iterator it2 = arg.iterator();
		while ( it1.hasNext() ) {
			Object elem1 = it1.next();
			Object elem2 = it2.next();
			if ( elem1 instanceof Integer ) {
				if ( ((Integer)elem1).intValue() != ((Integer)elem2).intValue() ) {
					return false;
				}
			} else {
				if ( elem1 instanceof Float ) {
					if ( ((Float)elem1).floatValue() != ((Float)elem2).floatValue() ) {
						return false;
					}
				} else {
					if ( elem1 instanceof Boolean ) {
						if ( ((Boolean)elem1).booleanValue() != ((Boolean)elem2).booleanValue() ) {
							return false;
						}
					} else {
						if ( !elem1.equals(elem2) ) {
							return false;
						}
					}
				}
			}
		}
		return true;
	}
	
	/** Implements the standard flatten operation on Sequences.
	 * Because Java generic types are not checked during run-time this operation 
	 * cannot be implemented without compiler warnings.
	 * 
	 * @param source 
	 */
	@SuppressWarnings("unchecked")
	static public List sequenceFlatten(Object source) {
		List result = new ArrayList();
		if ( source instanceof Collection ) {
			Iterator it = ((Collection)source).iterator();
			while ( it.hasNext() ) {
				Object elem = it.next();
				if ( elem instanceof Collection ) {
					result.addAll( sequenceFlatten(elem));
				} else {
					result.add(elem);
				}
			}
		}
		return result;
	}
	
	/** Implements the equals operation for sets
	 * 
	 * @param source 
	 * @param arg 
	 */
	static public boolean setEquals(Set source, Set arg) {
		if ( source.size() != arg.size() ) {
			return false;
		}
		Iterator it = arg.iterator();
		while ( it.hasNext() ) {
			Object elem = it.next();
			if ( !source.contains(elem) ) {
				return false;
			}
		}
		return true;
	}
	
	/** Implements the standard flatten operation on Sets.
	 * Because Java generic types are not checked during run-time this operation 
	 * cannot be implemented without compiler warnings.
	 * 
	 * @param source 
	 */
	@SuppressWarnings("unchecked")
	static public Set setFlatten(Object source) {
		Set result = new HashSet();
		if ( source instanceof Collection ) {
			Iterator it = ((Collection)source).iterator();
			while ( it.hasNext() ) {
				Object elem = it.next();
				if ( elem instanceof Collection ) {
					result.addAll( setFlatten(elem));
				} else {
					result.add(elem);
				}
			}
		}
		return result;
	}
	
	static public List stringAsBag(String myString) {
		List result = new ArrayList();
		if ( myString != null ) {
			result.add( myString );
		}
		return result;
	}
	
	static public List stringAsOrderedSet(String myString) {
		List result = new ArrayList();
		if ( myString != null ) {
			result.add( myString );
		}
		return result;
	}
	
	static public List stringAsSequence(String myString) {
		List result = new ArrayList();
		if ( myString != null ) {
			result.add( myString );
		}
		return result;
	}
	
	static public Set stringAsSet(String myString) {
		Set result = new HashSet();
		if ( myString != null ) {
			result.add( myString );
		}
		return result;
	}

}