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:00 UTC
[08/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/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..4eef132
--- /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 java.util.Iterator;
+
+import javolution.util.function.Equality;
+import javolution.util.service.CollectionService;
+
+/**
+ * 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/e43574ef/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..5b7ff6f
--- /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 java.util.Iterator;
+
+import javolution.util.function.Consumer;
+import javolution.util.function.Equality;
+import javolution.util.service.CollectionService;
+
+/**
+ * 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/e43574ef/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..74ff05c
--- /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 java.util.Collection;
+import java.util.Iterator;
+
+import javolution.util.function.Consumer;
+import javolution.util.function.Equality;
+import javolution.util.internal.ReadWriteLockImpl;
+import javolution.util.service.CollectionService;
+
+/**
+ * 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/e43574ef/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..6f35d53
--- /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 java.util.Comparator;
+import java.util.Iterator;
+
+import javolution.util.FastTable;
+import javolution.util.function.Equality;
+import javolution.util.internal.comparator.WrapperComparatorImpl;
+import javolution.util.service.CollectionService;
+
+/**
+ * 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/e43574ef/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..d6b0481
--- /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 java.util.Iterator;
+
+import javolution.util.function.Equality;
+import javolution.util.service.CollectionService;
+
+/**
+ * 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/e43574ef/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/e43574ef/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/e43574ef/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/e43574ef/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/e43574ef/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/e43574ef/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/e43574ef/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..5578107
--- /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 java.util.Comparator;
+
+import javolution.util.function.Equality;
+
+/**
+ * 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).");
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/AtomicMapImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/AtomicMapImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/AtomicMapImpl.java
new file mode 100644
index 0000000..988b677
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/AtomicMapImpl.java
@@ -0,0 +1,192 @@
+/*
+ * 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.map;
+
+import java.util.Iterator;
+import java.util.Map;
+
+import javolution.util.function.Consumer;
+import javolution.util.function.Equality;
+import javolution.util.service.MapService;
+
+/**
+ * An atomic view over a map (copy-on-write).
+ */
+public class AtomicMapImpl<K, V> extends MapView<K, V> {
+
+ /** Thread-Safe Iterator. */
+ private class IteratorImpl implements Iterator<Entry<K, V>> {
+ private Entry<K, V> current;
+ private final Iterator<Entry<K, V>> targetIterator = targetView().iterator();
+
+ @Override
+ public boolean hasNext() {
+ return targetIterator.hasNext();
+ }
+
+ @Override
+ public Entry<K, V> next() {
+ current = targetIterator.next();
+ return current;
+ }
+
+ @Override
+ public void remove() {
+ if (current == null) throw new IllegalStateException();
+ AtomicMapImpl.this.remove(current.getKey());
+ current = null;
+ }
+ }
+
+ private static final long serialVersionUID = 0x600L; // Version.
+ protected volatile MapService<K, V> immutable; // The copy used by readers.
+ protected transient Thread updatingThread; // The thread executing an update.
+
+ public AtomicMapImpl(MapService<K, V> target) {
+ super(target);
+ this.immutable = cloneTarget();
+ }
+
+ @Override
+ public synchronized void clear() {
+ clear();
+ if (!updateInProgress()) {
+ immutable = cloneTarget();
+ }
+ }
+
+ @Override
+ public synchronized AtomicMapImpl<K, V> clone() {
+ AtomicMapImpl<K, V> copy = (AtomicMapImpl<K, V>) super.clone();
+ copy.updatingThread = null;
+ return copy;
+ }
+
+ @Override
+ public boolean containsKey(Object key) {
+ return targetView().containsKey(key);
+ }
+
+ @Override
+ public boolean containsValue(Object value) {
+ return targetView().containsValue(value);
+ }
+
+ @Override
+ public V get(Object key) {
+ return targetView().get(key);
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return targetView().isEmpty();
+ }
+
+ @Override
+ public Iterator<Entry<K, V>> iterator() {
+ return new IteratorImpl();
+ }
+
+ @Override
+ public Equality<? super K> keyComparator() {
+ return targetView().keyComparator();
+ }
+
+ @Override
+ public synchronized V put(K key, V value) {
+ V v = target().put(key, value);
+ if (!updateInProgress()) immutable = cloneTarget();
+ return v;
+ }
+
+ @Override
+ public synchronized void putAll(Map<? extends K, ? extends V> m) {
+ target().putAll(m);
+ if (!updateInProgress()) immutable = cloneTarget();
+ }
+
+ @Override
+ public synchronized V putIfAbsent(K key, V value) {
+ V v = target().putIfAbsent(key, value);
+ if (!updateInProgress()) immutable = cloneTarget();
+ return v;
+ }
+
+ @Override
+ public synchronized V remove(Object key) {
+ V v = target().remove(key);
+ if (!updateInProgress()) immutable = cloneTarget();
+ return v;
+ }
+
+ @Override
+ public synchronized boolean remove(Object key, Object value) {
+ boolean changed = target().remove(key, value);
+ if (changed && !updateInProgress()) immutable = cloneTarget();
+ return changed;
+ }
+
+ @Override
+ public synchronized V replace(K key, V value) {
+ V v = target().replace(key, value);
+ if (!updateInProgress()) immutable = cloneTarget();
+ return v;
+ }
+
+ @Override
+ public synchronized boolean replace(K key, V oldValue, V newValue) {
+ boolean changed = target().replace(key, oldValue, newValue);
+ if (changed && !updateInProgress()) immutable = cloneTarget();
+ return changed;
+ }
+
+ @Override
+ public int size() {
+ return targetView().size();
+ }
+
+ @Override
+ public synchronized void update(Consumer<MapService<K, V>> action,
+ MapService<K, V> view) {
+ updatingThread = Thread.currentThread(); // Update in progress.
+ try {
+ target().update(action, view); // No copy performed.
+ } finally {
+ updatingThread = null;
+ immutable = cloneTarget(); // One single copy !
+ }
+ }
+
+ @Override
+ public Equality<? super V> valueComparator() {
+ return targetView().valueComparator();
+ }
+
+ /** Returns a clone copy of target. */
+ protected MapService<K, V> 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 MapService<K, V> 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/map/FastMapImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/FastMapImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/FastMapImpl.java
new file mode 100644
index 0000000..7676647
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/FastMapImpl.java
@@ -0,0 +1,250 @@
+/*
+ * 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.map;
+
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+
+import javolution.util.function.Equality;
+
+/**
+ * The default {@link javolution.util.FastMap FastMap} implementation
+ * based on {@link FractalMapImpl fractal maps}.
+ * This implementation ensures that no more than 3/4 of the map capacity is
+ * ever wasted.
+ */
+public class FastMapImpl<K, V> extends MapView<K, V> {
+
+ private static final long serialVersionUID = 0x600L; // Version.
+ transient MapEntryImpl<K, V> firstEntry = null;
+ transient FractalMapImpl fractal = new FractalMapImpl();
+ transient MapEntryImpl<K, V> freeEntry = new MapEntryImpl<K, V>();
+ final Equality<? super K> keyComparator;
+ transient MapEntryImpl<K, V> lastEntry = null;
+ transient int size;
+ final Equality<? super V> valueComparator;
+
+ public FastMapImpl(Equality<? super K> keyComparator,
+ final Equality<? super V> valueComparator) {
+ super(null); // Root.
+ this.keyComparator = keyComparator;
+ this.valueComparator = valueComparator;
+ }
+
+ @Override
+ public void clear() {
+ firstEntry = null;
+ lastEntry = null;
+ fractal = new FractalMapImpl();
+ size = 0;
+ }
+
+ @Override
+ public FastMapImpl<K, V> clone() { // Makes a copy.
+ FastMapImpl<K, V> copy = new FastMapImpl<K, V>(keyComparator(),
+ valueComparator());
+ copy.putAll(this);
+ return copy;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean containsKey(Object key) {
+ return fractal.getEntry(key, keyComparator.hashCodeOf((K) key), keyComparator) != null;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public V get(Object key) {
+ MapEntryImpl<K, V> entry = fractal.getEntry(key,
+ keyComparator.hashCodeOf((K) key), keyComparator);
+ if (entry == null) return null;
+ return entry.value;
+ }
+
+ @Override
+ public Iterator<Entry<K, V>> iterator() {
+ return new Iterator<Entry<K, V>>() {
+ MapEntryImpl<K, V> current;
+ MapEntryImpl<K, V> next = firstEntry;
+
+ @Override
+ public boolean hasNext() {
+ return (next != null);
+ }
+
+ @Override
+ public java.util.Map.Entry<K, V> next() {
+ if (next == null) throw new NoSuchElementException();
+ current = next;
+ next = next.next;
+ return current;
+ }
+
+ @Override
+ public void remove() {
+ if (current == null) throw new IllegalStateException();
+ fractal.removeEntry(current.key, current.hash, keyComparator);
+ detachEntry(current); // Entry is not referenced anymore and will be gc.
+ size--;
+ }
+ };
+
+ }
+
+ @Override
+ public Equality<? super K> keyComparator() {
+ return keyComparator;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public V put(K key, V value) {
+ int hash = keyComparator.hashCodeOf(key);
+ MapEntryImpl<K, V> tmp = fractal.addEntry(freeEntry, key, hash, keyComparator);
+ if (tmp == freeEntry) { // New entry.
+ freeEntry = new MapEntryImpl<K, V>();
+ attachEntry(tmp);
+ size++;
+ tmp.value = value;
+ return null;
+ } else { // Existing entry.
+ V oldValue = (V) tmp.value;
+ tmp.value = value;
+ return oldValue;
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public V putIfAbsent(K key, V value) {
+ int hash = keyComparator.hashCodeOf(key);
+ MapEntryImpl<K, V> tmp = fractal.addEntry(freeEntry, key, hash, keyComparator);
+ if (tmp == freeEntry) { // New entry.
+ freeEntry = new MapEntryImpl<K, V>();
+ attachEntry(tmp);
+ size++;
+ tmp.value = value;
+ return null;
+ } else { // Existing entry.
+ return (V) tmp.value;
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public V remove(Object key) {
+ MapEntryImpl<K, V> entry = fractal.removeEntry(key,
+ keyComparator.hashCodeOf((K) key), keyComparator);
+ if (entry == null) return null;
+ detachEntry(entry); // Entry is not referenced anymore and will be gc.
+ size--;
+ return entry.value;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean remove(Object key, Object value) {
+ int hash = keyComparator.hashCodeOf((K) key);
+ MapEntryImpl<K, V> entry = fractal.getEntry(key, hash, keyComparator);
+ if (entry == null) return false;
+ if (!valueComparator.areEqual((V) entry.value, (V) value)) return false;
+ fractal.removeEntry(key, hash, keyComparator);
+ detachEntry(entry); // Entry is not referenced anymore and will be gc.
+ size--;
+ return true;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public V replace(K key, V value) {
+ MapEntryImpl<K, V> entry = fractal.getEntry(key,
+ keyComparator.hashCodeOf(key), keyComparator);
+ if (entry == null) return null;
+ V oldValue = entry.value;
+ entry.value = value;
+ return oldValue;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean replace(K key, V oldValue, V newValue) {
+ MapEntryImpl<K, V> entry = fractal.getEntry(key,
+ keyComparator.hashCodeOf(key), keyComparator);
+ if (entry == null) return false;
+ if (!valueComparator.areEqual(entry.value, oldValue)) return false;
+ entry.value = newValue;
+ return true;
+ }
+
+ @Override
+ public int size() {
+ return size;
+ }
+
+ @Override
+ public Equality<? super V> valueComparator() {
+ return valueComparator;
+ }
+
+ private void attachEntry(MapEntryImpl<K, V> entry) {
+ if (lastEntry != null) {
+ lastEntry.next = entry;
+ entry.previous = lastEntry;
+ }
+ lastEntry = entry;
+ if (firstEntry == null) {
+ firstEntry = entry;
+ }
+ }
+
+ private void detachEntry(MapEntryImpl<K, V> entry) {
+ if (entry == firstEntry) {
+ firstEntry = entry.next;
+ }
+ if (entry == lastEntry) {
+ lastEntry = entry.previous;
+ }
+ MapEntryImpl<K, V> previous = entry.previous;
+ MapEntryImpl<K, V> next = entry.next;
+ if (previous != null) {
+ previous.next = next;
+ }
+ if (next != null) {
+ next.previous = previous;
+ }
+ }
+
+ /** For serialization support */
+ @SuppressWarnings("unchecked")
+ private void readObject(java.io.ObjectInputStream s)
+ throws java.io.IOException, ClassNotFoundException {
+ s.defaultReadObject(); // Deserialize comparator.
+ fractal = new FractalMapImpl();
+ freeEntry = new MapEntryImpl<K, V>();
+ int n = s.readInt();
+ for (int i = 0; i < n; i++) {
+ put((K) s.readObject(), (V) s.readObject());
+ }
+ }
+
+ /** For serialization support */
+ private void writeObject(java.io.ObjectOutputStream s)
+ throws java.io.IOException {
+ s.defaultWriteObject(); // Serialize comparators.
+ s.writeInt(size);
+ Iterator<Entry<K, V>> it = iterator();
+ while (it.hasNext()) {
+ Entry<K, V> e = it.next();
+ s.writeObject(e.getKey());
+ s.writeObject(e.getValue());
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/FractalMapImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/FractalMapImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/FractalMapImpl.java
new file mode 100644
index 0000000..7159b41
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/FractalMapImpl.java
@@ -0,0 +1,121 @@
+/*
+ * 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.map;
+
+import javolution.util.function.Equality;
+
+/**
+ * A fractal-based map with rehash performed only on limited size maps.
+ * It is based on a fractal structure with self-similar patterns at any scale
+ * (maps holding submaps). At each depth only a part of the hashcode is used
+ * starting by the last bits.
+ */
+@SuppressWarnings("rawtypes")
+final class FractalMapImpl {
+
+ static final int EMPTINESS_LEVEL = 2; // Can be 1 (load factor 0.5), 2 (load factor 0.25) or any greater value.
+ static final int INITIAL_BLOCK_CAPACITY = 2 << EMPTINESS_LEVEL;
+ static final int SHIFT = 10; // Number of hashcode bits per depth.
+ //private static final int MAX_BLOCK_CAPACITY = 1 << SHIFT;
+ private int count; // Number of entries different from null in this block.
+ private MapEntryImpl[] entries = new MapEntryImpl[INITIAL_BLOCK_CAPACITY]; // Entries value can be a sub-map.
+ private final int shift; // Zero if base map.
+
+ public FractalMapImpl() {
+ this.shift = 0;
+ }
+
+ public FractalMapImpl(int shift) {
+ this.shift = shift;
+ }
+
+ /** Adds the specified entry if not already present; returns
+ * either the specified entry or an existing entry for the specified key. **/
+ @SuppressWarnings("unchecked")
+ public MapEntryImpl addEntry(MapEntryImpl newEntry, Object key, int hash, Equality comparator) {
+ int i = indexOfKey(key, hash, comparator);
+ MapEntryImpl entry = entries[i];
+ if (entry != null) return entry; // Entry exists
+ entries[i] = newEntry;
+ newEntry.key = key;
+ newEntry.hash = hash;
+ // Check if we need to resize.
+ if ((++count << EMPTINESS_LEVEL) > entries.length) {
+ resize(entries.length << 1);
+ }
+ return newEntry;
+ }
+
+ public void clear() {
+ entries = new MapEntryImpl[INITIAL_BLOCK_CAPACITY];
+ count = 0;
+ }
+
+ /** Returns null if no entry with specified key */
+ public MapEntryImpl getEntry(Object key, int hash, Equality comparator) {
+ return entries[indexOfKey(key, hash, comparator)];
+ }
+
+ /** Returns the entry removed or null if none. */
+ public MapEntryImpl removeEntry(Object key, int hash, Equality comparator) {
+ int i = indexOfKey(key, hash, comparator);
+ MapEntryImpl oldEntry = entries[i];
+ if (oldEntry == null) return null; // Entry does not exist.
+ entries[i] = null;
+ // Since we have made a hole, adjacent keys might have to shift.
+ for (;;) {
+ // We use a step of 1 (improve caching through memory locality).
+ i = (i + 1) & (entries.length - 1);
+ MapEntryImpl entry = entries[i];
+ if (entry == null) break; // Done.
+ int correctIndex = indexOfKey(entry.key, entry.hash, comparator);
+ if (correctIndex != i) { // Misplaced.
+ entries[correctIndex] = entries[i];
+ entries[i] = null;
+ }
+ }
+ // Check if we need to resize.
+ if (((--count << (EMPTINESS_LEVEL + 1)) <= entries.length)
+ && (entries.length > INITIAL_BLOCK_CAPACITY)) {
+ resize(entries.length >> 1);
+ }
+ return oldEntry;
+ }
+
+ /** Returns the index of the specified key in the map
+ (points to a null key if key not present). */
+ private int indexOfKey(Object key, int hash, Equality comparator) {
+ int mask = entries.length - 1;
+ int i = (hash >> shift) & mask;
+ while (true) {
+ MapEntryImpl entry = entries[i];
+ if (entry == null) return i;
+ if ((entry.hash == hash) && comparator.areEqual(key,entry.key)) return i;
+ i = (i + 1) & mask;
+ }
+ }
+
+ // The capacity is a power of two such as:
+ // (count * 2**EMPTINESS_LEVEL) <= capacity < (count * 2**(EMPTINESS_LEVEL+1))
+ // TODO: Use submaps if max capacity reached.
+ private void resize(int newCapacity) {
+ MapEntryImpl[] newEntries = new MapEntryImpl[newCapacity];
+ int newMask = newEntries.length - 1;
+ for (int i = 0, n = entries.length; i < n; i++) {
+ MapEntryImpl entry = entries[i];
+ if (entry == null) continue;
+ int newIndex = entry.hash & newMask;
+ while (newEntries[newIndex] != null) { // Find empty slot.
+ newIndex = (newIndex + 1) & newMask;
+ }
+ newEntries[newIndex] = entry;
+ }
+ entries = newEntries;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/MapEntryImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/MapEntryImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/MapEntryImpl.java
new file mode 100644
index 0000000..2eddac9
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/MapEntryImpl.java
@@ -0,0 +1,46 @@
+/*
+ * 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.map;
+
+import java.util.Map;
+
+/**
+ * The hash map entry implementation (not serializable).
+ */
+public final class MapEntryImpl<K, V> implements Map.Entry<K, V> {
+
+ int hash;
+ K key;
+ MapEntryImpl<K, V> next;
+ MapEntryImpl<K, V> previous;
+ V value;
+
+ @Override
+ public K getKey() {
+ return key;
+ }
+
+ @Override
+ public V getValue() {
+ return value;
+ }
+
+ @Override
+ public V setValue(V value) {
+ V oldValue = this.value;
+ this.value = value;
+ return oldValue;
+ }
+
+ @Override
+ public String toString() {
+ return key + "=" + value;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/MapView.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/MapView.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/MapView.java
new file mode 100644
index 0000000..6ec0238
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/MapView.java
@@ -0,0 +1,363 @@
+/*
+ * 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.map;
+
+import java.io.Serializable;
+import java.util.Iterator;
+import java.util.Map;
+
+import javolution.util.function.Consumer;
+import javolution.util.function.Equality;
+import javolution.util.function.Function;
+import javolution.util.internal.collection.MappedCollectionImpl;
+import javolution.util.internal.set.MappedSetImpl;
+import javolution.util.internal.set.SetView;
+import javolution.util.service.CollectionService;
+import javolution.util.service.MapService;
+import javolution.util.service.SetService;
+
+/**
+ * Map 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
+ * isEmpty, size and clear rather than using the default implementation.
+ */
+public abstract class MapView<K, V> implements MapService<K, V> {
+
+ /**
+ * Entry comparator. Entries are considered equals if they have the same
+ * keys regardless of their associated values.
+ */
+ protected class EntryComparator implements Equality<Entry<K,V>>, Serializable {
+ private static final long serialVersionUID = MapView.serialVersionUID;
+
+ public EntryComparator() {
+ }
+
+ @Override
+ public boolean areEqual(Entry<K, V> left, Entry<K, V> right) {
+ return keyComparator().areEqual(left.getKey(),
+ right.getKey());
+ }
+
+ @Override
+ public int compare(Entry<K, V> left, Entry<K, V> right) {
+ return keyComparator().compare(left.getKey(),
+ right.getKey());
+ }
+
+ @Override
+ public int hashCodeOf(Entry<K, V> e) {
+ return keyComparator().hashCodeOf(e.getKey());
+ }
+ }
+
+ /** Entry Set View */
+ protected class EntrySet extends SetView<Entry<K, V>> {
+ private static final long serialVersionUID = MapView.serialVersionUID;
+ public EntrySet() {
+ super(null); // Actual target is the outer map.
+ }
+
+ @Override
+ public boolean add(Entry<K, V> entry) {
+ put(entry.getKey(), entry.getValue());
+ return true;
+ }
+
+ @Override
+ public Equality<? super Entry<K, V>> comparator() {
+ return new EntryComparator();
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean contains(Object obj) {
+ if (obj instanceof Entry) {
+ Entry<K, V> e = (Entry<K, V>) obj;
+ return contains(e.getKey());
+ }
+ return false;
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return MapView.this.isEmpty();
+ }
+
+ @Override
+ public Iterator<Entry<K, V>> iterator() {
+ return MapView.this.iterator();
+ }
+
+ @Override
+ public void perform(
+ final Consumer<CollectionService<Entry<K, V>>> action,
+ final CollectionService<Entry<K, V>> view) {
+ Consumer<MapService<K, V>> mapAction = new Consumer<MapService<K, V>>() {
+ @Override
+ public void accept(MapService<K, V> param) {
+ action.accept(view);
+ }
+ };
+ MapView.this.perform(mapAction, MapView.this);
+ }
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public boolean remove(Object obj) {
+ if (obj instanceof Entry) {
+ Entry<K, V> e = (Entry<K, V>) obj;
+ if (!contains(e.getKey())) return false;
+ MapView.this.remove(e.getKey());
+ return true;
+ }
+ return false;
+ }
+
+ @Override
+ public int size() {
+ return MapView.this.size();
+ }
+
+ @Override
+ public void update(
+ final Consumer<CollectionService<Entry<K, V>>> action,
+ final CollectionService<Entry<K, V>> view) {
+ Consumer<MapService<K, V>> mapAction = new Consumer<MapService<K, V>>() {
+ @Override
+ public void accept(MapService<K, V> param) {
+ action.accept(view);
+ }
+ };
+ MapView.this.update(mapAction, MapView.this);
+ }
+ }
+
+ /** Entry to key mapping function */
+ class EntryToKey implements Function<Entry<K, V>, K>, Serializable {
+ private static final long serialVersionUID = MapView.serialVersionUID;
+ @Override
+ public K apply(java.util.Map.Entry<K, V> param) {
+ return param.getKey();
+ }
+ }
+
+ /** Key Set View */
+ protected class KeySet extends MappedSetImpl<Entry<K, V>, K> {
+ private static final long serialVersionUID = MapView.serialVersionUID;
+
+ public KeySet() {
+ super(entrySet(), new EntryToKey());
+ }
+
+ @Override
+ public boolean add(K key) { // Supports adding new key with null value.
+ if (containsKey(key)) return false;
+ put(key, null);
+ return true;
+ }
+
+ @Override
+ public Equality<? super K> comparator() {
+ return keyComparator();
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean contains(Object obj) {
+ return containsKey((K) obj);
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public boolean remove(Object obj) {
+ if (!containsKey((K) obj)) return false;
+ MapView.this.remove((K) obj);
+ return true;
+ }
+ }
+
+ /** Values View */
+ protected class Values extends MappedCollectionImpl<Entry<K, V>, V> {
+ private static final long serialVersionUID = MapView.serialVersionUID;
+
+ public Values() {
+ super(entrySet(), new Function<Entry<K, V>, V>() {
+ @Override
+ public V apply(Map.Entry<K, V> e) {
+ return e.getValue();
+ }
+ });
+ }
+
+ @Override
+ public Equality<? super V> comparator() {
+ return valueComparator();
+ }
+ }
+
+ private static final long serialVersionUID = 0x600L; // Version.
+ private MapService<K, V> target;
+
+ /**
+ * The view constructor or root class constructor if target is {@code null}.
+ */
+ public MapView(MapService<K, V> target) {
+ this.target = target;
+ }
+
+ @Override
+ public void clear() {
+ Iterator<Entry<K, V>> it = iterator();
+ while (it.hasNext()) {
+ it.remove();
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public MapView<K, V> clone() {
+ try {
+ MapView<K, V> copy = (MapView<K, V>) 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 boolean containsKey(Object key);
+
+ @Override
+ public boolean containsValue(Object value) {
+ return values().contains(value);
+ }
+
+ @Override
+ public SetService<Entry<K, V>> entrySet() {
+ return new EntrySet();
+ }
+
+ @Override
+ public abstract V get(Object key);
+
+ @Override
+ public boolean isEmpty() {
+ return !iterator().hasNext();
+ }
+
+ @Override
+ public abstract Iterator<Entry<K, V>> iterator();
+
+ @Override
+ public abstract Equality<? super K> keyComparator();
+
+ @Override
+ public SetService<K> keySet() {
+ return new KeySet();
+ }
+
+ @Override
+ public void perform(Consumer<MapService<K, V>> action, MapService<K, V> view) {
+ if (target == null) {
+ action.accept(view);
+ } else {
+ target.perform(action, view);
+ }
+ }
+
+ @Override
+ public abstract V put(K key, V value);
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public void putAll(Map<? extends K, ? extends V> m) {
+ Iterator<?> it = m.entrySet().iterator();
+ while (it.hasNext()) {
+ Entry<K, V> e = (Entry<K, V>) it.next();
+ put(e.getKey(), e.getValue());
+ }
+ }
+
+ @Override
+ public V putIfAbsent(K key, V value) {
+ if (!containsKey(key)) return put(key, value);
+ else return get(key);
+ }
+
+ @Override
+ public abstract V remove(Object key);
+
+ @Override
+ public boolean remove(Object key, Object value) {
+ if (containsKey(key) && get(key).equals(value)) {
+ remove(key);
+ return true;
+ } else return false;
+ }
+
+ @Override
+ public V replace(K key, V value) {
+ if (containsKey(key)) {
+ return put(key, value);
+ } else return null;
+ }
+
+ @Override
+ public boolean replace(K key, V oldValue, V newValue) {
+ if (containsKey(key) && get(key).equals(oldValue)) {
+ put(key, newValue);
+ return true;
+ } else return false;
+ }
+
+ @Override
+ public int size() {
+ int count = 0;
+ Iterator<Entry<K, V>> it = iterator();
+ while (it.hasNext()) {
+ count++;
+ it.next();
+ }
+ return count;
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public MapService<K, V>[] split(int n, boolean threadsafe) {
+ return new MapService[] { this }; // Splits not supported.
+ }
+
+ @Override
+ public void update(Consumer<MapService<K, V>> action, MapService<K, V> view) {
+ if (target == null) {
+ action.accept(view);
+ } else {
+ target.update(action, view);
+ }
+ }
+
+ @Override
+ public abstract Equality<? super V> valueComparator();
+
+ @Override
+ public CollectionService<V> values() {
+ return new Values();
+ }
+
+ /** Returns the actual target */
+ protected MapService<K, V> target() {
+ return target;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/ParallelMapImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/ParallelMapImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/ParallelMapImpl.java
new file mode 100644
index 0000000..21d079e
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/ParallelMapImpl.java
@@ -0,0 +1,130 @@
+/*
+ * 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.map;
+
+import java.util.Iterator;
+
+import javolution.context.ConcurrentContext;
+import javolution.util.function.Consumer;
+import javolution.util.function.Equality;
+import javolution.util.service.MapService;
+
+/**
+ * A parallel view over a map.
+ */
+public class ParallelMapImpl<K, V> extends MapView<K, V> {
+
+ private static final long serialVersionUID = 0x600L; // Version.
+
+ public ParallelMapImpl(MapService<K, V> target) {
+ super(target);
+ }
+
+ @Override
+ public void clear() {
+ target().clear();
+ }
+
+ @Override
+ public boolean containsKey(Object key) {
+ return target().containsKey(key);
+ }
+
+ @Override
+ public V get(Object key) {
+ return target().get(key);
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return target().isEmpty();
+ }
+
+ @Override
+ public Iterator<java.util.Map.Entry<K, V>> iterator() {
+ return target().iterator();
+ }
+
+ @Override
+ public Equality<? super K> keyComparator() {
+ return target().keyComparator();
+ }
+
+ @Override
+ public void perform(final Consumer<MapService<K, V>> action,
+ MapService<K, V> view) {
+ ConcurrentContext ctx = ConcurrentContext.enter();
+ try {
+ int concurrency = ctx.getConcurrency();
+ MapService<K, V>[] subViews = view.split(concurrency + 1, false);
+ for (int i = 1; i < subViews.length; i++) {
+ final MapService<K, V> 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 V put(K key, V value) {
+ return target().put(key, value);
+ }
+
+ @Override
+ public V remove(Object key) {
+ return target().remove(key);
+ }
+
+ @Override
+ public int size() {
+ return target().size();
+ }
+
+ @Override
+ public void update(final Consumer<MapService<K, V>> action,
+ MapService<K, V> view) {
+ ConcurrentContext ctx = ConcurrentContext.enter();
+ try {
+ int concurrency = ctx.getConcurrency();
+ MapService<K, V>[] subViews = view.split(concurrency + 1, true);
+ for (int i = 1; i < subViews.length; i++) {
+ final MapService<K, V> 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();
+ }
+ }
+
+ @Override
+ public Equality<? super V> valueComparator() {
+ return target().valueComparator();
+ }
+
+ @Override
+ public MapService<K, V>[] split(int n, boolean threadsafe) {
+ return target().split(n, threadsafe); // Forwards.
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/SequentialMapImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/SequentialMapImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/SequentialMapImpl.java
new file mode 100644
index 0000000..114a884
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/SequentialMapImpl.java
@@ -0,0 +1,93 @@
+/*
+ * 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.map;
+
+import java.util.Iterator;
+
+import javolution.util.function.Consumer;
+import javolution.util.function.Equality;
+import javolution.util.service.MapService;
+
+/**
+ * A sequential view over a map.
+ */
+public class SequentialMapImpl<K, V> extends MapView<K, V> {
+
+ private static final long serialVersionUID = 0x600L; // Version.
+
+ public SequentialMapImpl(MapService<K, V> target) {
+ super(target);
+ }
+
+ @Override
+ public void clear() {
+ target().clear();
+ }
+
+ @Override
+ public boolean containsKey(Object key) {
+ return target().containsKey(key);
+ }
+
+ @Override
+ public V get(Object key) {
+ return target().get(key);
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return target().isEmpty();
+ }
+
+ @Override
+ public Iterator<java.util.Map.Entry<K, V>> iterator() {
+ return target().iterator();
+ }
+
+ @Override
+ public Equality<? super K> keyComparator() {
+ return target().keyComparator();
+ }
+
+ @Override
+ public void perform(Consumer<MapService<K, V>> action, MapService<K, V> view) {
+ action.accept(view); // Executes immediately.
+ }
+
+ @Override
+ public V put(K key, V value) {
+ return target().put(key, value);
+ }
+
+ @Override
+ public V remove(Object key) {
+ return target().remove(key);
+ }
+
+ @Override
+ public int size() {
+ return target().size();
+ }
+
+ @Override
+ public void update(Consumer<MapService<K, V>> action, MapService<K, V> view) {
+ action.accept(view); // Executes immediately.
+ }
+
+ @Override
+ public Equality<? super V> valueComparator() {
+ return target().valueComparator();
+ }
+
+ @Override
+ public MapService<K, V>[] split(int n, boolean threadsafe) {
+ return target().split(n, threadsafe); // Forwards.
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/SharedMapImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/SharedMapImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/SharedMapImpl.java
new file mode 100644
index 0000000..833e728
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/SharedMapImpl.java
@@ -0,0 +1,240 @@
+/*
+ * 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.map;
+
+import java.util.Iterator;
+import java.util.Map;
+
+import javolution.util.function.Equality;
+import javolution.util.internal.ReadWriteLockImpl;
+import javolution.util.service.MapService;
+
+/**
+ * A shared view over a map.
+ */
+public class SharedMapImpl<K, V> extends MapView<K, V> {
+
+ /** Thread-Safe Iterator. */
+ private class IteratorImpl implements Iterator<Entry<K, V>> {
+ private Entry<K, V> next;
+ private final Iterator<Entry<K, V>> targetIterator;
+
+ public IteratorImpl() {
+ lock.readLock.lock();
+ try {
+ targetIterator = cloneTarget().entrySet().iterator(); // Copy.
+ } finally {
+ lock.readLock.unlock();
+ }
+ }
+
+ @Override
+ public boolean hasNext() {
+ return targetIterator.hasNext();
+ }
+
+ @Override
+ public Entry<K, V> next() {
+ next = targetIterator.next();
+ return next;
+ }
+
+ @Override
+ public void remove() {
+ if (next == null) throw new IllegalStateException();
+ SharedMapImpl.this.remove(next.getKey());
+ next = null;
+ }
+ }
+
+ private static final long serialVersionUID = 0x600L; // Version.
+ protected ReadWriteLockImpl lock;
+ protected transient Thread updatingThread; // The thread executing an update.
+
+ public SharedMapImpl(MapService<K, V> target) {
+ this(target, new ReadWriteLockImpl());
+ }
+
+ public SharedMapImpl(MapService<K, V> target, ReadWriteLockImpl lock) {
+ super(target);
+ this.lock = lock;
+ }
+
+ @Override
+ public void clear() {
+ lock.writeLock.lock();
+ try {
+ target().clear();
+ } finally {
+ lock.writeLock.unlock();
+ }
+ }
+
+ @Override
+ public boolean containsKey(Object key) {
+ lock.readLock.lock();
+ try {
+ return target().containsKey(key);
+ } finally {
+ lock.readLock.unlock();
+ }
+ }
+
+ @Override
+ public boolean containsValue(Object value) {
+ lock.readLock.lock();
+ try {
+ return target().containsValue(value);
+ } finally {
+ lock.readLock.unlock();
+ }
+ }
+
+ @Override
+ public V get(Object key) {
+ lock.readLock.lock();
+ try {
+ return target().get(key);
+ } finally {
+ lock.readLock.unlock();
+ }
+ }
+
+ @Override
+ public boolean isEmpty() {
+ lock.readLock.lock();
+ try {
+ return target().isEmpty();
+ } finally {
+ lock.readLock.unlock();
+ }
+ }
+
+ @Override
+ public Iterator<Entry<K, V>> iterator() {
+ return new IteratorImpl();
+ }
+
+ @Override
+ public Equality<? super K> keyComparator() {
+ return target().keyComparator();
+ }
+
+ @Override
+ public V put(K key, V value) {
+ lock.writeLock.lock();
+ try {
+ return target().put(key, value);
+ } finally {
+ lock.writeLock.unlock();
+ }
+ }
+
+ @Override
+ public void putAll(Map<? extends K, ? extends V> m) {
+ lock.writeLock.lock();
+ try {
+ target().putAll(m);
+ } finally {
+ lock.writeLock.unlock();
+ }
+ }
+
+ @Override
+ public V putIfAbsent(K key, V value) {
+ lock.writeLock.lock();
+ try {
+ return target().putIfAbsent(key, value);
+ } finally {
+ lock.writeLock.unlock();
+ }
+ }
+
+ @Override
+ public V remove(Object key) {
+ lock.writeLock.lock();
+ try {
+ return target().remove(key);
+ } finally {
+ lock.writeLock.unlock();
+ }
+ }
+
+ @Override
+ public boolean remove(Object key, Object value) {
+ lock.writeLock.lock();
+ try {
+ return target().remove(key, value);
+ } finally {
+ lock.writeLock.unlock();
+ }
+ }
+
+ @Override
+ public V replace(K key, V value) {
+ lock.writeLock.lock();
+ try {
+ return target().replace(key, value);
+ } finally {
+ lock.writeLock.unlock();
+ }
+ }
+
+ @Override
+ public boolean replace(K key, V oldValue, V newValue) {
+ lock.writeLock.lock();
+ try {
+ return target().replace(key, oldValue, newValue);
+ } finally {
+ lock.writeLock.unlock();
+ }
+ }
+
+ @Override
+ public int size() {
+ lock.readLock.lock();
+ try {
+ return target().size();
+ } finally {
+ lock.readLock.unlock();
+ }
+ }
+
+ @Override
+ public Equality<? super V> valueComparator() {
+ return target().valueComparator();
+ }
+
+ /** Returns a clone copy of target. */
+ protected MapService<K, V> cloneTarget() {
+ try {
+ return target().clone();
+ } catch (CloneNotSupportedException e) {
+ throw new Error("Cannot happen since target is Cloneable.");
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public MapService<K,V>[] split(int n, boolean updateable) {
+ MapService<K,V>[] tmp;
+ lock.readLock.lock();
+ try {
+ tmp = target().split(n, updateable);
+ } finally {
+ lock.readLock.unlock();
+ }
+ MapService<K,V>[] result = new MapService[tmp.length];
+ for (int i = 0; i < tmp.length; i++) {
+ result[i] = new SharedMapImpl<K,V>(tmp[i], lock); // Shares the same locks.
+ }
+ return result;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/e43574ef/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/UnmodifiableMapImpl.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/UnmodifiableMapImpl.java b/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/UnmodifiableMapImpl.java
new file mode 100644
index 0000000..196e9cc
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/internal/map/UnmodifiableMapImpl.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.map;
+
+import java.util.Iterator;
+
+import javolution.util.function.Equality;
+import javolution.util.service.MapService;
+
+/**
+ * * An unmodifiable view over a map.
+ */
+public class UnmodifiableMapImpl<K, V> extends MapView<K, V> {
+
+ /** Read-Only Iterator. */
+ private class IteratorImpl implements Iterator<Entry<K, V>> {
+ private final Iterator<Entry<K, V>> targetIterator = target()
+ .iterator();
+
+ @Override
+ public boolean hasNext() {
+ return targetIterator.hasNext();
+ }
+
+ @Override
+ public Entry<K, V> next() {
+ return targetIterator.next();
+ }
+
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException("Read-Only Map.");
+ }
+ }
+
+ private static final long serialVersionUID = 0x600L; // Version.
+
+ public UnmodifiableMapImpl(MapService<K, V> target) {
+ super(target);
+ }
+
+ @Override
+ public void clear() {
+ throw new UnsupportedOperationException("Unmodifiable");
+ }
+
+ @Override
+ public boolean containsKey(Object key) {
+ return target().containsKey(key);
+ }
+
+ @Override
+ public V get(Object key) {
+ return target().get(key);
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return target().isEmpty();
+ }
+
+ @Override
+ public Iterator<Entry<K, V>> iterator() {
+ return new IteratorImpl();
+ }
+
+ @Override
+ public Equality<? super K> keyComparator() {
+ return target().keyComparator();
+ }
+
+ @Override
+ public V put(K key, V value) {
+ throw new UnsupportedOperationException("Unmodifiable");
+ }
+
+ @Override
+ public V remove(Object key) {
+ throw new UnsupportedOperationException("Unmodifiable");
+ }
+
+ @Override
+ public int size() {
+ return target().size();
+ }
+
+ @Override
+ public Equality<? super V> valueComparator() {
+ return target().valueComparator();
+ }
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public MapService<K,V>[] split(int n, boolean updateable) {
+ MapService<K,V>[] subTargets = target().split(n, updateable);
+ MapService<K,V>[] result = new MapService[subTargets.length];
+ for (int i = 0; i < subTargets.length; i++) {
+ result[i] = new UnmodifiableMapImpl<K,V>(subTargets[i]);
+ }
+ return result;
+ }
+
+}