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

org.umlg.runtime.collection.ocl.OclStdLibOrderedSetImpl Maven / Gradle / Ivy

There is a newer version: 2.0.15
Show newest version
package org.umlg.runtime.collection.ocl;

import org.apache.commons.collections4.set.ListOrderedSet;
import org.umlg.runtime.collection.UmlgCollection;
import org.umlg.runtime.collection.UmlgOrderedSet;
import org.umlg.runtime.collection.UmlgSequence;
import org.umlg.runtime.collection.memory.UmlgMemoryOrderedSet;
import org.umlg.runtime.domain.ocl.OclIsInvalidException;

import java.util.*;

public class OclStdLibOrderedSetImpl extends OclStdLibCollectionImpl implements UmlgOrderedSet {

	private ListOrderedSet orderedSet;

	/**
	 * A regular constructor compiles in eclipse but not in maven
	 * @param collection
	 * @return
	 */
	public static  OclStdLibOrderedSetImpl get(Collection collection) {
		return new OclStdLibOrderedSetImpl(ListOrderedSet.listOrderedSet(new ArrayList<>(collection)));
	}
	
	@SuppressWarnings("unchecked")
	public OclStdLibOrderedSetImpl(ListOrderedSet orderedSet) {
		super(orderedSet);
		this.orderedSet = orderedSet;
	}
	
	@Override
	public UmlgOrderedSet append(E e) {
        UmlgOrderedSet result = new UmlgMemoryOrderedSet(this);
        result.add(e);
        return result;
	}

	@Override
	public UmlgOrderedSet prepend(E e) {
        UmlgOrderedSet result = new UmlgMemoryOrderedSet(this);
        result.add(0, e);
        return result;
	}

	@Override
	public UmlgOrderedSet insertAt(Integer index, E e) {
        UmlgOrderedSet result = new UmlgMemoryOrderedSet(this);
        result.add(index, e);
        return result;
	}

	@Override
	public UmlgOrderedSet subOrderedSet(Integer lower, Integer upper) {
        //Sublist excludes the upper element
        UmlgOrderedSet subList = OclStdLibOrderedSetImpl.get(this.orderedSet.asList().subList(lower, upper));
        subList.add(get(upper));
        return subList;
	}

	@Override
	public E at(Integer i) {
        return get(i);
	}

	@Override
	public E first() {
        if (this.orderedSet.isEmpty()) {
            throw new OclIsInvalidException();
        } else {
            return (E)this.orderedSet.get(0);
        }
	}

	@Override
	public E last() {
        if (this.orderedSet.isEmpty()) {
            throw new OclIsInvalidException();
        } else {
            return (E)this.orderedSet.get(this.orderedSet.size() - 1);
        }
	}

	@Override
	public UmlgOrderedSet reverse() {
        List result = new ArrayList(this);
        Collections.reverse(result);
        return new UmlgMemoryOrderedSet(result);
	}

	
	/***************************************************
	 * Iterate goodies
	 ***************************************************/
	
	@Override
	public UmlgOrderedSet select(BooleanExpressionEvaluator v) {
		ListOrderedSet result = new ListOrderedSet();
		for (E e : this.collection) {
			if (v.evaluate(e)) {
				result.add(e);
			}
		}
		return new OclStdLibOrderedSetImpl(result);
	}

	@SuppressWarnings("unchecked")
	@Override
	public  UmlgSequence collectNested(BodyExpressionEvaluator v) {
        List result = new ArrayList();
		for (Object e : this.orderedSet) {
			R evaluate = v.evaluate((E)e);
			if (evaluate != null) {
				result.add(evaluate);
			}
		}
		return new OclStdLibSequenceImpl(result);
	}
	
	@Override
	public  UmlgSequence collect(BodyExpressionEvaluator v) {
		return collectNested(v).flatten();
	}

	@Override
	public  UmlgSequence flatten() {
        List result = new ArrayList();
		for (Object e : this.orderedSet) {
			if (e instanceof UmlgCollection) {
				UmlgCollection collection = (UmlgCollection) e;
				result.addAll(collection. flatten());
			} else {
				result.add((R)e);
			}
		}
		return new OclStdLibSequenceImpl(result);
	}

	@Override
	public boolean isEmpty() {
		return this.orderedSet.isEmpty();
	}

	@Override
	public boolean contains(Object o) {
		return this.orderedSet.contains(o);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Iterator iterator() {
		return this.orderedSet.iterator();
	}

	@Override
	public Object[] toArray() {
		return this.orderedSet.toArray();
	}

	@SuppressWarnings("unchecked")
	@Override
	public  T[] toArray(T[] a) {
		return (T[]) this.orderedSet.toArray(a);
	}

    @Override
    public boolean inverseAdder(E e) {
        return this.orderedSet.add(e);
    }

    @Override
	public boolean add(E e) {
		return this.orderedSet.add(e);
	}

	@Override
	public boolean remove(Object o) {
		return this.orderedSet.remove(o);
	}

	@Override
	public boolean containsAll(Collection c) {
		return this.orderedSet.containsAll(c);
	}

	@Override
	public boolean addAll(Collection c) {
		return this.orderedSet.addAll(c);
	}

	@Override
	public boolean retainAll(Collection c) {
		return this.orderedSet.retainAll(c);
	}

	@Override
	public boolean removeAll(Collection c) {
		return this.orderedSet.removeAll(c);
	}

	@Override
	public void clear() {
		this.orderedSet.clear();
	}

	@Override
	public boolean addAll(int index, Collection c) {
		return this.orderedSet.addAll(index, c);
	}

	@SuppressWarnings("unchecked")
	@Override
	public E get(int index) {
		return (E) this.orderedSet.get(index);
	}

	@Override
	public E set(int index, E element) {
		throw new RuntimeException("Not supported");
	}

	@Override
	public void add(int index, E element) {
		this.orderedSet.add(index, element);
	}

	@SuppressWarnings("unchecked")
	@Override
	public E remove(int index) {
		return (E) this.orderedSet.remove(index);
	}

	@Override
	public int indexOf(Object o) {
		return this.orderedSet.indexOf(o);
	}

	@Override
	public int lastIndexOf(Object o) {
		throw new RuntimeException("Not supported");
	}

	@Override
	public ListIterator listIterator() {
		throw new RuntimeException("Not supported");
	}

	@Override
	public ListIterator listIterator(int index) {
		throw new RuntimeException("Not supported");
	}

	@Override
	public List subList(int fromIndex, int toIndex) {
        return this.orderedSet.asList().subList(fromIndex, toIndex);
	}

	@Override
	public UmlgOrderedSet including(E e) {
		if (e != null) {
			this.orderedSet.add(e);
		}
		return this;
	}

	@Override
	public String toJson() {
		//TODO
		throw new RuntimeException("Not yet implemented");
	}

	//Predefined Iterator Expressions
	@Override
	public UmlgOrderedSet sortedBy(Comparator comparator) {
		List list = new ArrayList<>(this.orderedSet);
		Collections.sort(list, comparator);
		UmlgOrderedSet result = new UmlgMemoryOrderedSet<>(list);
		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy