Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordwagelaar2014-01-03 20:15:20 +0000
committerdwagelaar2014-01-03 20:15:20 +0000
commit9076785c08c193306e4a62086173694752c9b2bc (patch)
treee4562b433792b7d158f879e8818d1e776b4ab581 /plugins/org.eclipse.m2m.atl.emftvm
parent7b280e8bc7276c69a9c4944c5e8f6177e0c62cab (diff)
downloadorg.eclipse.atl-9076785c08c193306e4a62086173694752c9b2bc.tar.gz
org.eclipse.atl-9076785c08c193306e4a62086173694752c9b2bc.tar.xz
org.eclipse.atl-9076785c08c193306e4a62086173694752c9b2bc.zip
Implementation + tests of Collection::sortedBy(body).
422818: EMFTVM does not implement Collection::sortedBy(body) https://bugs.eclipse.org/bugs/show_bug.cgi?id=422818
Diffstat (limited to 'plugins/org.eclipse.m2m.atl.emftvm')
-rw-r--r--plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazyBag.java45
-rw-r--r--plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazyCollection.java13
-rw-r--r--plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazyList.java66
-rw-r--r--plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazyOrderedSet.java47
-rw-r--r--plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazySet.java47
5 files changed, 214 insertions, 4 deletions
diff --git a/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazyBag.java b/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazyBag.java
index f4fb351d..09dd2145 100644
--- a/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazyBag.java
+++ b/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazyBag.java
@@ -12,8 +12,10 @@
package org.eclipse.m2m.atl.emftvm.util;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@@ -766,6 +768,47 @@ public class LazyBag<E> extends LazyCollection<E> {
});
}
- //TODO provide other iterator operations: collectNested, sortedBy
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public LazyList<E> sortedBy(final CodeBlock body) {
+ // Parent frame may change after this method returns!
+ final StackFrame parentFrame = body.getParentFrame();
+ body.setParentFrame(null);
+ return new LazyList<E>(this) {
+ @SuppressWarnings("unchecked")
+ @Override
+ public Iterator<E> iterator() {
+ final Collection<E> inner = (Collection<E>) dataSource;
+ if (inner != null) {
+ final Iterator<Comparable<Object>> sortingKeys = new CollectIterator<Comparable<Object>>(inner, body, parentFrame);
+ final Object[] innerCopy = inner.toArray();
+ final Map<Object, Comparable<Object>> elementsToKeys = new HashMap<Object, Comparable<Object>>(innerCopy.length);
+ for (Object o : innerCopy) {
+ elementsToKeys.put(o, sortingKeys.next());
+ }
+ assert !sortingKeys.hasNext();
+ Arrays.sort(innerCopy, new Comparator<Object>() {
+ public int compare(Object o1, Object o2) {
+ return elementsToKeys.get(o1).compareTo(elementsToKeys.get(o2));
+ }
+ });
+ cache = (Collection<E>) Arrays.asList(innerCopy);
+ dataSource = null;
+ }
+ return super.iterator();
+ }
+ @Override
+ public int size() {
+ if (dataSource == null) {
+ return cache.size();
+ }
+ return ((Collection<E>) dataSource).size();
+ }
+ };
+ }
+
+ //TODO provide other iterator operations: collectNested
}
diff --git a/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazyCollection.java b/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazyCollection.java
index 910a7580..250c476c 100644
--- a/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazyCollection.java
+++ b/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazyCollection.java
@@ -2353,4 +2353,17 @@ public abstract class LazyCollection<E> implements Collection<E> {
return result;
}
+ /**
+ * Results in the Collection containing all elements of the source
+ * collection. The element for which body has the lowest value comes
+ * first, and so on. The type of the body expression must have the
+ * <code>&lt;</code> operation defined. The <code>&lt;</code> operation
+ * must return a Boolean value and must be transitive (i.e., if
+ * <code>a &lt; b</code> and <code>b &lt; c</code> then
+ * <code>a &lt; c</code>).
+ * @param body the function to evaluate on each element
+ * @return the sorted collection
+ */
+ public abstract LazyCollection<E> sortedBy(final CodeBlock body);
+
} \ No newline at end of file
diff --git a/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazyList.java b/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazyList.java
index d4fc5cee..91d4a73d 100644
--- a/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazyList.java
+++ b/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazyList.java
@@ -12,11 +12,15 @@
package org.eclipse.m2m.atl.emftvm.util;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
+import java.util.Map;
import java.util.NoSuchElementException;
import org.eclipse.m2m.atl.emftvm.CodeBlock;
@@ -1546,6 +1550,25 @@ public class LazyList<E> extends LazyCollection<E> implements List<E> {
}
}
+
+ /**
+ * {@link LazyList} that implements the {@link LazyList#collect(CodeBlock)} function.
+ * @author <a href="dwagelaar@gmail.com">Dennis Wagelaar</a>
+ *
+ * @param <E> the element type
+ */
+ public static class CollectList<E> extends LazyList<E> {
+
+ /**
+ * Creates a {@link CollectList} around <code>dataSource</code>.
+ * @param dataSource the underlying {@link LazyList}
+ */
+ public CollectList(final LazyList<E> dataSource) {
+ super(dataSource);
+ assert dataSource != null;
+ }
+
+ }
/**
* Creates a {@link LazyList} around <code>dataSource</code>.
@@ -2183,6 +2206,47 @@ public class LazyList<E> extends LazyCollection<E> implements List<E> {
};
}
- //TODO provide other iterator operations: collectNested, sortedBy
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public LazyList<E> sortedBy(final CodeBlock body) {
+ // Parent frame may change after this method returns!
+ final StackFrame parentFrame = body.getParentFrame();
+ body.setParentFrame(null);
+ return new LazyList<E>(this) {
+ @SuppressWarnings("unchecked")
+ @Override
+ public Iterator<E> iterator() {
+ final Collection<E> inner = (Collection<E>) dataSource;
+ if (inner != null) {
+ final Iterator<Comparable<Object>> sortingKeys = new CollectIterator<Comparable<Object>>(inner, body, parentFrame);
+ final Object[] innerCopy = inner.toArray();
+ final Map<Object, Comparable<Object>> elementsToKeys = new HashMap<Object, Comparable<Object>>(innerCopy.length);
+ for (Object o : innerCopy) {
+ elementsToKeys.put(o, sortingKeys.next());
+ }
+ assert !sortingKeys.hasNext();
+ Arrays.sort(innerCopy, new Comparator<Object>() {
+ public int compare(Object o1, Object o2) {
+ return elementsToKeys.get(o1).compareTo(elementsToKeys.get(o2));
+ }
+ });
+ cache = (Collection<E>) Arrays.asList(innerCopy);
+ dataSource = null;
+ }
+ return super.iterator();
+ }
+ @Override
+ public int size() {
+ if (dataSource == null) {
+ return cache.size();
+ }
+ return ((Collection<E>) dataSource).size();
+ }
+ };
+ }
+
+ // TODO provide other iterator operations: collectNested
}
diff --git a/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazyOrderedSet.java b/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazyOrderedSet.java
index b8a785cd..b99369db 100644
--- a/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazyOrderedSet.java
+++ b/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazyOrderedSet.java
@@ -12,11 +12,15 @@
package org.eclipse.m2m.atl.emftvm.util;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
+import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
@@ -2583,6 +2587,47 @@ public class LazyOrderedSet<E> extends LazyCollection<E> implements Set<E>, List
});
}
- //TODO provide other iterator operations: collectNested, sortedBy
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public LazyOrderedSet<E> sortedBy(final CodeBlock body) {
+ // Parent frame may change after this method returns!
+ final StackFrame parentFrame = body.getParentFrame();
+ body.setParentFrame(null);
+ return new LazyOrderedSet<E>(this) {
+ @SuppressWarnings("unchecked")
+ @Override
+ public Iterator<E> iterator() {
+ final Collection<E> inner = (Collection<E>) dataSource;
+ if (inner != null) {
+ final Iterator<Comparable<Object>> sortingKeys = new CollectIterator<Comparable<Object>>(inner, body, parentFrame);
+ final Object[] innerCopy = inner.toArray();
+ final Map<Object, Comparable<Object>> elementsToKeys = new HashMap<Object, Comparable<Object>>(innerCopy.length);
+ for (Object o : innerCopy) {
+ elementsToKeys.put(o, sortingKeys.next());
+ }
+ assert !sortingKeys.hasNext();
+ Arrays.sort(innerCopy, new Comparator<Object>() {
+ public int compare(Object o1, Object o2) {
+ return elementsToKeys.get(o1).compareTo(elementsToKeys.get(o2));
+ }
+ });
+ cache = (Collection<E>) Arrays.asList(innerCopy);
+ dataSource = null;
+ }
+ return super.iterator();
+ }
+ @Override
+ public int size() {
+ if (dataSource == null) {
+ return cache.size();
+ }
+ return ((Collection<E>) dataSource).size();
+ }
+ };
+ }
+
+ //TODO provide other iterator operations: collectNested
}
diff --git a/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazySet.java b/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazySet.java
index 6061516c..7b1f30f6 100644
--- a/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazySet.java
+++ b/plugins/org.eclipse.m2m.atl.emftvm/src/org/eclipse/m2m/atl/emftvm/util/LazySet.java
@@ -11,10 +11,14 @@
*******************************************************************************/
package org.eclipse.m2m.atl.emftvm.util;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
+import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
@@ -912,6 +916,47 @@ public class LazySet<E> extends LazyCollection<E> implements Set<E> {
});
}
- //TODO provide other iterator operations: collectNested, sortedBy
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public LazyOrderedSet<E> sortedBy(final CodeBlock body) {
+ // Parent frame may change after this method returns!
+ final StackFrame parentFrame = body.getParentFrame();
+ body.setParentFrame(null);
+ return new LazyOrderedSet<E>(this) {
+ @SuppressWarnings("unchecked")
+ @Override
+ public Iterator<E> iterator() {
+ final Collection<E> inner = (Collection<E>) dataSource;
+ if (inner != null) {
+ final Iterator<Comparable<Object>> sortingKeys = new CollectIterator<Comparable<Object>>(inner, body, parentFrame);
+ final Object[] innerCopy = inner.toArray();
+ final Map<Object, Comparable<Object>> elementsToKeys = new HashMap<Object, Comparable<Object>>(innerCopy.length);
+ for (Object o : innerCopy) {
+ elementsToKeys.put(o, sortingKeys.next());
+ }
+ assert !sortingKeys.hasNext();
+ Arrays.sort(innerCopy, new Comparator<Object>() {
+ public int compare(Object o1, Object o2) {
+ return elementsToKeys.get(o1).compareTo(elementsToKeys.get(o2));
+ }
+ });
+ cache = (Collection<E>) Arrays.asList(innerCopy);
+ dataSource = null;
+ }
+ return super.iterator();
+ }
+ @Override
+ public int size() {
+ if (dataSource == null) {
+ return cache.size();
+ }
+ return ((Collection<E>) dataSource).size();
+ }
+ };
+ }
+
+ //TODO provide other iterator operations: collectNested
}

Back to the top