You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@marmotta.apache.org by ss...@apache.org on 2013/09/06 14:27:01 UTC
[09/18] temporarily added a patched version of javolution with fast
collections, because the released version has several bugs (see
https://java.net/jira/browse/JAVOLUTION-106 and
https://java.net/jira/browse/JAVOLUTION-105)
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/Index.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/Index.java b/commons/marmotta-commons/src/ext/java/javolution/util/Index.java
new file mode 100644
index 0000000..e63df1f
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/Index.java
@@ -0,0 +1,262 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util;
+
+import java.io.IOException;
+import java.io.ObjectStreamException;
+
+import javolution.lang.Configurable;
+import javolution.lang.MathLib;
+import javolution.lang.Realtime;
+import javolution.lang.ValueType;
+import javolution.text.Cursor;
+import javolution.text.DefaultTextFormat;
+import javolution.text.TextContext;
+import javolution.text.TextFormat;
+import javolution.text.TypeFormat;
+
+/**
+ * <p> A non-negative number representing a position in an arrangement.
+ * For example:
+ * [code]
+ * class SparseVector<F> {
+ * FastMap<Index, F> elements = new FastMap<Index, F>();
+ * ...
+ * }[/code]</p>
+
+ * <p> Index performance is on-par with the primitive {@code int} type
+ * for small values and similar to {@link Integer} instances for large
+ * values. Small indexes have no adverse effect on the garbage collector
+ * and have fast {@link #equals(Object) equals} method due to their unicity.</p>
+ *
+ * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
+ * @version 5.1, July 26, 2007
+ */
+@Realtime
+@DefaultTextFormat(Index.Decimal.class)
+public final class Index extends Number implements Comparable<Index>,
+ ValueType<Index> {
+
+ /**
+ * Default text format for indices (decimal value representation).
+ */
+ public static class Decimal extends TextFormat<Index> {
+
+ @Override
+ public Appendable format(Index obj, Appendable dest) throws IOException {
+ return TypeFormat.format(obj.intValue(), dest);
+ }
+
+ @Override
+ public Index parse(CharSequence csq, Cursor cursor)
+ throws IllegalArgumentException {
+ return Index.valueOf(TypeFormat.parseInt(csq, cursor));
+ }
+
+ }
+
+ /**
+ * Holds the number of unique preallocated instances (default {@code 1024}).
+ * This number is configurable, for example with
+ * {@code -Djavolution.util.Index#UNIQUE=0} there is no unique instance.
+ */
+ public static final Configurable<Integer> UNIQUE = new Configurable<Integer>() {
+
+ @Override
+ protected Integer getDefault() {
+ return 1024;
+ }
+
+ @Override
+ protected Integer initialized(Integer value) {
+ return MathLib.min(value, 65536); // Hard-limiting
+ }
+
+ @Override
+ protected Integer reconfigured(Integer oldCount, Integer newCount) {
+ throw new UnsupportedOperationException(
+ "Unicity reconfiguration not supported.");
+ }
+ };
+
+ /**
+ * Holds the index zero (value <code>0</code>).
+ */
+ public static final Index ZERO = new Index(0);
+
+ private static final long serialVersionUID = 0x600L; // Version.
+ private static final Index[] INSTANCES = new Index[UNIQUE.get()];
+ static {
+ INSTANCES[0] = ZERO;
+ for (int i = 1; i < INSTANCES.length; i++) {
+ INSTANCES[i] = new Index(i);
+ }
+ }
+
+ /**
+ * Returns the index for the specified {@code int} non-negative
+ * value (returns a preallocated instance if the specified value is
+ * small).
+ *
+ * @param value the index value.
+ * @return the corresponding index.
+ * @throws IndexOutOfBoundsException if <code>value < 0</code>
+ */
+ public static Index valueOf(int value) {
+ return (value < INSTANCES.length) ? INSTANCES[value] : new Index(value);
+ }
+
+ /**
+ * Holds the index value.
+ */
+ private final int value;
+
+ /**
+ * Creates an index having the specified value.
+ */
+ private Index(int value) {
+ this.value = value;
+ }
+
+ /**
+ * Compares this index with the specified index for order. Returns a
+ * negative integer, zero, or a positive integer as this index is less
+ * than, equal to, or greater than the specified index.
+ *
+ * @param that the index to be compared.
+ * @return a negative integer, zero, or a positive integer as this index
+ * is less than, equal to, or greater than the specified index.
+ */
+ public int compareTo(Index that) {
+ return this.value - that.value;
+ }
+
+ /**
+ * Compares this index with the specified integer value for order. Returns a
+ * negative integer, zero, or a positive integer as this index is less
+ * than, equal to, or greater than the specified value.
+ *
+ * @param value the value to be compared.
+ * @return a negative integer, zero, or a positive integer as this index
+ * is less than, equal to, or greater than the specified value.
+ */
+ public int compareTo(int value) {
+ return this.value - value;
+ }
+
+ /**
+ * Returns a copy of this index or <code>this</code> if the indexes
+ * is small (in permanent memory) in order to maintain unicity.
+ */
+ public Index copy() {
+ return value < INSTANCES.length ? this : new Index(value);
+ }
+
+ /**
+ * Returns the index value as <code>double</code>.
+ *
+ * @return the index value.
+ */
+ public double doubleValue() {
+ return (double) value;
+ }
+
+ /**
+ * Indicates if this index is equals to the one specified (for small
+ * indices this method is equivalent to <code>==</code>).
+ */
+ @Override
+ public boolean equals(Object obj) {
+ return (this.value < INSTANCES.length) ? (this == obj)
+ : ((obj instanceof Index) ? (((Index) obj).value == value)
+ : false);
+ }
+
+ /**
+ * Returns the index value as <code>float</code>.
+ *
+ * @return the index value.
+ */
+ public float floatValue() {
+ return (float) value;
+ }
+
+ /**
+ * Returns the hash code for this index.
+ */
+ @Override
+ public int hashCode() {
+ return value;
+ }
+
+ /**
+ * Returns the index value as <code>int</code>.
+ *
+ * @return the index value.
+ */
+ public int intValue() {
+ return value;
+ }
+
+ /**
+ * Indicates if this index is zero.
+ *
+ * @return {@code this == ZERO}
+ */
+ public boolean isZero() {
+ return this == ZERO;
+ }
+
+ /**
+ * Returns the index value as <code>long</code>.
+ *
+ * @return the index value.
+ */
+ public long longValue() {
+ return value;
+ }
+
+ /**
+ * Returns the index after this one.
+ */
+ public Index next() {
+ return Index.valueOf(value + 1);
+ }
+
+ /**
+ * Returns the index before this one.
+ *
+ * @throws IndexOutOfBoundsException if (this == Index.ZERO)
+ */
+ public Index previous() {
+ return Index.valueOf(value - 1);
+ }
+
+ /**
+ * Ensures index unicity during deserialization.
+ */
+ protected final Object readResolve() throws ObjectStreamException {
+ return Index.valueOf(value);
+ }
+
+ /**
+ * Returns the {@link String} representation of this index.
+ *
+ * @return {@code TextContext.getFormat(Index.class).format(this)}
+ */
+ @Override
+ public String toString() {
+ return TextContext.getFormat(Index.class).format(this);
+ }
+
+ @Override
+ public Index value() {
+ return this;
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/doc-files/FastTable-WCET.png
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/doc-files/FastTable-WCET.png b/commons/marmotta-commons/src/ext/java/javolution/util/doc-files/FastTable-WCET.png
new file mode 100644
index 0000000..68b19c1
Binary files /dev/null and b/commons/marmotta-commons/src/ext/java/javolution/util/doc-files/FastTable-WCET.png differ
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/doc-files/architecture.png
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/doc-files/architecture.png b/commons/marmotta-commons/src/ext/java/javolution/util/doc-files/architecture.png
new file mode 100644
index 0000000..a3b4ed9
Binary files /dev/null and b/commons/marmotta-commons/src/ext/java/javolution/util/doc-files/architecture.png differ
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/function/Consumer.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/function/Consumer.java b/commons/marmotta-commons/src/ext/java/javolution/util/function/Consumer.java
new file mode 100644
index 0000000..9c03869
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/function/Consumer.java
@@ -0,0 +1,29 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util.function;
+
+/**
+ * <p> A special type of function which does not return anything.</p>
+ *
+ * <p> Note: In future version this interface may derive from
+ * {@code Function<P, Void>}.</p>
+ *
+ * @param <T> The type of input parameter to accept.
+ *
+ * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
+ * @version 6.0, July 21, 2013
+ */
+public interface Consumer<T> {
+
+ /**
+ * Accepts an input value.
+ */
+ void accept(T param);
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/function/Equalities.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/function/Equalities.java b/commons/marmotta-commons/src/ext/java/javolution/util/function/Equalities.java
new file mode 100644
index 0000000..75e1441
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/function/Equalities.java
@@ -0,0 +1,79 @@
+package javolution.util.function;
+
+import javolution.lang.Parallelizable;
+import javolution.lang.Realtime;
+import javolution.util.internal.comparator.ArrayComparatorImpl;
+import javolution.util.internal.comparator.IdentityComparatorImpl;
+import javolution.util.internal.comparator.LexicalCaseInsensitiveComparatorImpl;
+import javolution.util.internal.comparator.LexicalComparatorImpl;
+import javolution.util.internal.comparator.LexicalFastComparatorImpl;
+import javolution.util.internal.comparator.StandardComparatorImpl;
+
+import static javolution.lang.Realtime.Limit.*;
+
+/**
+ * <p> A set of useful equalities comparators.</p>
+ *
+ * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
+ * @version 6.0, July 21, 2013
+ */
+public class Equalities {
+
+ /**
+ * A standard object comparator (based on the object hashCode and equals
+ * methods). Comparisons either use the object natural order (which
+ * should be consistent with equals) or an empirical method
+ * (if the object does not implement {@link Comparable}).
+ *
+ */
+ @Parallelizable
+ @Realtime(limit = UNKNOWN)
+ public static final Equality<Object> STANDARD = new StandardComparatorImpl<Object>();
+
+ /**
+ * A comparator for which instances are only equals to themselves.
+ * For comparisons an empirical method consistent with equals ({@code == })
+ * is used.
+ */
+ @Parallelizable
+ @Realtime(limit = CONSTANT)
+ public static final Equality<Object> IDENTITY = new IdentityComparatorImpl<Object>();
+
+ /**
+ * A content array comparator. If the content of an array is also
+ * an array (multi-dimensional arrays), that same comparator is used
+ * for equality and comparison (recursive). The {@link #STANDARD standard}
+ * comparator is used for non-array elements.
+ */
+ @Parallelizable
+ @Realtime(limit = LINEAR)
+ public static final Equality<Object> ARRAY = new ArrayComparatorImpl();
+
+ /**
+ * A lexicographic comparator for any {@link CharSequence}.
+ */
+ @Parallelizable
+ @Realtime(limit = LINEAR)
+ public static final Equality<CharSequence> LEXICAL = new LexicalComparatorImpl();
+
+ /**
+ * A case insensitive lexicographic comparator for any {@link CharSequence}.
+ */
+ @Parallelizable
+ @Realtime(limit = LINEAR)
+ public static final Equality<CharSequence> LEXICAL_CASE_INSENSITIVE = new LexicalCaseInsensitiveComparatorImpl();
+
+ /**
+ * An optimized lexical comparator for any {@link CharSequence} taking
+ * a sample of few characters instead of the whole character sequence to
+ * calculate the hash code (still equality comparison checks all characters).
+ */
+ @Parallelizable
+ @Realtime(limit = LINEAR)
+ public static final Equality<CharSequence> LEXICAL_FAST = new LexicalFastComparatorImpl();
+
+ /**
+ * Utility class (private constructor).
+ */
+ private Equalities() {}
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/function/Equality.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/function/Equality.java b/commons/marmotta-commons/src/ext/java/javolution/util/function/Equality.java
new file mode 100644
index 0000000..1d7e8dc
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/function/Equality.java
@@ -0,0 +1,77 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util.function;
+
+import java.io.Serializable;
+import java.util.Comparator;
+
+/**
+ * <p> A comparator to be used for element equality as well as for
+ * ordering. Implementing classes should ensure that:
+ * <ul>
+ * <li> The {@link #compare compare} function is consistent with
+ * {@link #areEqual equals}. If two objects {@link #compare compare}
+ * to {@code 0} then they are {@link #areEqual equals} and the
+ * the reciprocal is true (this ensures that sorted collections/maps
+ * do not break the general contract of their parent class based on
+ * object equal).</li>
+ * <li> The {@link #hashCodeOf hashcode} function is consistent with
+ * {@link #areEqual equals}: If two objects are equals, they have
+ * the same hashcode (the reciprocal is not true).</li>
+ * <li> The {@code null} value is supported (even for
+ * {@link #compare comparisons}) and the {@link #hashCodeOf(Object)
+ * hashcode} value of {@code null} is {@code 0}.</li>
+ * </ul>
+ * </p>
+ *
+ * @param <T> the type of objects that may be compared for equality or order.
+ *
+ * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
+ * @version 6.0, July 21, 2013
+ * @see Equalities
+ */
+public interface Equality<T> extends Comparator<T>, Serializable {
+
+ /**
+ * Returns the hash code for the specified object (consistent with
+ * {@link #areEqual}). Two objects considered {@link #areEqual equal} have
+ * the same hash code. The hash code of <code>null</code> is always
+ * <code>0</code>.
+ *
+ * @param object the object to return the hashcode for.
+ * @return the hashcode for the specified object.
+ */
+ int hashCodeOf(T object);
+
+ /**
+ * Indicates if the specified objects can be considered equal.
+ * This methods is equivalent to {@code (compare(o1, o2) == 0)} but
+ * usually faster.
+ *
+ * @param left the first object (or <code>null</code>).
+ * @param right the second object (or <code>null</code>).
+ * @return <code>true</code> if both objects are considered equal;
+ * <code>false</code> otherwise.
+ */
+ boolean areEqual(T left, T right);
+
+ /**
+ * Compares the specified objects for order. Returns a negative integer,
+ * zero, or a positive integer as the first argument is less than, possibly
+ * equal to, or greater than the second. Implementation classes should
+ * ensure that comparisons with {@code null} is supported.
+ *
+ * @param left the first object.
+ * @param right the second object.
+ * @return a negative integer, zero, or a positive integer as the first
+ * argument is less than, possibly equal to, or greater than the second.
+ */
+ int compare(T left, T right);
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/function/Function.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/function/Function.java b/commons/marmotta-commons/src/ext/java/javolution/util/function/Function.java
new file mode 100644
index 0000000..35471de
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/function/Function.java
@@ -0,0 +1,33 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util.function;
+
+
+/**
+ * <p> A function that perform some operation and returns the result of
+ * that operation.</p>
+ *
+ * @param <T> the type of the input parameter of the apply operation.
+ * @param <R> the type of the result of the apply operation.
+ *
+ * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
+ * @version 6.0, July 21, 2013
+ * @see <a href="http://en.wikipedia.org/wiki/Function_(computer_science)">Wikipedia: Function<a>
+ */
+public interface Function<T, R> {
+
+ /**
+ * Returns the result of applying this function to the specified parameter.
+ *
+ * @param param the parameter object on which the function is performed.
+ * @return the result of the function.
+ */
+ R apply(T param);
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/function/Iteration.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/function/Iteration.java b/commons/marmotta-commons/src/ext/java/javolution/util/function/Iteration.java
new file mode 100644
index 0000000..8d870ce
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/function/Iteration.java
@@ -0,0 +1,32 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util.function;
+
+import java.util.Iterator;
+
+/**
+ * <p> A function iterating over a collection.</p>
+ *
+ * <p> Except for {@link Mutable} instances, iterations are not
+ * allowed to modify the collection iterated.
+ *
+ * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
+ * @version 6.0, July 21, 2013
+ */
+public interface Iteration<E> {
+
+ public interface Mutable<E> extends Iteration<E> {}
+ public interface Sequential<E> extends Iteration<E> {}
+
+ /**
+ * Runs the iteration using the specified iterator.
+ */
+ void run(Iterator<E> it);
+
+ }
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/function/MultiVariable.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/function/MultiVariable.java b/commons/marmotta-commons/src/ext/java/javolution/util/function/MultiVariable.java
new file mode 100644
index 0000000..c38f4d8
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/function/MultiVariable.java
@@ -0,0 +1,54 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util.function;
+
+/**
+ * <p> An object holding multiple variables; typically used to create
+ * {@link Function multi-parameters functions}.</p>
+ *
+ * <p> Multi-variables may represent an unbounded number of variables.
+ * [code]
+ * MultiVariable<Double, MultiVariable<Integer, Boolean>> tertiaryVariable
+ * = new MultiVariable(2.3, new MultiVariable(57, true));
+ * [/code].</p>
+ *
+ * @param <L> the type of the variable on the left.
+ * @param <R> the type of the variable on the right.
+ *
+ * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
+ * @version 6.0, July 21, 2013
+ */
+public class MultiVariable<L, R> {
+
+ private final L left;
+ private final R right;
+
+ /**
+ * Returns a multi-variable holding the specified objects (possibly
+ * multi-variables themselves).
+ */
+ public MultiVariable(L left, R right) {
+ this.left = left;
+ this.right = right;
+ }
+
+ /**
+ * Returns the variable on the left.
+ */
+ public L getLeft() {
+ return left;
+ }
+
+ /**
+ * Returns the variable on the right.
+ */
+ public R getRight() {
+ return right;
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/function/Predicate.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/function/Predicate.java b/commons/marmotta-commons/src/ext/java/javolution/util/function/Predicate.java
new file mode 100644
index 0000000..e2c5803
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/function/Predicate.java
@@ -0,0 +1,32 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util.function;
+
+/**
+ * <p> A function which states or affirms the attribute or quality of something.</p>
+ *
+ * <p> Note: In future version this interface may derive from
+ * {@code Function<P, Boolean>}.</p>
+
+ * @param <T> The type of input object to test.
+ *
+ * @see <a href="http://en.wikipedia.org/wiki/Predicate_(mathematical_logic)">
+ * Wikipedia: Predicate<a>
+ *
+ * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
+ * @version 6.0, July 21, 2013
+ */
+public interface Predicate<T> {
+
+ /**
+ * Tests the specified value.
+ */
+ boolean test(T param);
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/function/Reducer.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/function/Reducer.java b/commons/marmotta-commons/src/ext/java/javolution/util/function/Reducer.java
new file mode 100644
index 0000000..73b3e44
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/function/Reducer.java
@@ -0,0 +1,26 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util.function;
+
+import java.util.Collection;
+
+
+/**
+ * <p> An operator upon multiple elements of a collection yielding a result
+ * of that collection type.</p>
+ *
+ * @param <E> The type of elements in the collection operated upon.
+ *
+ * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
+ * @version 6.0, July 21, 2013
+ * @see Reducers
+ */
+public interface Reducer<E> extends Consumer<Collection<E>>, Supplier<E> {
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/function/Reducers.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/function/Reducers.java b/commons/marmotta-commons/src/ext/java/javolution/util/function/Reducers.java
new file mode 100644
index 0000000..8705e12
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/function/Reducers.java
@@ -0,0 +1,229 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util.function;
+
+import static javolution.lang.Realtime.Limit.LINEAR;
+
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicReference;
+
+import javolution.lang.Parallelizable;
+import javolution.lang.Realtime;
+import javolution.util.FastCollection;
+
+/**
+ * <p> A set of useful {@link Reducer reducers} of collection elements.</p>
+ *
+ * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
+ * @version 6.0, July 21, 2013
+ * @see FastCollection#reduce(Reducer)
+ */
+public class Reducers {
+
+ // Utility class, no default constructor.
+ private Reducers() {}
+
+ /**
+ * Returns any non-null element of the specified type.
+ * This reducer stops iterating as soon as an element with the matching
+ * type is found.
+ */
+ @Parallelizable
+ @Realtime(limit = LINEAR)
+ public static <E> Reducer<E> any(Class<? extends E> type) {
+ return new AnyReducer<E>(type);
+ }
+
+ private static class AnyReducer<E> implements Reducer<E> {
+ private final Class<? extends E> type;
+ private volatile E found;
+
+ public AnyReducer(Class<? extends E> type) {
+ this.type = type;
+ }
+
+ @Override
+ public void accept(Collection<E> param) {
+ Iterator<E> it = param.iterator();
+ while (it.hasNext() && (found == null)) {
+ E e = it.next();
+ if (type.isInstance(e)) {
+ found = e;
+ break;
+ }
+ }
+ }
+
+ @Override
+ public E get() {
+ return found;
+ }
+ }
+
+ /**
+ * Returns the greatest element of a collection according to the
+ * specified comparator (returns {@code null} if the collection is empty).
+ */
+ @Parallelizable(mutexFree = true, comment = "Internal use of AtomicReference")
+ @Realtime(limit = LINEAR)
+ public static <E> Reducer<E> max(Comparator<? super E> comparator) {
+ return new MaxReducer<E>(comparator);
+ }
+
+ private static class MaxReducer<E> implements Reducer<E> {
+ private final Comparator<? super E> cmp;
+ private final AtomicReference<E> max = new AtomicReference<E>(null);
+
+ public MaxReducer(Comparator<? super E> cmp) {
+ this.cmp = cmp;
+ }
+
+ @Override
+ public void accept(Collection<E> param) {
+ Iterator<E> it = param.iterator();
+ while (it.hasNext()) {
+ E e = it.next();
+ E read = max.get();
+ while ((read == null) || (cmp.compare(e, read) > 0)) {
+ if (max.compareAndSet(read, e)) break;
+ read = max.get();
+ }
+ }
+ }
+
+ @Override
+ public E get() {
+ return max.get();
+ }
+ }
+
+ /**
+ * Returns the smallest element of a collection according to the collection
+ * comparator (returns {@code null} if the collection is empty).
+ */
+ @Parallelizable(mutexFree = true, comment = "Internal use of AtomicReference")
+ @Realtime(limit = LINEAR)
+ public static <E> Reducer<E> min(Comparator<? super E> comparator) {
+ return new MinReducer<E>(comparator);
+ }
+
+ private static class MinReducer<E> implements Reducer<E> {
+ private final Comparator<? super E> cmp;
+ private final AtomicReference<E> min = new AtomicReference<E>(null);
+
+ public MinReducer(Comparator<? super E> cmp) {
+ this.cmp = cmp;
+ }
+
+ @Override
+ public void accept(Collection<E> param) {
+ Iterator<E> it = param.iterator();
+ while (it.hasNext()) {
+ E e = it.next();
+ E read = min.get();
+ while ((read == null) || (cmp.compare(e, read) < 0)) {
+ if (min.compareAndSet(read, e)) break;
+ read = min.get();
+ }
+ }
+ }
+
+ @Override
+ public E get() {
+ return min.get();
+ }
+ }
+
+ /**
+ * Conditional 'and' operator (returns {@code true} if the collection is
+ * empty). This operator stops iterating as soon as a {@code false} value
+ * is found.
+ */
+ @Parallelizable
+ @Realtime(limit = LINEAR)
+ public static Reducer<Boolean> and() {
+ return new AndReducer();
+ }
+
+ private static class AndReducer implements Reducer<Boolean> {
+ volatile boolean result = true;
+
+ @Override
+ public void accept(Collection<Boolean> param) {
+ Iterator<Boolean> it = param.iterator();
+ while (result && it.hasNext()) {
+ if (!it.next()) result = false;
+ }
+ }
+
+ @Override
+ public Boolean get() {
+ return result;
+ }
+ }
+
+ /**
+ * Conditional 'or' operator (returns {@code false} if the collection is
+ * empty). This operator stops iterating as soon as a {@code true} value
+ * is found.
+ */
+ @Parallelizable
+ @Realtime(limit = LINEAR)
+ public static Reducer<Boolean> or() {
+ return new OrReducer();
+ }
+
+ private static class OrReducer implements Reducer<Boolean> {
+ volatile boolean result = false;
+
+ @Override
+ public void accept(Collection<Boolean> param) {
+ Iterator<Boolean> it = param.iterator();
+ while (!result && it.hasNext()) {
+ if (!it.next()) result = true;
+ }
+ }
+
+ @Override
+ public Boolean get() {
+ return result;
+ }
+ }
+
+ /**
+ * Returns the sum of the specified integers value (returns {@code 0}
+ * if the collection is empty).
+ */
+ @Parallelizable(comment = "Internal use of AtomicInteger")
+ @Realtime(limit = LINEAR)
+ public static Reducer<Integer> sum() {
+ return new SumReducer();
+ }
+
+ private static class SumReducer implements Reducer<Integer> {
+ private final AtomicInteger sum = new AtomicInteger(0);
+
+ @Override
+ public void accept(Collection<Integer> param) {
+ Iterator<Integer> it = param.iterator();
+ while (it.hasNext()) {
+ sum.getAndAdd(it.next().intValue());
+ }
+ }
+
+ @Override
+ public Integer get() {
+ return sum.get();
+ }
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/function/Splittable.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/function/Splittable.java b/commons/marmotta-commons/src/ext/java/javolution/util/function/Splittable.java
new file mode 100644
index 0000000..4ad3d5d
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/function/Splittable.java
@@ -0,0 +1,53 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util.function;
+
+
+/**
+ * An object which can be divided in distinct parts and on which the same
+ * action may be performed on the parts rather than the whole.
+ *
+ * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
+ * @version 6.0, July 21, 2013
+ */
+public interface Splittable<T> {
+
+ /**
+ * Executes a read-only action on the specified part of this object.
+ *
+ * @param action the read-only action.
+ * @param part this object or a part of it.
+ * @throws UnsupportedOperationException if the action tries to update the
+ * specified part.
+ */
+ void perform(Consumer<T> action, T part);
+
+ /**
+ * Returns {@code n} distinct parts of this object.
+ * This method may return an array of size less than {@code n}
+ * (e.g. an array of size one if this object cannot split).
+ *
+ * @param n the number of parts.
+ * @param threadsafe {@code true} if the returned parts can be updated
+ * concurrently; {@code false} otherwise.
+ * @return the distinct parts (or views) for this object.
+ * @throws IllegalArgumentException if {@code n < 1}
+ */
+ T[] split(int n, boolean threadsafe);
+
+ /**
+ * Executes an update action on the specified part of this object.
+ * Any change to the part is reflected in the whole (this object).
+ *
+ * @param action the action authorized to update this object part.
+ * @param part this object or a part of it.
+ */
+ void update(Consumer<T> action, T part);
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/function/Supplier.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/function/Supplier.java b/commons/marmotta-commons/src/ext/java/javolution/util/function/Supplier.java
new file mode 100644
index 0000000..984549a
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/function/Supplier.java
@@ -0,0 +1,30 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util.function;
+
+/**
+ * <p> A function which does not take any argument and returns instances
+ * of a particular class.</p>
+ *
+ * <p> Note: In future version this interface may derive from
+ * {@code Function<Void, R>}.</p>
+ *
+ * @param <T> The type of result this supplier returns.
+ *
+ * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
+ * @version 6.0, July 21, 2013
+ */
+public interface Supplier<T> {
+
+ /**
+ * Returns an object.
+ */
+ T get();
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/function/package-info.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/function/package-info.java b/commons/marmotta-commons/src/ext/java/javolution/util/function/package-info.java
new file mode 100644
index 0000000..4501ea9
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/function/package-info.java
@@ -0,0 +1,25 @@
+/**
+<p> Basic functions for lambda expressions and method references.</p>
+ Most often, functions do not have a state and can be called concurrently,
+ as indicated by the annotation {@link javolution.lang.Parallelizable Parallelizable}.</p>
+
+<p> Functions may take an arbitrary number of arguments through the use of
+ {@link javolution.util.function.MultiVariable multi-variables}
+ or no argument at all using the standard {@link java.lang.Void} class.
+[code]
+// Function populating a list of integer and returning void.
+Function<MultiVariable<List<Integer>, Integer>, Void> fill = new Function<>() {
+ public Void apply(MultiVariable<List<Integer>, Integer> params) {
+ List<Integer> list = params.getLeft();
+ for (int i = 0, n = params.getRight(); i < n; i++) {
+ list.add(i);
+ }
+ return null;
+ }
+};
+FastTable<Integer> list = new FastTable<Integer>();
+fill.apply(new MultiVariable(list, 100)); // Populates with numbers [0 .. 100[
+[/code]</p>
+ */
+package javolution.util.function;
+
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/internal/ReadWriteLockImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/ReadWriteLockImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/ReadWriteLockImpl.java
new file mode 100644
index 0000000..e4a464e
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/ReadWriteLockImpl.java
@@ -0,0 +1,138 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util.internal;
+
+import java.io.Serializable;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReadWriteLock;
+
+/**
+ * Simple and efficient read/write lock implementation giving
+ * preferences to writers. Acquiring a write lock then a read lock is
+ * supported. Writers may acquire a read lock after having the write lock
+ * but the reverse would result in deadlock.
+ */
+public final class ReadWriteLockImpl implements ReadWriteLock, Serializable {
+
+ /** Read-Lock Implementation. */
+ public final class ReadLock implements Lock, Serializable {
+ private static final long serialVersionUID = 0x600L; // Version.
+
+ @Override
+ public void lock() {
+ try {
+ lockInterruptibly();
+ } catch (java.lang.InterruptedException e) {}
+ }
+
+ @Override
+ public void lockInterruptibly() throws InterruptedException {
+ synchronized (ReadWriteLockImpl.this) {
+ if (writerThread == Thread.currentThread()) return; // Current thread has the writer lock.
+ while ((writerThread != null) || (waitingWriters != 0)) {
+ ReadWriteLockImpl.this.wait();
+ }
+ givenLocks++;
+ }
+ }
+
+ @Override
+ public Condition newCondition() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean tryLock() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean tryLock(long time, TimeUnit unit)
+ throws InterruptedException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void unlock() {
+ synchronized (ReadWriteLockImpl.this) {
+ if (writerThread == Thread.currentThread()) return; // Itself is the writing thread.
+ assert (givenLocks > 0);
+ givenLocks--;
+ ReadWriteLockImpl.this.notifyAll();
+ }
+ }
+ }
+
+ /** Write-Lock Implementation. */
+ public final class WriteLock implements Lock, Serializable {
+ private static final long serialVersionUID = 0x600L; // Version.
+
+ @Override
+ public void lock() {
+ try {
+ lockInterruptibly();
+ } catch (java.lang.InterruptedException e) {}
+ }
+
+ @Override
+ public void lockInterruptibly() throws InterruptedException {
+ synchronized (ReadWriteLockImpl.this) {
+ waitingWriters++;
+ while (givenLocks != 0) {
+ ReadWriteLockImpl.this.wait();
+ }
+ waitingWriters--;
+ writerThread = Thread.currentThread();
+ }
+ }
+
+ @Override
+ public Condition newCondition() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean tryLock() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean tryLock(long time, TimeUnit unit)
+ throws InterruptedException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void unlock() {
+ synchronized (ReadWriteLockImpl.this) {
+ writerThread = null;
+ ReadWriteLockImpl.this.notifyAll();
+ }
+ }
+ }
+
+ private static final long serialVersionUID = 0x600L; // Version.
+ public final ReadLock readLock = new ReadLock();
+ public final WriteLock writeLock = new WriteLock();
+ private transient int givenLocks;
+ private transient int waitingWriters;
+ private transient Thread writerThread;
+
+ @Override
+ public ReadLock readLock() {
+ return readLock;
+ }
+
+ @Override
+ public WriteLock writeLock() {
+ return writeLock;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/internal/bitset/BitSetIteratorImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/bitset/BitSetIteratorImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/bitset/BitSetIteratorImpl.java
new file mode 100644
index 0000000..d5cadc9
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/bitset/BitSetIteratorImpl.java
@@ -0,0 +1,51 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util.internal.bitset;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import javolution.util.Index;
+import javolution.util.service.BitSetService;
+
+/**
+ * An iterator over a bit set.
+ */
+public final class BitSetIteratorImpl implements Iterator<Index> {
+
+ private final BitSetService that;
+
+ private int nextIndex;
+
+ private int currentIndex = -1;
+
+ public BitSetIteratorImpl(BitSetService that, int index) {
+ this.that = that;
+ this.nextIndex = that.nextSetBit(index);
+ }
+
+ public boolean hasNext() {
+ return (nextIndex >= 0);
+ }
+
+ public Index next() {
+ if (nextIndex < 0)
+ throw new NoSuchElementException();
+ currentIndex = nextIndex;
+ nextIndex = that.nextSetBit(nextIndex + 1);
+ return Index.valueOf(currentIndex);
+ }
+
+ public void remove() {
+ if (currentIndex < 0)
+ throw new IllegalStateException();
+ that.clear(currentIndex);
+ currentIndex = -1;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/internal/bitset/BitSetServiceImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/bitset/BitSetServiceImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/bitset/BitSetServiceImpl.java
new file mode 100644
index 0000000..77c8c0c
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/bitset/BitSetServiceImpl.java
@@ -0,0 +1,373 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util.internal.bitset;
+
+import java.io.Serializable;
+import java.util.Iterator;
+
+import javolution.lang.MathLib;
+import javolution.util.Index;
+import javolution.util.function.Equalities;
+import javolution.util.function.Equality;
+import javolution.util.internal.set.SetView;
+import javolution.util.service.BitSetService;
+
+/**
+ * A table of indices implemented using packed bits (long[]).
+ */
+public class BitSetServiceImpl extends SetView<Index> implements BitSetService, Serializable {
+
+ private static final long serialVersionUID = 0x600L; // Version.
+
+ /** Holds the bits (64 bits per long), trimmed. */
+ private long[] bits;
+
+ /** Creates a bit set (256 bits). */
+ public BitSetServiceImpl() {
+ super(null); // Root.
+ bits = new long[4];
+ }
+
+ @Override
+ public boolean add(Index index) {
+ return !getAndSet(index.intValue(), true);
+ }
+
+ @Override
+ public void and(BitSetService that) {
+ long[] thatBits = that.toLongArray();
+ int n = MathLib.min(this.bits.length, thatBits.length);
+ for (int i = 0; i < n; i++) {
+ this.bits[i] &= thatBits[i];
+ }
+ for (int i = n; i < bits.length; i++) {
+ this.bits[i] = 0L;
+ }
+ trim();
+ }
+
+ @Override
+ public void andNot(BitSetService that) {
+ long[] thatBits = that.toLongArray();
+ int n = MathLib.min(this.bits.length, thatBits.length);
+ for (int i = 0; i < n; i++) {
+ this.bits[i] &= ~thatBits[i];
+ }
+ trim();
+ }
+
+ @Override
+ public int cardinality() {
+ int sum = 0;
+ for (int i = 0; i < bits.length; i++) {
+ sum += MathLib.bitCount(bits[i]);
+ }
+ return sum;
+ }
+
+ @Override
+ public void clear() {
+ bits = new long[0];
+ }
+
+ @Override
+ public void clear(int bitIndex) {
+ int longIndex = bitIndex >> 6;
+ if (longIndex >= bits.length)
+ return;
+ bits[longIndex] &= ~(1L << bitIndex);
+ trim();
+ }
+
+ @Override
+ public void clear(int fromIndex, int toIndex) {
+ if ((fromIndex < 0) || (toIndex < fromIndex))
+ throw new IndexOutOfBoundsException();
+ int i = fromIndex >>> 6;
+ if (i >= bits.length)
+ return; // Ensures that i < _length
+ int j = toIndex >>> 6;
+ if (i == j) {
+ bits[i] &= ((1L << fromIndex) - 1) | (-1L << toIndex);
+ return;
+ }
+ bits[i] &= (1L << fromIndex) - 1;
+ if (j < bits.length) {
+ bits[j] &= -1L << toIndex;
+ }
+ for (int k = i + 1; (k < j) && (k < bits.length); k++) {
+ bits[k] = 0;
+ }
+ trim();
+ }
+
+ @Override
+ public Equality<? super Index> comparator() {
+ return Equalities.IDENTITY;
+ }
+
+ @Override
+ public boolean contains(Object index) {
+ return get(((Index)index).intValue());
+ }
+
+ @Override
+ public void flip(int bitIndex) {
+ int i = bitIndex >> 6;
+ ensureCapacity(i + 1);
+ bits[i] ^= 1L << bitIndex;
+ trim();
+ }
+
+ @Override
+ public void flip(int fromIndex, int toIndex) {
+ if ((fromIndex < 0) || (toIndex < fromIndex))
+ throw new IndexOutOfBoundsException();
+ int i = fromIndex >>> 6;
+ int j = toIndex >>> 6;
+ ensureCapacity(j + 1);
+ if (i == j) {
+ bits[i] ^= (-1L << fromIndex) & ((1L << toIndex) - 1);
+ return;
+ }
+ bits[i] ^= -1L << fromIndex;
+ bits[j] ^= (1L << toIndex) - 1;
+ for (int k = i + 1; k < j; k++) {
+ bits[k] ^= -1;
+ }
+ trim();
+ }
+
+ @Override
+ public boolean get(int bitIndex) {
+ int i = bitIndex >> 6;
+ return (i >= bits.length) ? false : (bits[i] & (1L << bitIndex)) != 0;
+ }
+
+ @Override
+ public BitSetServiceImpl get(int fromIndex, int toIndex) {
+ if (fromIndex < 0 || fromIndex > toIndex)
+ throw new IndexOutOfBoundsException();
+ BitSetServiceImpl bitSet = new BitSetServiceImpl();
+ int length = MathLib.min(bits.length, (toIndex >>> 6) + 1);
+ bitSet.bits = new long[length];
+ System.arraycopy(bits, 0, bitSet.bits, 0, length);
+ bitSet.clear(0, fromIndex);
+ bitSet.clear(toIndex, length << 6);
+ return bitSet;
+ }
+
+ /** Sets the specified bit, returns <code>true</code> if previously set. */
+ @Override
+ public boolean getAndSet(int bitIndex, boolean value) {
+ int i = bitIndex >> 6;
+ ensureCapacity(i + 1);
+ boolean previous = (bits[i] & (1L << bitIndex)) != 0;
+ if (value) {
+ bits[i] |= 1L << bitIndex;
+ } else {
+ bits[i] &= ~(1L << bitIndex);
+ }
+ trim();
+ return previous;
+ }
+
+ @Override
+ public boolean intersects(BitSetService that) {
+ long[] thatBits = that.toLongArray();
+ int i = MathLib.min(this.bits.length, thatBits.length);
+ while (--i >= 0) {
+ if ((bits[i] & thatBits[i]) != 0) return true;
+ }
+ return false;
+ }
+
+ @Override
+ public Iterator<Index> iterator() {
+ return new BitSetIteratorImpl(this, 0);
+ }
+
+ @Override
+ public int length() {
+ if (bits.length == 0) return 0;
+ return (bits.length << 6) - MathLib.numberOfLeadingZeros(bits[bits.length -1]);
+ }
+
+ @Override
+ public int nextClearBit(int fromIndex) {
+ int offset = fromIndex >> 6;
+ long mask = 1L << fromIndex;
+ while (offset < bits.length) {
+ long h = bits[offset];
+ do {
+ if ((h & mask) == 0) { return fromIndex; }
+ mask <<= 1;
+ fromIndex++;
+ } while (mask != 0);
+ mask = 1;
+ offset++;
+ }
+ return fromIndex;
+ }
+
+ @Override
+ public int nextSetBit(int fromIndex) {
+ int offset = fromIndex >> 6;
+ long mask = 1L << fromIndex;
+ while (offset < bits.length) {
+ long h = bits[offset];
+ do {
+ if ((h & mask) != 0)
+ return fromIndex;
+ mask <<= 1;
+ fromIndex++;
+ } while (mask != 0);
+ mask = 1;
+ offset++;
+ }
+ return -1;
+ }
+
+ @Override
+ public void or(BitSetService that) {
+ long[] thatBits = (that instanceof BitSetServiceImpl) ? ((BitSetServiceImpl) that).bits
+ : that.toLongArray();
+ ensureCapacity(thatBits.length);
+ for (int i = thatBits.length; --i >= 0;) {
+ bits[i] |= thatBits[i];
+ }
+ trim();
+ }
+
+ @Override
+ public int previousClearBit(int fromIndex) {
+ int offset = fromIndex >> 6;
+ long mask = 1L << fromIndex;
+ while (offset >= 0) {
+ long h = bits[offset];
+ do {
+ if ((h & mask) == 0)
+ return fromIndex;
+ mask >>= 1;
+ fromIndex--;
+ } while (mask != 0);
+ mask = 1L << 63;
+ offset--;
+ }
+ return -1;
+ }
+
+ @Override
+ public int previousSetBit(int fromIndex) {
+ int offset = fromIndex >> 6;
+ long mask = 1L << fromIndex;
+ while (offset >= 0) {
+ long h = bits[offset];
+ do {
+ if ((h & mask) != 0)
+ return fromIndex;
+ mask >>= 1;
+ fromIndex--;
+ } while (mask != 0);
+ mask = 1L << 63;
+ offset--;
+ }
+ return -1;
+ }
+
+ @Override
+ public boolean remove(Object index) {
+ return getAndSet(((Index)index).intValue(), false);
+ }
+
+ @Override
+ public void set(int bitIndex) {
+ int i = bitIndex >> 6;
+ ensureCapacity(i + 1);
+ bits[i] |= 1L << bitIndex;
+ }
+
+ @Override
+ public void set(int bitIndex, boolean value) {
+ if (value) {
+ set(bitIndex);
+ } else {
+ clear(bitIndex);
+ }
+ }
+
+ @Override
+ public void set(int fromIndex, int toIndex) {
+ int i = fromIndex >>> 6;
+ int j = toIndex >>> 6;
+ ensureCapacity(j + 1);
+ if (i == j) {
+ bits[i] |= (-1L << fromIndex) & ((1L << toIndex) - 1);
+ return;
+ }
+ bits[i] |= -1L << fromIndex;
+ bits[j] |= (1L << toIndex) - 1;
+ for (int k = i + 1; k < j; k++) {
+ bits[k] = -1;
+ }
+ }
+
+ @Override
+ public void set(int fromIndex, int toIndex, boolean value) {
+ if (value) {
+ set(fromIndex, toIndex);
+ } else {
+ clear(fromIndex, toIndex);
+ }
+ }
+
+ @Override
+ public int size() {
+ return cardinality();
+ }
+
+ @Override
+ public long[] toLongArray() {
+ return bits;
+ }
+
+ @Override
+ public void xor(BitSetService that) {
+ long[] thatBits = (that instanceof BitSetServiceImpl) ? ((BitSetServiceImpl) that).bits
+ : that.toLongArray();
+ ensureCapacity(thatBits.length);
+ for (int i = thatBits.length; --i >= 0;) {
+ bits[i] ^= thatBits[i];
+ }
+ trim();
+ }
+
+ // Checks capacity.
+ private void ensureCapacity(int capacity) {
+ if (bits.length < capacity) resize(capacity);
+ }
+
+ // Resize.
+ private void resize(int min) {
+ int newLength = bits.length * 2;
+ while (newLength < min) newLength *= 2;
+ long[] tmp = new long[newLength];
+ System.arraycopy(bits, 0, tmp, 0, bits.length);
+ bits = tmp;
+ }
+
+ /**
+ * Removes the tails words if cleared.
+ */
+ private void trim() {
+ int n = bits.length;
+ while ((--n >= 0) && (bits[n] == 0L)) {}
+ if (++n < bits.length) ensureCapacity(n);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/AtomicCollectionImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/AtomicCollectionImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/AtomicCollectionImpl.java
new file mode 100644
index 0000000..9773743
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/AtomicCollectionImpl.java
@@ -0,0 +1,194 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util.internal.collection;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+import javolution.util.function.Consumer;
+import javolution.util.function.Equality;
+import javolution.util.service.CollectionService;
+
+/**
+ * An atomic view over a collection (copy-on-write).
+ */
+public class AtomicCollectionImpl<E> extends CollectionView<E> {
+
+ /** Thread-Safe Iterator. */
+ private class IteratorImpl implements Iterator<E> {
+ private E current;
+ private final Iterator<E> targetIterator;
+
+ public IteratorImpl() {
+ targetIterator = targetView().iterator();
+ }
+
+ @Override
+ public boolean hasNext() {
+ return targetIterator.hasNext();
+ }
+
+ @Override
+ public E next() {
+ current = targetIterator.next();
+ return current;
+ }
+
+ @Override
+ public void remove() {
+ if (current == null) throw new IllegalStateException();
+ AtomicCollectionImpl.this.remove(current);
+ current = null;
+ }
+ }
+
+ private static final long serialVersionUID = 0x600L; // Version.
+ protected volatile CollectionService<E> immutable; // The copy used by readers.
+ protected transient Thread updatingThread; // The thread executing an update.
+
+ public AtomicCollectionImpl(CollectionService<E> target) {
+ super(target);
+ this.immutable = cloneTarget();
+ }
+
+ @Override
+ public synchronized boolean add(E element) {
+ boolean changed = target().add(element);
+ if (changed && !updateInProgress()) immutable = cloneTarget();
+ return changed;
+ }
+
+ @Override
+ public synchronized boolean addAll(Collection<? extends E> c) {
+ boolean changed = target().addAll(c);
+ if (changed && !updateInProgress()) immutable = cloneTarget();
+ return changed;
+ }
+
+ @Override
+ public synchronized void clear() {
+ clear();
+ if (!updateInProgress()) {
+ immutable = cloneTarget();
+ }
+ }
+
+ @Override
+ public synchronized AtomicCollectionImpl<E> clone() { // Synchronized required since working with real target.
+ AtomicCollectionImpl<E> copy = (AtomicCollectionImpl<E>) super.clone();
+ copy.updatingThread = null;
+ return copy;
+ }
+
+ @Override
+ public Equality<? super E> comparator() {
+ return immutable.comparator();
+ }
+
+ @Override
+ public boolean contains(Object o) {
+ return targetView().contains(o);
+ }
+
+ @Override
+ public boolean containsAll(Collection<?> c) {
+ return targetView().containsAll(c);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ return targetView().equals(o);
+ }
+
+ @Override
+ public int hashCode() {
+ return targetView().hashCode();
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return targetView().isEmpty();
+ }
+
+ @Override
+ public Iterator<E> iterator() {
+ return new IteratorImpl();
+ }
+
+ @Override
+ public synchronized boolean remove(Object o) {
+ boolean changed = target().remove(o);
+ if (changed && !updateInProgress()) immutable = cloneTarget();
+ return changed;
+ }
+
+ @Override
+ public synchronized boolean removeAll(Collection<?> c) {
+ boolean changed = target().removeAll(c);
+ if (changed && !updateInProgress()) immutable = cloneTarget();
+ return changed;
+ }
+
+ @Override
+ public synchronized boolean retainAll(Collection<?> c) {
+ boolean changed = target().retainAll(c);
+ if (changed && !updateInProgress()) immutable = cloneTarget();
+ return changed;
+ }
+
+ @Override
+ public int size() {
+ return targetView().size();
+ }
+
+ @Override
+ public Object[] toArray() {
+ return targetView().toArray();
+ }
+
+ @Override
+ public <T> T[] toArray(T[] a) {
+ return targetView().toArray(a);
+ }
+
+ @Override
+ public synchronized void update(Consumer<CollectionService<E>> action,
+ CollectionService<E> view) {
+ updatingThread = Thread.currentThread(); // Update in progress.
+ try {
+ target().update(action, view); // No copy performed.
+ } finally {
+ updatingThread = null;
+ immutable = cloneTarget(); // One single copy !
+ }
+ }
+
+ /** Returns a clone copy of target. */
+ protected CollectionService<E> cloneTarget() {
+ try {
+ return target().clone();
+ } catch (CloneNotSupportedException e) {
+ throw new Error("Cannot happen since target is Cloneable.");
+ }
+ }
+
+ /** Returns either the immutable target or the actual target if updating
+ * thread. */
+ protected CollectionService<E> targetView() {
+ return ((updatingThread == null) || (updatingThread != Thread.currentThread()))
+ ? immutable : target();
+ }
+
+ /** Indicates if the current thread is doing an atomic update. */
+ protected final boolean updateInProgress() {
+ return updatingThread == Thread.currentThread();
+
+ }
+}
+
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/CollectionView.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/CollectionView.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/CollectionView.java
new file mode 100644
index 0000000..c077ad7
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/CollectionView.java
@@ -0,0 +1,268 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util.internal.collection;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+import javolution.util.FastCollection;
+import javolution.util.function.Consumer;
+import javolution.util.function.Equalities;
+import javolution.util.function.Equality;
+import javolution.util.service.CollectionService;
+
+/**
+ * Collection view implementation; can be used as root class for implementations
+ * if target is {@code null}.
+ * When possible sub-classes should forward to the actual target for the methods
+ * clear, remove, contains, size and isEmpty rather than using the default
+ * implementation.
+ */
+public abstract class CollectionView<E> extends FastCollection<E> implements CollectionService<E> {
+//public abstract class CollectionView<E> implements CollectionService<E> {
+
+ private static final long serialVersionUID = 0x600L; // Version.
+ private CollectionService<E> target;
+
+ /**
+ * The view constructor or root class constructor if target is {@code null}.
+ */
+ public CollectionView(CollectionService<E> target) {
+ this.target = target;
+ }
+
+ @Override
+ public abstract boolean add(E element);
+
+ @Override
+ public boolean addAll(Collection<? extends E> c) {
+ boolean changed = false;
+ Iterator<? extends E> it = c.iterator();
+ while (it.hasNext()) {
+ if (add(it.next())) changed = true;
+ }
+ return changed;
+ }
+
+ @Override
+ public void clear() {
+ Iterator<? extends E> it = iterator();
+ while (it.hasNext()) {
+ it.next();
+ it.remove();
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public CollectionView<E> clone() {
+ try {
+ CollectionView<E> copy = (CollectionView<E>) super.clone();
+ if (target != null) { // Not a root class.
+ copy.target = target.clone();
+ }
+ return copy;
+ } catch (CloneNotSupportedException e) {
+ throw new Error("Should not happen since target is cloneable");
+ }
+ }
+
+ @Override
+ public abstract Equality<? super E> comparator();
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public boolean contains(Object obj) {
+ Iterator<? extends E> it = iterator();
+ Equality<Object> cmp = (Equality<Object>) comparator();
+ while (it.hasNext()) {
+ if (cmp.areEqual(obj, it.next())) return true;
+ }
+ return false;
+ }
+
+ @Override
+ public boolean containsAll(Collection<?> c) {
+ for (Object e : c) {
+ if (!contains(e)) return false;
+ }
+ return true;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean equals(Object o) {
+ // Follow Collection.equals specification if this comparator is standard.
+ if (this == o) return true;
+ // Check comparators consistency.
+ if (o instanceof CollectionService) {
+ if (!comparator().equals(((CollectionService<E>) o).comparator())) return false; // Different comparators.
+ } else {
+ if (!comparator().equals(Equalities.STANDARD)) return false;
+ }
+ // Collection.equals contract.
+ if (this instanceof Set) {
+ if (!(o instanceof Set)) return false;
+ Set<E> set = (Set<E>) o;
+ return (size() == set.size()) && containsAll(set);
+ } else if (this instanceof List) {
+ if (!(o instanceof List)) return false;
+ List<E> list = (List<E>) o;
+ if (size() != list.size()) return false; // Short-cut.
+ Equality<? super E> cmp = comparator();
+ Iterator<E> it1 = this.iterator();
+ Iterator<E> it2 = list.iterator();
+ while (it1.hasNext()) {
+ if (!it2.hasNext()) return false;
+ if (!cmp.areEqual(it1.next(), it2.next())) return false;
+ }
+ if (it2.hasNext()) return false;
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ // Follow Collection.equals specification if this comparator is standard.
+ Equality<? super E> cmp = comparator();
+ Iterator<E> it = this.iterator();
+ int hash = 0;
+ if (this instanceof Set) {
+ while (it.hasNext()) {
+ hash += cmp.hashCodeOf(it.next());
+ }
+ } else if (this instanceof List) {
+ while (it.hasNext()) {
+ hash += 31 * hash + cmp.hashCodeOf(it.next());
+ }
+ } else {
+ hash = super.hashCode();
+ }
+ return hash;
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return !iterator().hasNext();
+ }
+
+ @Override
+ public abstract Iterator<E> iterator();
+
+ @Override
+ public void perform(Consumer<CollectionService<E>> action, CollectionService<E> view) {
+ if (target == null) {
+ action.accept(view);
+ } else {
+ target.perform(action, view);
+ }
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public boolean remove(Object obj) {
+ Iterator<? extends E> it = iterator();
+ Equality<Object> cmp = (Equality<Object>) comparator();
+ while (it.hasNext()) {
+ if (cmp.areEqual(obj, it.next())) {
+ it.remove();
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public boolean removeAll(Collection<?> c) {
+ boolean changed = false;
+ Iterator<? extends E> it = iterator();
+ while (it.hasNext()) {
+ if (c.contains(it.next())) {
+ it.remove();
+ changed = true;
+ }
+ }
+ return changed;
+ }
+
+ @Override
+ public boolean retainAll(Collection<?> c) {
+ boolean changed = false;
+ Iterator<? extends E> it = iterator();
+ while (it.hasNext()) {
+ if (!c.contains(it.next())) {
+ it.remove();
+ changed = true;
+ }
+ }
+ return changed;
+ }
+
+ @Override
+ public int size() {
+ int count = 0;
+ Iterator<? extends E> it = iterator();
+ while (it.hasNext()) {
+ count++;
+ it.next();
+ }
+ return count;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public CollectionService<E>[] split(int n, boolean updateable) {
+ return new CollectionService[] { this }; // Split not supported.
+ }
+
+ @Override
+ public Object[] toArray() {
+ return toArray(new Object[size()]);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public <T> T[] toArray(T[] a) {
+ final int size = size();
+ final T[] result = (size <= a.length) ? a
+ : (T[]) java.lang.reflect.Array.newInstance(a.getClass()
+ .getComponentType(), size);
+ int i = 0;
+ Iterator<E> it = iterator();
+ while (it.hasNext()) {
+ result[i++] = (T) it.next();
+ }
+ if (result.length > size) {
+ result[size] = null; // As per Collection contract.
+ }
+ return result;
+ }
+
+ @Override
+ public void update(Consumer<CollectionService<E>> action, CollectionService<E> view) {
+ if (target == null) {
+ action.accept(view);
+ } else {
+ target.perform(action, view);
+ }
+ }
+
+ protected CollectionService<E> service() {
+ return this;
+ }
+
+ /** Returns the actual target */
+ protected CollectionService<E> target() {
+ return target;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/DistinctCollectionImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/DistinctCollectionImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/DistinctCollectionImpl.java
new file mode 100644
index 0000000..713dc5b
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/DistinctCollectionImpl.java
@@ -0,0 +1,102 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util.internal.collection;
+
+import java.util.Iterator;
+
+import javolution.util.FastSet;
+import javolution.util.function.Equality;
+import javolution.util.service.CollectionService;
+
+/**
+ * A view which does not iterate twice over the same elements.
+ */
+public class DistinctCollectionImpl<E> extends CollectionView<E> {
+
+ /** Peeking ahead iterator. */
+ private class IteratorImpl implements Iterator<E> {
+
+ private boolean ahead;
+ private final FastSet<E> iterated = new FastSet<E>(comparator());
+ private E next;
+ private final Iterator<E> targetIterator = target().iterator();
+
+ @Override
+ public boolean hasNext() {
+ if (ahead) return true;
+ while (targetIterator.hasNext()) {
+ next = targetIterator.next();
+ if (!iterated.contains(next)) {
+ ahead = true;
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public E next() {
+ hasNext(); // Moves ahead.
+ ahead = false;
+ return next;
+ }
+
+ @Override
+ public void remove() {
+ targetIterator.remove();
+ }
+ }
+
+ private static final long serialVersionUID = 0x600L; // Version.
+
+ public DistinctCollectionImpl(CollectionService<E> target) {
+ super(target);
+ }
+
+ @Override
+ public boolean add(E element) {
+ if (target().contains(element)) return false;
+ return target().add(element);
+ }
+
+ @Override
+ public void clear() {
+ target().clear();
+ }
+
+ @Override
+ public Equality<? super E> comparator() {
+ return target().comparator();
+ }
+
+ @Override
+ public boolean contains(Object o) {
+ return target().contains(o);
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return target().isEmpty();
+ }
+
+ @Override
+ public Iterator<E> iterator() {
+ return new IteratorImpl();
+ }
+
+ @Override
+ public boolean remove(Object o) { // Remove all instances.
+ boolean changed = false;
+ while (true) {
+ if (!remove(o)) return changed;
+ changed = true;
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/FilteredCollectionImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/FilteredCollectionImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/FilteredCollectionImpl.java
new file mode 100644
index 0000000..8f1574c
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/FilteredCollectionImpl.java
@@ -0,0 +1,111 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util.internal.collection;
+
+import java.util.Iterator;
+
+import javolution.util.function.Equality;
+import javolution.util.function.Predicate;
+import javolution.util.service.CollectionService;
+
+/**
+ * A filtered view over a collection.
+ */
+public class FilteredCollectionImpl<E> extends CollectionView<E> {
+
+ /** Peeking ahead iterator. */
+ private class IteratorImpl implements Iterator<E> {
+
+ private boolean ahead; // Indicates if the iterator is ahead (on next element)
+ private final Predicate<? super E> filter;
+ private E next;
+ private final Iterator<E> targetIterator;
+
+ public IteratorImpl(Predicate<? super E> filter) {
+ this.filter = filter;
+ targetIterator = target().iterator();
+ }
+
+ @Override
+ public boolean hasNext() {
+ if (ahead) return true;
+ while (targetIterator.hasNext()) {
+ next = targetIterator.next();
+ if (filter.test(next)) {
+ ahead = true;
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public E next() {
+ hasNext(); // Moves ahead.
+ ahead = false;
+ return next;
+ }
+
+ @Override
+ public void remove() {
+ targetIterator.remove();
+ }
+ }
+
+ private static final long serialVersionUID = 0x600L; // Version.
+ protected final Predicate<? super E> filter;
+
+ public FilteredCollectionImpl(CollectionService<E> target,
+ Predicate<? super E> filter) {
+ super(target);
+ this.filter = filter;
+ }
+
+ @Override
+ public boolean add(E element) {
+ if (!filter.test(element)) return false;
+ return target().add(element);
+ }
+
+ @Override
+ public Equality<? super E> comparator() {
+ return target().comparator();
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean contains(Object o) {
+ if (!filter.test((E) o)) return false;
+ return target().contains(o);
+ }
+
+ @Override
+ public Iterator<E> iterator() {
+ return new IteratorImpl(filter);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean remove(Object o) {
+ if (!filter.test((E) o)) return false;
+ return target().remove(o);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public CollectionService<E>[] split(int n, boolean updateable) {
+ CollectionService<E>[] subTargets = target().split(n, updateable);
+ CollectionService<E>[] result = new CollectionService[subTargets.length];
+ for (int i = 0; i < subTargets.length; i++) {
+ result[i] = new FilteredCollectionImpl<E>(subTargets[i], filter);
+ }
+ return result;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/MappedCollectionImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/MappedCollectionImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/MappedCollectionImpl.java
new file mode 100644
index 0000000..425b7b8
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/MappedCollectionImpl.java
@@ -0,0 +1,101 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util.internal.collection;
+
+import java.util.Iterator;
+
+import javolution.util.function.Equalities;
+import javolution.util.function.Equality;
+import javolution.util.function.Function;
+import javolution.util.service.CollectionService;
+
+/**
+ * A mapped view over a collection.
+ */
+public class MappedCollectionImpl<E, R> extends CollectionView<R> {
+
+ /** Mapping iterator. */
+ private class IteratorImpl implements Iterator<R> {
+ private final Iterator<E> targetIterator;
+
+ @SuppressWarnings("unchecked")
+ public IteratorImpl() {
+ targetIterator = (Iterator<E>) target().iterator();
+ }
+
+ @Override
+ public boolean hasNext() {
+ return targetIterator.hasNext();
+ }
+
+ @Override
+ public R next() {
+ return function.apply(targetIterator.next());
+ }
+
+ @Override
+ public void remove() {
+ targetIterator.remove();
+ }
+
+ }
+
+ private static final long serialVersionUID = 0x600L; // Version.
+ protected final Function<? super E, ? extends R> function;
+
+ @SuppressWarnings("unchecked")
+ public MappedCollectionImpl(CollectionService<E> target,
+ Function<? super E, ? extends R> function) {
+ super((CollectionService<R>) target); // Beware target is of type <E>
+ this.function = function;
+ }
+
+ @Override
+ public boolean add(R element) {
+ throw new UnsupportedOperationException(
+ "New elements cannot be added to mapped views");
+ }
+
+ @Override
+ public void clear() {
+ target().clear();
+ }
+
+ @Override
+ public Equality<? super R> comparator() {
+ return Equalities.STANDARD;
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return target().isEmpty();
+ }
+
+ @Override
+ public Iterator<R> iterator() {
+ return new IteratorImpl();
+ }
+
+ @Override
+ public int size() {
+ return target().size();
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public CollectionService<R>[] split(int n, boolean updateable) {
+ CollectionService<E>[] subTargets = (CollectionService<E>[]) target().split(n, updateable);
+ CollectionService<R>[] result = new CollectionService[subTargets.length];
+ for (int i = 0; i < subTargets.length; i++) {
+ result[i] = new MappedCollectionImpl<E, R>(subTargets[i], function);
+ }
+ return result;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/ParallelCollectionImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/ParallelCollectionImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/ParallelCollectionImpl.java
new file mode 100644
index 0000000..8056aed
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/ParallelCollectionImpl.java
@@ -0,0 +1,120 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util.internal.collection;
+
+import java.util.Iterator;
+
+import javolution.context.ConcurrentContext;
+import javolution.util.function.Consumer;
+import javolution.util.function.Equality;
+import javolution.util.service.CollectionService;
+
+/**
+ * A parallel view over a collection.
+ */
+public class ParallelCollectionImpl<E> extends CollectionView<E> {
+
+ private static final long serialVersionUID = 0x600L; // Version.
+
+ public ParallelCollectionImpl(CollectionService<E> target) {
+ super(target);
+ }
+
+ @Override
+ public boolean add(E e) {
+ return target().add(e);
+ }
+
+ @Override
+ public void clear() {
+ target().clear();
+ }
+
+ @Override
+ public Equality<? super E> comparator() {
+ return target().comparator();
+ }
+
+ @Override
+ public boolean contains(Object obj) {
+ return target().contains(obj);
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return target().isEmpty();
+ }
+
+ @Override
+ public Iterator<E> iterator() {
+ return target().iterator();
+ }
+
+ @Override
+ public void perform(final Consumer<CollectionService<E>> action,
+ CollectionService<E> view) {
+ ConcurrentContext ctx = ConcurrentContext.enter();
+ try {
+ int concurrency = ctx.getConcurrency();
+ CollectionService<E>[] subViews = view.split(concurrency + 1, false);
+ for (int i = 1; i < subViews.length; i++) {
+ final CollectionService<E> subView = subViews[i];
+ ctx.execute(new Runnable() {
+ @Override
+ public void run() {
+ target().perform(action, subView);
+ }
+ });
+ }
+ target().perform(action, subViews[0]); // This thread works too !
+ } finally {
+ // Any exception raised during parallel iterations will be re-raised here.
+ ctx.exit();
+ }
+ }
+
+ @Override
+ public boolean remove(Object obj) {
+ return target().remove(obj);
+ }
+
+ @Override
+ public int size() {
+ return target().size();
+ }
+
+ @Override
+ public CollectionService<E>[] split(int n, boolean threadsafe) {
+ return target().split(n, threadsafe); // Forwards.
+ }
+
+ @Override
+ public void update(final Consumer<CollectionService<E>> action,
+ CollectionService<E> view) {
+ ConcurrentContext ctx = ConcurrentContext.enter();
+ try {
+ int concurrency = ctx.getConcurrency();
+ CollectionService<E>[] subViews = view.split(concurrency + 1, true);
+ for (int i = 1; i < subViews.length; i++) {
+ final CollectionService<E> subView = subViews[i];
+ ctx.execute(new Runnable() {
+ @Override
+ public void run() {
+ target().update(action, subView);
+ }
+ });
+ }
+ target().perform(action, subViews[0]); // This thread works too !
+ } finally {
+ // Any exception raised during parallel iterations will be re-raised here.
+ ctx.exit();
+ }
+ }
+
+}