You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@marmotta.apache.org by ja...@apache.org on 2013/11/05 11:52:52 UTC
[47/52] [partial] Reverting the erroneous merge by Sebastian
according to the instructions in INFRA-6876
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/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/582abb5b/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/582abb5b/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/582abb5b/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..020c44e
--- /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 javolution.lang.Parallelizable;
+import javolution.lang.Realtime;
+import javolution.util.FastCollection;
+
+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 static javolution.lang.Realtime.Limit.LINEAR;
+
+/**
+ * <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/582abb5b/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/582abb5b/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/582abb5b/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/582abb5b/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/582abb5b/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..a76ce38
--- /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 javolution.util.function.Consumer;
+import javolution.util.function.Equality;
+import javolution.util.service.CollectionService;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+/**
+ * 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/582abb5b/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..a2ed55b
--- /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 javolution.util.FastCollection;
+import javolution.util.function.Consumer;
+import javolution.util.function.Equalities;
+import javolution.util.function.Equality;
+import javolution.util.service.CollectionService;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * 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/582abb5b/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..404e76e
--- /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 javolution.util.FastSet;
+import javolution.util.function.Equality;
+import javolution.util.service.CollectionService;
+
+import java.util.Iterator;
+
+/**
+ * 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/582abb5b/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..e64f4c5
--- /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 javolution.util.function.Equality;
+import javolution.util.function.Predicate;
+import javolution.util.service.CollectionService;
+
+import java.util.Iterator;
+
+/**
+ * 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/582abb5b/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..114bba4
--- /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 javolution.util.function.Equalities;
+import javolution.util.function.Equality;
+import javolution.util.function.Function;
+import javolution.util.service.CollectionService;
+
+import java.util.Iterator;
+
+/**
+ * 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/582abb5b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/ReversedCollectionImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/ReversedCollectionImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/ReversedCollectionImpl.java
new file mode 100644
index 0000000..edb5b1d
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/ReversedCollectionImpl.java
@@ -0,0 +1,98 @@
+/*
+ * 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 javolution.util.function.Equality;
+import javolution.util.service.CollectionService;
+
+import java.util.Iterator;
+
+/**
+ * A reversed view over a collection.
+ */
+public class ReversedCollectionImpl<E> extends CollectionView<E> {
+
+ /** Reversing Iterator. */
+ private class IteratorImpl implements Iterator<E> {
+
+ @SuppressWarnings("unchecked")
+ private final E[] elements = (E[]) new Object[size()];
+ private int index = 0;
+
+ public IteratorImpl() {
+ Iterator<E> it = target().iterator();
+ while (it.hasNext() && (index < elements.length)) {
+ elements[index++] = it.next();
+ }
+ }
+
+ @Override
+ public boolean hasNext() {
+ return index > 0;
+ }
+
+ @Override
+ public E next() {
+ return elements[--index];
+ }
+
+ @Override
+ public void remove() {
+ target().remove(elements[index]);
+ }
+
+ }
+
+ private static final long serialVersionUID = 0x600L; // Version.
+
+ public ReversedCollectionImpl(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 new IteratorImpl();
+ }
+
+ @Override
+ public boolean remove(Object obj) {
+ return target().remove(obj);
+ }
+
+ @Override
+ public int size() {
+ return target().size();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/SequentialCollectionImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/SequentialCollectionImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/SequentialCollectionImpl.java
new file mode 100644
index 0000000..0623bfe
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/SequentialCollectionImpl.java
@@ -0,0 +1,83 @@
+/*
+ * 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 javolution.util.function.Consumer;
+import javolution.util.function.Equality;
+import javolution.util.service.CollectionService;
+
+import java.util.Iterator;
+
+/**
+ * A sequential view over a collection.
+ */
+public class SequentialCollectionImpl<E> extends CollectionView<E> {
+
+ private static final long serialVersionUID = 0x600L; // Version.
+
+ public SequentialCollectionImpl(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(Consumer<CollectionService<E>> action, CollectionService<E> view) {
+ action.accept(view); // Executes immediately.
+ }
+
+ @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(Consumer<CollectionService<E>> action, CollectionService<E> view) {
+ action.accept(view); // Executes immediately.
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/SharedCollectionImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/SharedCollectionImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/SharedCollectionImpl.java
new file mode 100644
index 0000000..689fa5c
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/SharedCollectionImpl.java
@@ -0,0 +1,270 @@
+/*
+ * 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 javolution.util.function.Consumer;
+import javolution.util.function.Equality;
+import javolution.util.internal.ReadWriteLockImpl;
+import javolution.util.service.CollectionService;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+/**
+ * A shared view over a collection (reads-write locks).
+ */
+public class SharedCollectionImpl<E> extends CollectionView<E> {
+
+ /** Thread-Safe Iterator. */
+ private class IteratorImpl implements Iterator<E> { // Thread-Safe.
+ private E next;
+ private final Iterator<E> targetIterator;
+
+ public IteratorImpl() {
+ lock.readLock.lock();
+ try {
+ targetIterator = cloneTarget().iterator(); // Copy.
+ } finally {
+ lock.readLock.unlock();
+ }
+ }
+
+ @Override
+ public boolean hasNext() {
+ return targetIterator.hasNext();
+ }
+
+ @Override
+ public E next() {
+ next = targetIterator.next();
+ return next;
+ }
+
+ @Override
+ public void remove() {
+ if (next == null) throw new IllegalStateException();
+ SharedCollectionImpl.this.remove(next);
+ next = null;
+ }
+ }
+
+ private static final long serialVersionUID = 0x600L; // Version.
+ protected ReadWriteLockImpl lock;
+
+ public SharedCollectionImpl(CollectionService<E> target) {
+ this(target, new ReadWriteLockImpl());
+ }
+
+ public SharedCollectionImpl(CollectionService<E> target,
+ ReadWriteLockImpl lock) {
+ super(target);
+ this.lock = lock;
+ }
+
+ @Override
+ public boolean add(E element) {
+ lock.writeLock.lock();
+ try {
+ return target().add(element);
+ } finally {
+ lock.writeLock.unlock();
+ }
+ }
+
+ @Override
+ public boolean addAll(Collection<? extends E> c) {
+ lock.writeLock.lock();
+ try {
+ return target().addAll(c);
+ } finally {
+ lock.writeLock.unlock();
+ }
+ }
+
+ @Override
+ public void clear() {
+ lock.writeLock.lock();
+ try {
+ target().clear();
+ } finally {
+ lock.writeLock.unlock();
+ }
+ }
+
+ @Override
+ public SharedCollectionImpl<E> clone() {
+ lock.readLock.lock();
+ try {
+ SharedCollectionImpl<E> copy = (SharedCollectionImpl<E>) super
+ .clone();
+ copy.lock = new ReadWriteLockImpl(); // No need to share the same lock.
+ return copy;
+ } finally {
+ lock.readLock.unlock();
+ }
+ }
+
+ @Override
+ public Equality<? super E> comparator() {
+ return target().comparator();
+ }
+
+ @Override
+ public boolean contains(Object o) {
+ lock.readLock.lock();
+ try {
+ return target().contains(o);
+ } finally {
+ lock.readLock.unlock();
+ }
+ }
+
+ @Override
+ public boolean containsAll(Collection<?> c) {
+ lock.readLock.lock();
+ try {
+ return target().containsAll(c);
+ } finally {
+ lock.readLock.unlock();
+ }
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ lock.readLock.lock();
+ try {
+ return target().equals(o);
+ } finally {
+ lock.readLock.unlock();
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ lock.readLock.lock();
+ try {
+ return target().hashCode();
+ } finally {
+ lock.readLock.unlock();
+ }
+ }
+
+ @Override
+ public boolean isEmpty() {
+ lock.readLock.lock();
+ try {
+ return target().isEmpty();
+ } finally {
+ lock.readLock.unlock();
+ }
+ }
+
+ @Override
+ public Iterator<E> iterator() {
+ return new IteratorImpl();
+ }
+
+ @Override
+ public void perform(Consumer<CollectionService<E>> action,
+ CollectionService<E> view) {
+ lock.readLock.lock();
+ try {
+ target().perform(action, view);
+ } finally {
+ lock.readLock.unlock();
+ }
+ }
+
+ @Override
+ public boolean remove(Object o) {
+ lock.writeLock.lock();
+ try {
+ return target().remove(o);
+ } finally {
+ lock.writeLock.unlock();
+ }
+ }
+
+ @Override
+ public boolean removeAll(Collection<?> c) {
+ lock.writeLock.lock();
+ try {
+ return target().removeAll(c);
+ } finally {
+ lock.writeLock.unlock();
+ }
+ }
+
+ @Override
+ public boolean retainAll(Collection<?> c) {
+ lock.writeLock.lock();
+ try {
+ return target().retainAll(c);
+ } finally {
+ lock.writeLock.unlock();
+ }
+ }
+
+ @Override
+ public int size() {
+ lock.readLock.lock();
+ try {
+ return target().size();
+ } finally {
+ lock.readLock.unlock();
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public CollectionService<E>[] split(int n, boolean updateable) {
+ CollectionService<E>[] tmp;
+ lock.readLock.lock();
+ try {
+ tmp = target().split(n, updateable);
+ } finally {
+ lock.readLock.unlock();
+ }
+ CollectionService<E>[] result = new CollectionService[tmp.length];
+ for (int i = 0; i < tmp.length; i++) {
+ result[i] = new SharedCollectionImpl<E>(tmp[i], lock); // Shares the same locks.
+ }
+ return result;
+ }
+
+ @Override
+ public Object[] toArray() {
+ lock.readLock.lock();
+ try {
+ return target().toArray();
+ } finally {
+ lock.readLock.unlock();
+ }
+ }
+
+ @Override
+ public <T> T[] toArray(T[] a) {
+ lock.readLock.lock();
+ try {
+ return target().toArray(a);
+ } finally {
+ lock.readLock.unlock();
+ }
+ }
+
+ /** 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.");
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/SortedCollectionImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/SortedCollectionImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/SortedCollectionImpl.java
new file mode 100644
index 0000000..9c04c9c
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/SortedCollectionImpl.java
@@ -0,0 +1,109 @@
+/*
+ * 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 javolution.util.FastTable;
+import javolution.util.function.Equality;
+import javolution.util.internal.comparator.WrapperComparatorImpl;
+import javolution.util.service.CollectionService;
+
+import java.util.Comparator;
+import java.util.Iterator;
+
+/**
+ * A sorted view over a collection.
+ */
+public class SortedCollectionImpl<E> extends CollectionView<E> {
+
+ /** Sorting Iterator. */
+ private class IteratorImpl implements Iterator<E> {
+ private final Iterator<E> iterator;
+ private E next;
+
+ public IteratorImpl() {
+ FastTable<E> sorted = new FastTable<E>(comparator);
+ Iterator<E> it = target().iterator();
+ while (it.hasNext()) {
+ sorted.add(it.next());
+ }
+ sorted.sort();
+ iterator = sorted.iterator();
+ }
+
+ @Override
+ public boolean hasNext() {
+ return iterator.hasNext();
+ }
+
+ @Override
+ public E next() {
+ next = iterator.next();
+ return next;
+ }
+
+ @Override
+ public void remove() {
+ if (next == null) throw new IllegalStateException();
+ target().remove(next);
+ next = null;
+ }
+
+ }
+
+ private static final long serialVersionUID = 0x600L; // Version.
+ protected final Equality<E> comparator;
+
+ @SuppressWarnings("unchecked")
+ public SortedCollectionImpl(CollectionService<E> target, Comparator<? super E> comparator) {
+ super(target);
+ this.comparator = (comparator instanceof Equality) ?
+ (Equality<E>) comparator : new WrapperComparatorImpl<E>(comparator);
+ }
+
+ @Override
+ public boolean add(E e) {
+ return target().add(e);
+ }
+
+ @Override
+ public void clear() {
+ target().clear();
+ }
+
+ @Override
+ public Equality<? super E> comparator() {
+ return comparator;
+ }
+
+ @Override
+ public boolean contains(Object obj) {
+ return target().contains(obj);
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return target().isEmpty();
+ }
+
+ @Override
+ public Iterator<E> iterator() {
+ return new IteratorImpl();
+ }
+
+ @Override
+ public boolean remove(Object obj) {
+ return target().remove(obj);
+ }
+
+ @Override
+ public int size() {
+ return target().size();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/UnmodifiableCollectionImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/UnmodifiableCollectionImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/UnmodifiableCollectionImpl.java
new file mode 100644
index 0000000..be13365
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/collection/UnmodifiableCollectionImpl.java
@@ -0,0 +1,98 @@
+/*
+ * 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 javolution.util.function.Equality;
+import javolution.util.service.CollectionService;
+
+import java.util.Iterator;
+
+/**
+ * An unmodifiable view over a collection.
+ */
+public class UnmodifiableCollectionImpl<E> extends CollectionView<E> {
+
+ /** Read-Only Iterator. */
+ private class IteratorImpl implements Iterator<E> {
+ private final Iterator<E> targetIterator = target().iterator();
+
+ @Override
+ public boolean hasNext() {
+ return targetIterator.hasNext();
+ }
+
+ @Override
+ public E next() {
+ return targetIterator.next();
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException("Read-Only Collection.");
+ }
+ }
+
+ private static final long serialVersionUID = 0x600L; // Version.
+
+ public UnmodifiableCollectionImpl(CollectionService<E> target) {
+ super(target);
+ }
+
+ @Override
+ public boolean add(E element) {
+ throw new UnsupportedOperationException("Read-Only Collection.");
+ }
+
+ @Override
+ public void clear() {
+ throw new UnsupportedOperationException("Read-Only Collection.");
+ }
+
+ @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 new IteratorImpl();
+ }
+
+ @Override
+ public boolean remove(Object o) {
+ throw new UnsupportedOperationException("Read-Only Collection.");
+ }
+
+ @Override
+ public int size() {
+ return target().size();
+ }
+
+ @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 UnmodifiableCollectionImpl<E>(subTargets[i]);
+ }
+ return result;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/ArrayComparatorImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/ArrayComparatorImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/ArrayComparatorImpl.java
new file mode 100644
index 0000000..fc1afbf
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/ArrayComparatorImpl.java
@@ -0,0 +1,72 @@
+/*
+ * 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.comparator;
+
+import java.util.Arrays;
+
+/**
+ * The array comparator implementation.
+ */
+public class ArrayComparatorImpl extends StandardComparatorImpl<Object> {
+
+ private static final long serialVersionUID = 4134048629840904441L;
+
+ @Override
+ public boolean areEqual(Object array1, Object array2) {
+ if (array1 == array2)
+ return true;
+ if ((array1 == null) || (array2 == null))
+ return false;
+ if (array1 instanceof Object[] && array2 instanceof Object[])
+ return Arrays.deepEquals((Object[]) array1, (Object[]) array2);
+ if (array1 instanceof byte[] && array2 instanceof byte[])
+ return Arrays.equals((byte[]) array1, (byte[]) array2);
+ if (array1 instanceof short[] && array2 instanceof short[])
+ return Arrays.equals((short[]) array1, (short[]) array2);
+ if (array1 instanceof int[] && array2 instanceof int[])
+ return Arrays.equals((int[]) array1, (int[]) array2);
+ if (array1 instanceof long[] && array2 instanceof long[])
+ return Arrays.equals((long[]) array1, (long[]) array2);
+ if (array1 instanceof char[] && array2 instanceof char[])
+ return Arrays.equals((char[]) array1, (char[]) array2);
+ if (array1 instanceof float[] && array2 instanceof float[])
+ return Arrays.equals((float[]) array1, (float[]) array2);
+ if (array1 instanceof double[] && array2 instanceof double[])
+ return Arrays.equals((double[]) array1, (double[]) array2);
+ if (array1 instanceof boolean[] && array2 instanceof boolean[])
+ return Arrays.equals((boolean[]) array1, (boolean[]) array2);
+ return array1.equals(array2);
+ }
+
+ @Override
+ public int hashCodeOf(Object array) {
+ if (array instanceof Object[])
+ return Arrays.deepHashCode((Object[]) array);
+ if (array instanceof byte[])
+ return Arrays.hashCode((byte[]) array);
+ if (array instanceof short[])
+ return Arrays.hashCode((short[]) array);
+ if (array instanceof int[])
+ return Arrays.hashCode((int[]) array);
+ if (array instanceof long[])
+ return Arrays.hashCode((long[]) array);
+ if (array instanceof char[])
+ return Arrays.hashCode((char[]) array);
+ if (array instanceof float[])
+ return Arrays.hashCode((float[]) array);
+ if (array instanceof double[])
+ return Arrays.hashCode((double[]) array);
+ if (array instanceof boolean[])
+ return Arrays.hashCode((boolean[]) array);
+ if (array != null)
+ return array.hashCode();
+ return 0;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/IdentityComparatorImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/IdentityComparatorImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/IdentityComparatorImpl.java
new file mode 100644
index 0000000..bbc94ea
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/IdentityComparatorImpl.java
@@ -0,0 +1,42 @@
+/*
+ * 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.comparator;
+
+import javolution.util.function.Equality;
+
+/**
+ * The identity comparator implementation.
+ */
+public class IdentityComparatorImpl<E> implements Equality<E> {
+
+ private static final long serialVersionUID = 6576306094743751922L;
+
+ @Override
+ public boolean areEqual(E e1, E e2) {
+ return e1 == e2;
+ }
+
+ @Override
+ public int compare(E left, E right) {
+ if (left == right)
+ return 0;
+ if (left == null)
+ return -1;
+ if (right == null)
+ return 1;
+
+ // Empirical comparison.
+ return (hashCodeOf(left) < hashCodeOf(right)) ? -1 : 1;
+ }
+
+ @Override
+ public int hashCodeOf(E obj) {
+ return System.identityHashCode(obj);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/LexicalCaseInsensitiveComparatorImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/LexicalCaseInsensitiveComparatorImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/LexicalCaseInsensitiveComparatorImpl.java
new file mode 100644
index 0000000..c391d6e
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/LexicalCaseInsensitiveComparatorImpl.java
@@ -0,0 +1,73 @@
+/*
+ * 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.comparator;
+
+import javolution.util.function.Equality;
+
+/**
+ * The case insensitive lexical comparator implementation.
+ */
+public class LexicalCaseInsensitiveComparatorImpl implements
+ Equality<CharSequence> {
+
+ private static final long serialVersionUID = -1046672327934410697L;
+
+ // Converts to upper case.
+ private static char up(char c) {
+ return Character.toUpperCase(c);
+ }
+
+ @Override
+ public boolean areEqual(CharSequence csq1, CharSequence csq2) {
+ if (csq1 == csq2)
+ return true;
+ if ((csq1 == null) || (csq2 == null))
+ return false;
+ if ((csq1 instanceof String) && (csq2 instanceof String)) // Optimization.
+ return ((String) csq1).equalsIgnoreCase((String) csq2);
+ int n = csq1.length();
+ if (csq2.length() != n)
+ return false;
+ for (int i = 0; i < n;) {
+ if (up(csq1.charAt(i)) != up(csq2.charAt(i++)))
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public int compare(CharSequence left, CharSequence right) {
+ if (left == null)
+ return -1;
+ if (right == null)
+ return 1;
+ if ((left instanceof String) && (right instanceof String)) // Optimization.
+ return ((String) left).compareToIgnoreCase((String) right);
+ int i = 0;
+ int n = Math.min(left.length(), right.length());
+ while (n-- != 0) {
+ char c1 = up(left.charAt(i));
+ char c2 = up(right.charAt(i++));
+ if (c1 != c2)
+ return c1 - c2;
+ }
+ return left.length() - right.length();
+ }
+
+ @Override
+ public int hashCodeOf(CharSequence csq) {
+ if (csq == null)
+ return 0;
+ int h = 0;
+ for (int i = 0, n = csq.length(); i < n;) {
+ h = 31 * h + up(csq.charAt(i++));
+ }
+ return h;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/LexicalComparatorImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/LexicalComparatorImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/LexicalComparatorImpl.java
new file mode 100644
index 0000000..9516331
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/LexicalComparatorImpl.java
@@ -0,0 +1,76 @@
+/*
+ * 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.comparator;
+
+import javolution.lang.MathLib;
+import javolution.util.function.Equality;
+
+/**
+ * The lexical comparator implementation (optimized for String).
+ */
+public class LexicalComparatorImpl implements Equality<CharSequence> {
+
+ private static final long serialVersionUID = 7904852144917623728L;
+
+ @Override
+ public boolean areEqual(CharSequence csq1, CharSequence csq2) {
+ if (csq1 == csq2)
+ return true;
+ if ((csq1 == null) || (csq2 == null))
+ return false;
+ if (csq1 instanceof String) { // Optimization.
+ if (csq2 instanceof String)
+ return csq1.equals(csq2);
+ return ((String) csq1).contentEquals(csq2);
+ } else if (csq2 instanceof String) { return ((String) csq2)
+ .contentEquals(csq1); }
+
+ // None of the CharSequence is a String.
+ int n = csq1.length();
+ if (csq2.length() != n)
+ return false;
+ for (int i = 0; i < n;) {
+ if (csq1.charAt(i) != csq2.charAt(i++))
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public int compare(CharSequence left, CharSequence right) {
+ if (left == null)
+ return -1;
+ if (right == null)
+ return 1;
+ if ((left instanceof String) && (right instanceof String)) // Optimization.
+ return ((String) left).compareTo((String) right);
+ int i = 0;
+ int n = MathLib.min(left.length(), right.length());
+ while (n-- != 0) {
+ char c1 = left.charAt(i);
+ char c2 = right.charAt(i++);
+ if (c1 != c2)
+ return c1 - c2;
+ }
+ return left.length() - right.length();
+ }
+
+ @Override
+ public int hashCodeOf(CharSequence csq) {
+ if (csq == null)
+ return 0;
+ if (csq instanceof String) // Optimization.
+ return csq.hashCode();
+ int h = 0;
+ for (int i = 0, n = csq.length(); i < n;) {
+ h = 31 * h + csq.charAt(i++);
+ }
+ return h;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/LexicalFastComparatorImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/LexicalFastComparatorImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/LexicalFastComparatorImpl.java
new file mode 100644
index 0000000..885bb22
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/LexicalFastComparatorImpl.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.internal.comparator;
+
+/**
+ * The high-performance lexical comparator.
+ */
+public class LexicalFastComparatorImpl extends LexicalComparatorImpl {
+
+ private static final long serialVersionUID = -1449702752185594025L;
+
+ @Override
+ public int hashCodeOf(CharSequence csq) {
+ if (csq == null)
+ return 0;
+ int n = csq.length();
+ if (n == 0)
+ return 0;
+ // Hash based on 5 characters only.
+ return csq.charAt(0) + csq.charAt(n - 1) * 31 + csq.charAt(n >> 1)
+ * 1009 + csq.charAt(n >> 2) * 27583
+ + csq.charAt(n - 1 - (n >> 2)) * 73408859;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/StandardComparatorImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/StandardComparatorImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/StandardComparatorImpl.java
new file mode 100644
index 0000000..c92f98a
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/StandardComparatorImpl.java
@@ -0,0 +1,48 @@
+/*
+ * 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.comparator;
+
+import javolution.util.function.Equality;
+
+/**
+ * The standard comparator implementation.
+ */
+public class StandardComparatorImpl<E> implements Equality<E> {
+
+ private static final long serialVersionUID = -615690677813206151L;
+
+ @Override
+ public boolean areEqual(E e1, E e2) {
+ return (e1 == e2) || (e1 != null && e1.equals(e2));
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public int compare(E left, E right) {
+ if (left == right)
+ return 0;
+ if (left == null)
+ return -1;
+ if (right == null)
+ return 1;
+ if (left instanceof Comparable)
+ return ((Comparable<E>) left).compareTo(right);
+
+ // Empirical method (consistent with equals).
+ if (left.equals(right))
+ return 0;
+ return left.hashCode() < right.hashCode() ? -1 : 1;
+ }
+
+ @Override
+ public int hashCodeOf(E e) {
+ return (e == null) ? 0 : e.hashCode();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/WrapperComparatorImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/WrapperComparatorImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/WrapperComparatorImpl.java
new file mode 100644
index 0000000..e5f0b51
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/comparator/WrapperComparatorImpl.java
@@ -0,0 +1,52 @@
+/*
+ * 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.comparator;
+
+import javolution.util.function.Equality;
+
+import java.util.Comparator;
+
+/**
+ * A comparator service wrapping a {@ link Comparator}, since
+ * consistency with hashcode cannot be maintained. The hashcode
+ * calculation method throws UnsupportedOperationException.
+ */
+public final class WrapperComparatorImpl<E> implements Equality<E> {
+
+ private static final long serialVersionUID = 8775282553794347279L;
+ private final Comparator<? super E> comparator;
+
+ public WrapperComparatorImpl(Comparator<? super E> comparator) {
+ this.comparator = comparator;
+ }
+
+ @Override
+ public boolean areEqual(E e1, E e2) {
+ return (e1 == e2) || (e1 != null && (comparator.compare(e1, e2) == 0));
+ }
+
+ @Override
+ public int compare(E left, E right) {
+ if (left == right)
+ return 0;
+ if (left == null)
+ return -1;
+ if (right == null)
+ return 1;
+ return comparator.compare(left, right);
+ }
+
+ @Override
+ public int hashCodeOf(E obj) {
+ throw new UnsupportedOperationException(
+ "Standard comparator (java.util.Comparator) cannot be used for "
+ + "hashcode calculations; please use a coherent equality comparator "
+ + "instead (e.g. javolution.util.function.Equality).");
+ }
+}