util.Stdlib Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of metamodel Show documentation
Show all versions of metamodel Show documentation
A uml code generator and execution engine
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