org.umlg.runtime.collection.ocl.OclStdLibSetImpl Maven / Gradle / Ivy
package org.umlg.runtime.collection.ocl;
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;
import org.umlg.runtime.collection.UmlgBag;
import org.umlg.runtime.collection.UmlgCollection;
import org.umlg.runtime.collection.UmlgOrderedSet;
import org.umlg.runtime.collection.UmlgSet;
import org.umlg.runtime.collection.memory.UmlgMemoryBag;
import org.umlg.runtime.collection.memory.UmlgMemoryOrderedSet;
import org.umlg.runtime.collection.memory.UmlgMemorySet;
import java.util.*;
public class OclStdLibSetImpl extends OclStdLibCollectionImpl implements UmlgSet {
private Set set;
/**
* A regular constructor compiles in eclipse but not in maven
*
* @param collection
* @return
*/
public static OclStdLibSetImpl get(Collection collection) {
return new OclStdLibSetImpl(new HashSet(collection));
}
public OclStdLibSetImpl(Set set) {
super(set);
this.set = set;
}
@Override
public UmlgSet union(UmlgSet extends E> s) {
UmlgSet result = new UmlgMemorySet(this);
result.addAll(s);
return result;
}
@Override
public UmlgBag union(UmlgBag extends E> bag) {
UmlgBag result = new UmlgMemoryBag(this);
result.addAll(bag);
return result;
}
@Override
public Boolean equals(UmlgSet s) {
if (size() != s.size()) {
return false;
}
for (E e : s) {
if (!contains(e)) {
return false;
}
}
return true;
}
@Override
public UmlgSet intersection(UmlgSet s) {
UmlgSet result = new UmlgMemorySet();
for (E e : s) {
if (contains(e)) {
result.add(e);
}
}
return result;
}
@Override
public UmlgSet intersection(UmlgBag bag) {
UmlgSet result = new UmlgMemorySet();
for (E e : bag) {
if (contains(e)) {
result.add(e);
}
}
return result;
}
@Override
public UmlgSet subtract(UmlgSet s) {
UmlgSet result = new UmlgMemorySet();
Iterator iter = iterator();
while (iter.hasNext()) {
E e = iter.next();
if (!s.contains(e)) {
result.add(e);
}
}
return result;
}
@Override
public UmlgSet including(E e) {
UmlgSet result = new UmlgMemorySet(this);
result.add(e);
return result;
}
@Override
public UmlgSet excluding(E e) {
UmlgSet result = new UmlgMemorySet(this);
result.remove(e);
return result;
}
@Override
public UmlgSet symmetricDifference(UmlgSet s) {
UmlgSet result = new UmlgMemorySet();
for (E e : s) {
if (!contains(e)) {
result.add(e);
}
}
Iterator iter = iterator();
while (iter.hasNext()) {
E e = iter.next();
if (!s.contains(e)) {
result.add(e);
}
}
return result;
}
/***************************************************
* Iterate goodies
***************************************************/
@Override
public UmlgSet select(BooleanExpressionEvaluator v) {
Set result = new HashSet();
for (E e : this.collection) {
if (v.evaluate(e)) {
result.add(e);
}
}
return new OclStdLibSetImpl(result);
}
@Override
public UmlgBag collectNested(BodyExpressionEvaluator v) {
Multiset result = HashMultiset.create();
for (E e : this.set) {
R evaluate = v.evaluate(e);
if (evaluate != null) {
result.add(evaluate);
}
}
return new OclStdLibBagImpl(result);
}
@Override
public UmlgBag collect(BodyExpressionEvaluator v) {
return collectNested(v).flatten();
}
@SuppressWarnings("unchecked")
@Override
public UmlgSet flatten() {
Set result = new HashSet();
for (E e : this.set) {
if (e instanceof UmlgCollection) {
UmlgCollection> collection = (UmlgCollection>) e;
result.addAll(collection.flatten());
} else {
result.add((R) e);
}
}
return new OclStdLibSetImpl(result);
}
@Override
public boolean isEmpty() {
return this.set.isEmpty();
}
@Override
public boolean contains(Object o) {
return this.set.contains(o);
}
@Override
public Iterator iterator() {
return this.set.iterator();
}
@Override
public Object[] toArray() {
return this.set.toArray();
}
@Override
public T[] toArray(T[] a) {
return this.set.toArray(a);
}
@Override
public boolean inverseAdder(E e) {
return this.set.add(e);
}
@Override
public boolean add(E e) {
return this.set.add(e);
}
@Override
public boolean remove(Object o) {
return this.set.remove(o);
}
@Override
public boolean containsAll(Collection> c) {
return this.set.containsAll(c);
}
@Override
public boolean addAll(Collection extends E> c) {
return this.set.addAll(c);
}
@Override
public boolean retainAll(Collection> c) {
return this.set.retainAll(c);
}
@Override
public boolean removeAll(Collection> c) {
return this.set.removeAll(c);
}
@Override
public void clear() {
this.set.clear();
}
//Predefined Iterator Expressions
@Override
public UmlgOrderedSet sortedBy(Comparator comparator) {
List list = new ArrayList<>(this.set);
Collections.sort(list, comparator);
UmlgOrderedSet result = new UmlgMemoryOrderedSet<>(list);
return result;
}
}