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:49 UTC
[44/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/service/SortedTableService.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/service/SortedTableService.java b/commons/marmotta-commons/src/ext/java/javolution/util/service/SortedTableService.java
new file mode 100644
index 0000000..74e47e2
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/service/SortedTableService.java
@@ -0,0 +1,36 @@
+/*
+ * 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.service;
+
+/**
+ * The set of related functionalities used to implement sorted tables collections.
+ *
+ * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
+ * @version 6.0, July 21, 2013
+ */
+public interface SortedTableService<E> extends TableService<E> {
+
+ /**
+ * Adds the specified element only if not already present.
+ *
+ * @return {@code true} if the element has been added;
+ * {@code false} otherwise.
+ */
+ boolean addIfAbsent(E element);
+
+ /**
+ * Returns what would be the index of the specified element if it were
+ * to be added or the index of the specified element if already present.
+ */
+ int positionOf(E element);
+
+ @Override
+ SortedTableService<E>[] split(int n, boolean updateable);
+
+}
\ 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/service/TableService.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/service/TableService.java b/commons/marmotta-commons/src/ext/java/javolution/util/service/TableService.java
new file mode 100644
index 0000000..e338840
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/service/TableService.java
@@ -0,0 +1,29 @@
+/*
+ * Javolution - Java(TM) Solution for Real-Time and Embedded Systems
+ * Copyright (C) 2012 - Javolution (http://javolution.org/)
+ * All rights reserved.
+ *
+ * Permission to use, copy, modify, and distribute this software is
+ * freely granted, provided that this notice is preserved.
+ */
+package javolution.util.service;
+
+import java.util.Deque;
+import java.util.List;
+import java.util.RandomAccess;
+
+/**
+ * The set of related functionalities used to implement tables collections.
+ *
+ * @author <a href="mailto:jean-marie@dautelle.com">Jean-Marie Dautelle</a>
+ * @version 6.0, July 21, 2013
+ */
+public interface TableService<E> extends CollectionService<E>, List<E>, Deque<E>, RandomAccess {
+
+ @Override
+ TableService<E> subList(int fromIndex, int toIndex);
+
+ @Override
+ TableService<E>[] split(int n, boolean updateable);
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/ext/java/javolution/util/service/package-info.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/ext/java/javolution/util/service/package-info.java b/commons/marmotta-commons/src/ext/java/javolution/util/service/package-info.java
new file mode 100644
index 0000000..26fa4aa
--- /dev/null
+++ b/commons/marmotta-commons/src/ext/java/javolution/util/service/package-info.java
@@ -0,0 +1,6 @@
+/**
+<p> Service interfaces to be implemented by {@code javolution.util.*}
+ collections and collections views.</p>
+ */
+package javolution.util.service;
+
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/collections/CollectionUtils.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/collections/CollectionUtils.java b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/collections/CollectionUtils.java
index 7c4fd6f..0b9785f 100644
--- a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/collections/CollectionUtils.java
+++ b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/collections/CollectionUtils.java
@@ -17,6 +17,8 @@
*/
package org.apache.marmotta.commons.collections;
+import com.google.common.base.Equivalence;
+
import java.text.Format;
import java.util.ArrayList;
import java.util.Collection;
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/collections/EquivalenceHashMap.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/collections/EquivalenceHashMap.java b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/collections/EquivalenceHashMap.java
new file mode 100644
index 0000000..451f2ac
--- /dev/null
+++ b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/collections/EquivalenceHashMap.java
@@ -0,0 +1,563 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.marmotta.commons.collections;
+
+import com.google.common.base.Equivalence;
+import com.google.common.base.Function;
+import com.google.common.collect.Collections2;
+import com.google.common.collect.Iterators;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Sets;
+
+import java.lang.reflect.Array;
+import java.util.*;
+
+/**
+ * A hashmap implementation accepting different notions of equvialence for keys (based on the Guava Equivalence class).
+ *
+ * @author Sebastian Schaffert (sschaffert@apache.org)
+ */
+public class EquivalenceHashMap<K,V> implements Map<K,V> {
+
+ private Equivalence equivalence;
+
+ private HashMap<ElementWrapper, V> delegate;
+
+
+ private UnwrapFunction unwrapper = new UnwrapFunction();
+ private WrapFunction wrapper = new WrapFunction();
+
+
+ public EquivalenceHashMap(Equivalence equivalence) {
+ this.equivalence = equivalence;
+ this.delegate = new HashMap<>();
+ }
+
+ /**
+ * Removes all of the mappings from this map (optional operation).
+ * The map will be empty after this call returns.
+ *
+ * @throws UnsupportedOperationException if the <tt>clear</tt> operation
+ * is not supported by this map
+ */
+ @Override
+ public void clear() {
+ delegate.clear();
+ }
+
+ /**
+ * Returns the number of key-value mappings in this map. If the
+ * map contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
+ * <tt>Integer.MAX_VALUE</tt>.
+ *
+ * @return the number of key-value mappings in this map
+ */
+ @Override
+ public int size() {
+ return delegate.size();
+ }
+
+ /**
+ * Returns <tt>true</tt> if this map contains no key-value mappings.
+ *
+ * @return <tt>true</tt> if this map contains no key-value mappings
+ */
+ @Override
+ public boolean isEmpty() {
+ return delegate.isEmpty();
+ }
+
+ /**
+ * Returns <tt>true</tt> if this map contains a mapping for the specified
+ * key. More formally, returns <tt>true</tt> if and only if
+ * this map contains a mapping for a key <tt>k</tt> such that
+ * <tt>(key==null ? k==null : key.equals(k))</tt>. (There can be
+ * at most one such mapping.)
+ *
+ * @param key key whose presence in this map is to be tested
+ * @return <tt>true</tt> if this map contains a mapping for the specified
+ * key
+ * @throws ClassCastException if the key is of an inappropriate type for
+ * this map
+ * (<a href="Collection.html#optional-restrictions">optional</a>)
+ * @throws NullPointerException if the specified key is null and this map
+ * does not permit null keys
+ * (<a href="Collection.html#optional-restrictions">optional</a>)
+ */
+ @Override
+ public boolean containsKey(Object key) {
+ return delegate.containsKey(new ElementWrapper((K)key));
+ }
+
+ /**
+ * Returns <tt>true</tt> if this map maps one or more keys to the
+ * specified value. More formally, returns <tt>true</tt> if and only if
+ * this map contains at least one mapping to a value <tt>v</tt> such that
+ * <tt>(value==null ? v==null : value.equals(v))</tt>. This operation
+ * will probably require time linear in the map size for most
+ * implementations of the <tt>Map</tt> interface.
+ *
+ * @param value value whose presence in this map is to be tested
+ * @return <tt>true</tt> if this map maps one or more keys to the
+ * specified value
+ * @throws ClassCastException if the value is of an inappropriate type for
+ * this map
+ * (<a href="Collection.html#optional-restrictions">optional</a>)
+ * @throws NullPointerException if the specified value is null and this
+ * map does not permit null values
+ * (<a href="Collection.html#optional-restrictions">optional</a>)
+ */
+ @Override
+ public boolean containsValue(Object value) {
+ return delegate.containsValue(value);
+ }
+
+ /**
+ * Returns the value to which the specified key is mapped,
+ * or {@code null} if this map contains no mapping for the key.
+ * <p/>
+ * <p>More formally, if this map contains a mapping from a key
+ * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
+ * key.equals(k))}, then this method returns {@code v}; otherwise
+ * it returns {@code null}. (There can be at most one such mapping.)
+ * <p/>
+ * <p>If this map permits null values, then a return value of
+ * {@code null} does not <i>necessarily</i> indicate that the map
+ * contains no mapping for the key; it's also possible that the map
+ * explicitly maps the key to {@code null}. The {@link #containsKey
+ * containsKey} operation may be used to distinguish these two cases.
+ *
+ * @param key the key whose associated value is to be returned
+ * @return the value to which the specified key is mapped, or
+ * {@code null} if this map contains no mapping for the key
+ * @throws ClassCastException if the key is of an inappropriate type for
+ * this map
+ * (<a href="Collection.html#optional-restrictions">optional</a>)
+ * @throws NullPointerException if the specified key is null and this map
+ * does not permit null keys
+ * (<a href="Collection.html#optional-restrictions">optional</a>)
+ */
+ @Override
+ public V get(Object key) {
+ return delegate.get(new ElementWrapper((K)key));
+ }
+
+ /**
+ * Associates the specified value with the specified key in this map
+ * (optional operation). If the map previously contained a mapping for
+ * the key, the old value is replaced by the specified value. (A map
+ * <tt>m</tt> is said to contain a mapping for a key <tt>k</tt> if and only
+ * if {@link #containsKey(Object) m.containsKey(k)} would return
+ * <tt>true</tt>.)
+ *
+ * @param key key with which the specified value is to be associated
+ * @param value value to be associated with the specified key
+ * @return the previous value associated with <tt>key</tt>, or
+ * <tt>null</tt> if there was no mapping for <tt>key</tt>.
+ * (A <tt>null</tt> return can also indicate that the map
+ * previously associated <tt>null</tt> with <tt>key</tt>,
+ * if the implementation supports <tt>null</tt> values.)
+ * @throws UnsupportedOperationException if the <tt>put</tt> operation
+ * is not supported by this map
+ * @throws ClassCastException if the class of the specified key or value
+ * prevents it from being stored in this map
+ * @throws NullPointerException if the specified key or value is null
+ * and this map does not permit null keys or values
+ * @throws IllegalArgumentException if some property of the specified key
+ * or value prevents it from being stored in this map
+ */
+ @Override
+ public V put(K key, V value) {
+ return delegate.put(new ElementWrapper(key), value);
+ }
+
+ /**
+ * Removes the mapping for a key from this map if it is present
+ * (optional operation). More formally, if this map contains a mapping
+ * from key <tt>k</tt> to value <tt>v</tt> such that
+ * <code>(key==null ? k==null : key.equals(k))</code>, that mapping
+ * is removed. (The map can contain at most one such mapping.)
+ * <p/>
+ * <p>Returns the value to which this map previously associated the key,
+ * or <tt>null</tt> if the map contained no mapping for the key.
+ * <p/>
+ * <p>If this map permits null values, then a return value of
+ * <tt>null</tt> does not <i>necessarily</i> indicate that the map
+ * contained no mapping for the key; it's also possible that the map
+ * explicitly mapped the key to <tt>null</tt>.
+ * <p/>
+ * <p>The map will not contain a mapping for the specified key once the
+ * call returns.
+ *
+ * @param key key whose mapping is to be removed from the map
+ * @return the previous value associated with <tt>key</tt>, or
+ * <tt>null</tt> if there was no mapping for <tt>key</tt>.
+ * @throws UnsupportedOperationException if the <tt>remove</tt> operation
+ * is not supported by this map
+ * @throws ClassCastException if the key is of an inappropriate type for
+ * this map
+ * (<a href="Collection.html#optional-restrictions">optional</a>)
+ * @throws NullPointerException if the specified key is null and this
+ * map does not permit null keys
+ * (<a href="Collection.html#optional-restrictions">optional</a>)
+ */
+ @Override
+ public V remove(Object key) {
+ return delegate.remove(new ElementWrapper((K)key));
+ }
+
+ /**
+ * Copies all of the mappings from the specified map to this map
+ * (optional operation). The effect of this call is equivalent to that
+ * of calling {@link #put(Object, Object) put(k, v)} on this map once
+ * for each mapping from key <tt>k</tt> to value <tt>v</tt> in the
+ * specified map. The behavior of this operation is undefined if the
+ * specified map is modified while the operation is in progress.
+ *
+ * @param m mappings to be stored in this map
+ * @throws UnsupportedOperationException if the <tt>putAll</tt> operation
+ * is not supported by this map
+ * @throws ClassCastException if the class of a key or value in the
+ * specified map prevents it from being stored in this map
+ * @throws NullPointerException if the specified map is null, or if
+ * this map does not permit null keys or values, and the
+ * specified map contains null keys or values
+ * @throws IllegalArgumentException if some property of a key or value in
+ * the specified map prevents it from being stored in this map
+ */
+ @Override
+ public void putAll(Map<? extends K, ? extends V> m) {
+ for(Entry entry : m.entrySet()) {
+ put((K)entry.getKey(),(V)entry.getValue());
+ }
+ }
+
+ /**
+ * Returns a {@link java.util.Set} view of the keys contained in this map.
+ * The set is backed by the map, so changes to the map are
+ * reflected in the set, and vice-versa. If the map is modified
+ * while an iteration over the set is in progress (except through
+ * the iterator's own <tt>remove</tt> operation), the results of
+ * the iteration are undefined. The set supports element removal,
+ * which removes the corresponding mapping from the map, via the
+ * <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
+ * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
+ * operations. It does not support the <tt>add</tt> or <tt>addAll</tt>
+ * operations.
+ *
+ * @return a set view of the keys contained in this map
+ */
+ @Override
+ public Set<K> keySet() {
+ final Set<ElementWrapper> wrapped = delegate.keySet();
+
+ return new Set<K>() {
+ @Override
+ public int size() {
+ return wrapped.size();
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return wrapped.isEmpty();
+ }
+
+ @Override
+ public boolean contains(Object o) {
+ return wrapped.contains(new ElementWrapper((K)o));
+ }
+
+ @Override
+ public Iterator<K> iterator() {
+ return Iterators.transform(wrapped.iterator(),unwrapper);
+ }
+
+ @Override
+ public Object[] toArray() {
+ return Iterators.toArray(this.iterator(), Object.class);
+ }
+
+ @Override
+ public <T> T[] toArray(T[] a) {
+ T[] result;
+ if(a.length < size()) {
+ result = (T[]) Array.newInstance(a.getClass().getComponentType(), size());
+ } else {
+ result = a;
+ }
+ int pos = 0;
+ for(ElementWrapper w : wrapped) {
+ try {
+ result[pos++] = (T) w.delegate;
+ } catch (ClassCastException ex) {
+ throw new ArrayStoreException("invalid type for array");
+ }
+ }
+ if(pos < result.length) {
+ result[pos] = null;
+ }
+ return result;
+ }
+
+ @Override
+ public boolean add(K k) {
+ return wrapped.add(new ElementWrapper(k));
+ }
+
+ @Override
+ public boolean remove(Object o) {
+ return wrapped.remove(new ElementWrapper((K)o));
+ }
+
+ @Override
+ public boolean containsAll(Collection<?> c) {
+ return wrapped.containsAll(Collections2.transform(c, wrapper));
+ }
+
+ @Override
+ public boolean addAll(Collection<? extends K> c) {
+ return wrapped.addAll(Collections2.transform(c, wrapper));
+ }
+
+ @Override
+ public boolean retainAll(Collection<?> c) {
+ return wrapped.retainAll(Collections2.transform(c, wrapper));
+ }
+
+ @Override
+ public boolean removeAll(Collection<?> c) {
+ return wrapped.removeAll(Collections2.transform(c, wrapper));
+ }
+
+ @Override
+ public void clear() {
+ wrapped.clear();
+ }
+ };
+ }
+
+ /**
+ * Returns a {@link java.util.Collection} view of the values contained in this map.
+ * The collection is backed by the map, so changes to the map are
+ * reflected in the collection, and vice-versa. If the map is
+ * modified while an iteration over the collection is in progress
+ * (except through the iterator's own <tt>remove</tt> operation),
+ * the results of the iteration are undefined. The collection
+ * supports element removal, which removes the corresponding
+ * mapping from the map, via the <tt>Iterator.remove</tt>,
+ * <tt>Collection.remove</tt>, <tt>removeAll</tt>,
+ * <tt>retainAll</tt> and <tt>clear</tt> operations. It does not
+ * support the <tt>add</tt> or <tt>addAll</tt> operations.
+ *
+ * @return a collection view of the values contained in this map
+ */
+ @Override
+ public Collection<V> values() {
+ return delegate.values();
+ }
+
+ /**
+ * Returns a {@link java.util.Set} view of the mappings contained in this map.
+ * The set is backed by the map, so changes to the map are
+ * reflected in the set, and vice-versa. If the map is modified
+ * while an iteration over the set is in progress (except through
+ * the iterator's own <tt>remove</tt> operation, or through the
+ * <tt>setValue</tt> operation on a map entry returned by the
+ * iterator) the results of the iteration are undefined. The set
+ * supports element removal, which removes the corresponding
+ * mapping from the map, via the <tt>Iterator.remove</tt>,
+ * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
+ * <tt>clear</tt> operations. It does not support the
+ * <tt>add</tt> or <tt>addAll</tt> operations.
+ *
+ * @return a set view of the mappings contained in this map
+ */
+ @Override
+ public Set<Entry<K, V>> entrySet() {
+ final Set<Entry<ElementWrapper,V>> wrapped = delegate.entrySet();
+ final EntryUnwrapFunction entryUnwrapFunction = new EntryUnwrapFunction();
+ final EntryWrapFunction entryWrapFunction = new EntryWrapFunction();
+
+ return new Set<Entry<K, V>>() {
+ @Override
+ public int size() {
+ return wrapped.size();
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return wrapped.isEmpty();
+ }
+
+ @Override
+ public boolean contains(Object o) {
+ return wrapped.contains(new ElementWrapper((K)o));
+ }
+
+ @Override
+ public Iterator<Entry<K, V>> iterator() {
+ return Iterators.transform(wrapped.iterator(), entryUnwrapFunction);
+ }
+
+ @Override
+ public Object[] toArray() {
+ return Iterators.toArray(this.iterator(), Object.class);
+ }
+
+ @Override
+ public <T> T[] toArray(T[] a) {
+ T[] result;
+ if(a.length < size()) {
+ result = (T[]) Array.newInstance(a.getClass().getComponentType(), size());
+ } else {
+ result = a;
+ }
+ int pos = 0;
+ for(Entry<ElementWrapper,V> w : wrapped) {
+ try {
+ result[pos++] = (T) entryUnwrapFunction.apply(w);
+ } catch (ClassCastException ex) {
+ throw new ArrayStoreException("invalid type for array");
+ }
+ }
+ if(pos < result.length) {
+ result[pos] = null;
+ }
+ return result;
+ }
+
+ @Override
+ public boolean add(Entry<K, V> k) {
+ return wrapped.add(entryWrapFunction.apply(k));
+ }
+
+ @Override
+ public boolean remove(Object o) {
+ return wrapped.remove(entryWrapFunction.apply((Entry<K, V>)o));
+ }
+
+ @Override
+ public boolean containsAll(Collection<?> c) {
+ return wrapped.containsAll(Collections2.transform(c, entryWrapFunction));
+ }
+
+ @Override
+ public boolean addAll(Collection<? extends Entry<K, V>> c) {
+ return wrapped.addAll(Collections2.transform(c, entryWrapFunction));
+ }
+
+ @Override
+ public boolean retainAll(Collection<?> c) {
+ return wrapped.retainAll(Collections2.transform(c, entryWrapFunction));
+ }
+
+ @Override
+ public boolean removeAll(Collection<?> c) {
+ return wrapped.removeAll(Collections2.transform(c, entryWrapFunction));
+ }
+
+ @Override
+ public void clear() {
+ wrapped.clear();
+ }
+ };
+ }
+
+ private class ElementWrapper {
+
+ private K delegate;
+
+ private ElementWrapper(K delegate) {
+ this.delegate = delegate;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if(o instanceof EquivalenceHashMap<?,?>.ElementWrapper) {
+ return equivalence.equivalent(ElementWrapper.this.delegate, ((ElementWrapper)o).delegate);
+ } else {
+ return false;
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ return equivalence.hash(delegate);
+ }
+ }
+
+ private class UnwrapFunction implements Function<ElementWrapper, K> {
+ @Override
+ public K apply(ElementWrapper input) {
+ return input.delegate;
+ }
+ }
+
+ private class WrapFunction implements Function<Object,ElementWrapper> {
+ @Override
+ public ElementWrapper apply(Object input) {
+ return new ElementWrapper((K)input);
+ }
+ }
+
+ private class EntryUnwrapFunction implements Function<Entry<ElementWrapper,V>, Entry<K, V>> {
+ @Override
+ public Entry<K, V> apply(final Entry<ElementWrapper, V> input) {
+ return new Entry<K, V>() {
+ @Override
+ public K getKey() {
+ return input.getKey().delegate;
+ }
+
+ @Override
+ public V getValue() {
+ return input.getValue();
+ }
+
+ @Override
+ public V setValue(V value) {
+ return input.setValue(value);
+ }
+ };
+ }
+ }
+
+ private class EntryWrapFunction implements Function<Object,Entry<ElementWrapper,V>> {
+ @Override
+ public Entry<ElementWrapper, V> apply(Object o) {
+ final Entry<K, V> input = (Entry<K,V>)o;
+ return new Entry<ElementWrapper, V>() {
+ @Override
+ public ElementWrapper getKey() {
+ return new ElementWrapper(input.getKey());
+ }
+
+ @Override
+ public V getValue() {
+ return input.getValue();
+ }
+
+ @Override
+ public V setValue(V value) {
+ return input.setValue(value);
+ }
+ };
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/collections/EquivalenceHashSet.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/collections/EquivalenceHashSet.java b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/collections/EquivalenceHashSet.java
new file mode 100644
index 0000000..9927efb
--- /dev/null
+++ b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/collections/EquivalenceHashSet.java
@@ -0,0 +1,472 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.marmotta.commons.collections;
+
+import com.google.common.base.Equivalence;
+import com.google.common.base.Function;
+import com.google.common.collect.Collections2;
+import com.google.common.collect.Iterators;
+
+import java.util.*;
+
+/**
+ * A hashset implementation accepting different notions of equvialence (based on the Guava Equivalence class).
+ *
+ * @author Sebastian Schaffert (sschaffert@apache.org)
+ */
+public class EquivalenceHashSet<E> implements Set<E> {
+
+ private Equivalence equivalence;
+
+ private HashSet<ElementWrapper> delegate;
+
+
+ private UnwrapFunction unwrapper = new UnwrapFunction();
+ private WrapFunction wrapper = new WrapFunction();
+
+
+ public EquivalenceHashSet() {
+ this.equivalence = Equivalence.equals();
+ this.delegate = new HashSet<>();
+ }
+
+ public EquivalenceHashSet(Equivalence equivalence) {
+ this.equivalence = equivalence;
+ this.delegate = new HashSet<>();
+ }
+
+
+ /**
+ * Adds the specified element to this set if it is not already present
+ * (optional operation). More formally, adds the specified element
+ * <tt>e</tt> to this set if the set contains no element <tt>e2</tt>
+ * such that
+ * <tt>(e==null ? e2==null : e.equals(e2))</tt>.
+ * If this set already contains the element, the call leaves the set
+ * unchanged and returns <tt>false</tt>. In combination with the
+ * restriction on constructors, this ensures that sets never contain
+ * duplicate elements.
+ * <p/>
+ * <p>The stipulation above does not imply that sets must accept all
+ * elements; sets may refuse to add any particular element, including
+ * <tt>null</tt>, and throw an exception, as described in the
+ * specification for {@link java.util.Collection#add Collection.add}.
+ * Individual set implementations should clearly document any
+ * restrictions on the elements that they may contain.
+ *
+ * @param e element to be added to this set
+ * @return <tt>true</tt> if this set did not already contain the specified
+ * element
+ * @throws UnsupportedOperationException if the <tt>add</tt> operation
+ * is not supported by this set
+ * @throws ClassCastException if the class of the specified element
+ * prevents it from being added to this set
+ * @throws NullPointerException if the specified element is null and this
+ * set does not permit null elements
+ * @throws IllegalArgumentException if some property of the specified element
+ * prevents it from being added to this set
+ */
+ @Override
+ public boolean add(E e) {
+ return delegate.add(new ElementWrapper(e));
+ }
+
+ /**
+ * Returns the number of elements in this set (its cardinality). If this
+ * set contains more than <tt>Integer.MAX_VALUE</tt> elements, returns
+ * <tt>Integer.MAX_VALUE</tt>.
+ *
+ * @return the number of elements in this set (its cardinality)
+ */
+ @Override
+ public int size() {
+ return delegate.size();
+ }
+
+ /**
+ * Returns <tt>true</tt> if this set contains no elements.
+ *
+ * @return <tt>true</tt> if this set contains no elements
+ */
+ @Override
+ public boolean isEmpty() {
+ return delegate.isEmpty();
+ }
+
+ /**
+ * Returns <tt>true</tt> if this set contains the specified element.
+ * More formally, returns <tt>true</tt> if and only if this set
+ * contains an element <tt>e</tt> such that
+ * <tt>(o==null ? e==null : o.equals(e))</tt>.
+ *
+ * @param o element whose presence in this set is to be tested
+ * @return <tt>true</tt> if this set contains the specified element
+ * @throws ClassCastException if the type of the specified element
+ * is incompatible with this set
+ * (<a href="Collection.html#optional-restrictions">optional</a>)
+ * @throws NullPointerException if the specified element is null and this
+ * set does not permit null elements
+ * (<a href="Collection.html#optional-restrictions">optional</a>)
+ */
+ @Override
+ public boolean contains(Object o) {
+ return delegate.contains(new ElementWrapper((E)o));
+ }
+
+ /**
+ * Returns an iterator over the elements in this set. The elements are
+ * returned in no particular order (unless this set is an instance of some
+ * class that provides a guarantee).
+ *
+ * @return an iterator over the elements in this set
+ */
+ @Override
+ public Iterator<E> iterator() {
+ return Iterators.transform(delegate.iterator(), unwrapper);
+ }
+
+ /**
+ * Returns an array containing all of the elements in this set.
+ * If this set makes any guarantees as to what order its elements
+ * are returned by its iterator, this method must return the
+ * elements in the same order.
+ * <p/>
+ * <p>The returned array will be "safe" in that no references to it
+ * are maintained by this set. (In other words, this method must
+ * allocate a new array even if this set is backed by an array).
+ * The caller is thus free to modify the returned array.
+ * <p/>
+ * <p>This method acts as bridge between array-based and collection-based
+ * APIs.
+ *
+ * @return an array containing all the elements in this set
+ */
+ @Override
+ public Object[] toArray() {
+ return Iterators.toArray(this.iterator(), Object.class);
+ }
+
+ /**
+ * Returns an array containing all of the elements in this set; the
+ * runtime type of the returned array is that of the specified array.
+ * If the set fits in the specified array, it is returned therein.
+ * Otherwise, a new array is allocated with the runtime type of the
+ * specified array and the size of this set.
+ * <p/>
+ * <p>If this set fits in the specified array with room to spare
+ * (i.e., the array has more elements than this set), the element in
+ * the array immediately following the end of the set is set to
+ * <tt>null</tt>. (This is useful in determining the length of this
+ * set <i>only</i> if the caller knows that this set does not contain
+ * any null elements.)
+ * <p/>
+ * <p>If this set makes any guarantees as to what order its elements
+ * are returned by its iterator, this method must return the elements
+ * in the same order.
+ * <p/>
+ * <p>Like the {@link #toArray()} method, this method acts as bridge between
+ * array-based and collection-based APIs. Further, this method allows
+ * precise control over the runtime type of the output array, and may,
+ * under certain circumstances, be used to save allocation costs.
+ * <p/>
+ * <p>Suppose <tt>x</tt> is a set known to contain only strings.
+ * The following code can be used to dump the set into a newly allocated
+ * array of <tt>String</tt>:
+ * <p/>
+ * <pre>
+ * String[] y = x.toArray(new String[0]);</pre>
+ *
+ * Note that <tt>toArray(new Object[0])</tt> is identical in function to
+ * <tt>toArray()</tt>.
+ *
+ * @param a the array into which the elements of this set are to be
+ * stored, if it is big enough; otherwise, a new array of the same
+ * runtime type is allocated for this purpose.
+ * @return an array containing all the elements in this set
+ * @throws ArrayStoreException if the runtime type of the specified array
+ * is not a supertype of the runtime type of every element in this
+ * set
+ * @throws NullPointerException if the specified array is null
+ */
+ @Override
+ public <T> T[] toArray(T[] a) {
+ // stolen from AbstractCollection
+
+ // Estimate size of array; be prepared to see more or fewer elements
+ int size = size();
+ T[] r = a.length >= size ? a :
+ (T[])java.lang.reflect.Array
+ .newInstance(a.getClass().getComponentType(), size);
+ Iterator<E> it = iterator();
+
+ for (int i = 0; i < r.length; i++) {
+ if (! it.hasNext()) { // fewer elements than expected
+ if (a != r)
+ return Arrays.copyOf(r, i);
+ r[i] = null; // null-terminate
+ return r;
+ }
+ r[i] = (T)it.next();
+ }
+ return it.hasNext() ? finishToArray(r, it) : r;
+ }
+
+
+ /**
+ * The maximum size of array to allocate.
+ * Some VMs reserve some header words in an array.
+ * Attempts to allocate larger arrays may result in
+ * OutOfMemoryError: Requested array size exceeds VM limit
+ */
+ private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
+
+ /**
+ * Reallocates the array being used within toArray when the iterator
+ * returned more elements than expected, and finishes filling it from
+ * the iterator.
+ *
+ * @param r the array, replete with previously stored elements
+ * @param it the in-progress iterator over this collection
+ * @return array containing the elements in the given array, plus any
+ * further elements returned by the iterator, trimmed to size
+ */
+ private static <T> T[] finishToArray(T[] r, Iterator<?> it) {
+ int i = r.length;
+ while (it.hasNext()) {
+ int cap = r.length;
+ if (i == cap) {
+ int newCap = cap + (cap >> 1) + 1;
+ // overflow-conscious code
+ if (newCap - MAX_ARRAY_SIZE > 0)
+ newCap = hugeCapacity(cap + 1);
+ r = Arrays.copyOf(r, newCap);
+ }
+ r[i++] = (T)it.next();
+ }
+ // trim if overallocated
+ return (i == r.length) ? r : Arrays.copyOf(r, i);
+ }
+
+
+ private static int hugeCapacity(int minCapacity) {
+ if (minCapacity < 0) // overflow
+ throw new OutOfMemoryError
+ ("Required array size too large");
+ return (minCapacity > MAX_ARRAY_SIZE) ?
+ Integer.MAX_VALUE :
+ MAX_ARRAY_SIZE;
+ }
+
+
+ /**
+ * Removes the specified element from this set if it is present
+ * (optional operation). More formally, removes an element <tt>e</tt>
+ * such that
+ * <tt>(o==null ? e==null : o.equals(e))</tt>, if
+ * this set contains such an element. Returns <tt>true</tt> if this set
+ * contained the element (or equivalently, if this set changed as a
+ * result of the call). (This set will not contain the element once the
+ * call returns.)
+ *
+ * @param o object to be removed from this set, if present
+ * @return <tt>true</tt> if this set contained the specified element
+ * @throws ClassCastException if the type of the specified element
+ * is incompatible with this set
+ * (<a href="Collection.html#optional-restrictions">optional</a>)
+ * @throws NullPointerException if the specified element is null and this
+ * set does not permit null elements
+ * (<a href="Collection.html#optional-restrictions">optional</a>)
+ * @throws UnsupportedOperationException if the <tt>remove</tt> operation
+ * is not supported by this set
+ */
+ @Override
+ public boolean remove(Object o) {
+ return delegate.remove(new ElementWrapper((E)o));
+ }
+
+ /**
+ * Returns <tt>true</tt> if this set contains all of the elements of the
+ * specified collection. If the specified collection is also a set, this
+ * method returns <tt>true</tt> if it is a <i>subset</i> of this set.
+ *
+ * @param c collection to be checked for containment in this set
+ * @return <tt>true</tt> if this set contains all of the elements of the
+ * specified collection
+ * @throws ClassCastException if the types of one or more elements
+ * in the specified collection are incompatible with this
+ * set
+ * (<a href="Collection.html#optional-restrictions">optional</a>)
+ * @throws NullPointerException if the specified collection contains one
+ * or more null elements and this set does not permit null
+ * elements
+ * (<a href="Collection.html#optional-restrictions">optional</a>),
+ * or if the specified collection is null
+ * @see #contains(Object)
+ */
+ @Override
+ public boolean containsAll(Collection<?> c) {
+ return delegate.containsAll(Collections2.transform(c,wrapper));
+ }
+
+ /**
+ * Adds all of the elements in the specified collection to this set if
+ * they're not already present (optional operation). If the specified
+ * collection is also a set, the <tt>addAll</tt> operation effectively
+ * modifies this set so that its value is the <i>union</i> of the two
+ * sets. The behavior of this operation is undefined if the specified
+ * collection is modified while the operation is in progress.
+ *
+ * @param c collection containing elements to be added to this set
+ * @return <tt>true</tt> if this set changed as a result of the call
+ * @throws UnsupportedOperationException if the <tt>addAll</tt> operation
+ * is not supported by this set
+ * @throws ClassCastException if the class of an element of the
+ * specified collection prevents it from being added to this set
+ * @throws NullPointerException if the specified collection contains one
+ * or more null elements and this set does not permit null
+ * elements, or if the specified collection is null
+ * @throws IllegalArgumentException if some property of an element of the
+ * specified collection prevents it from being added to this set
+ * @see #add(Object)
+ */
+ @Override
+ public boolean addAll(Collection<? extends E> c) {
+ return delegate.addAll(Collections2.transform(c, wrapper));
+ }
+
+ /**
+ * Retains only the elements in this set that are contained in the
+ * specified collection (optional operation). In other words, removes
+ * from this set all of its elements that are not contained in the
+ * specified collection. If the specified collection is also a set, this
+ * operation effectively modifies this set so that its value is the
+ * <i>intersection</i> of the two sets.
+ *
+ * @param c collection containing elements to be retained in this set
+ * @return <tt>true</tt> if this set changed as a result of the call
+ * @throws UnsupportedOperationException if the <tt>retainAll</tt> operation
+ * is not supported by this set
+ * @throws ClassCastException if the class of an element of this set
+ * is incompatible with the specified collection
+ * (<a href="Collection.html#optional-restrictions">optional</a>)
+ * @throws NullPointerException if this set contains a null element and the
+ * specified collection does not permit null elements
+ * (<a href="Collection.html#optional-restrictions">optional</a>),
+ * or if the specified collection is null
+ * @see #remove(Object)
+ */
+ @Override
+ public boolean retainAll(Collection<?> c) {
+ return delegate.retainAll(Collections2.transform(c, wrapper));
+ }
+
+ /**
+ * Removes from this set all of its elements that are contained in the
+ * specified collection (optional operation). If the specified
+ * collection is also a set, this operation effectively modifies this
+ * set so that its value is the <i>asymmetric set difference</i> of
+ * the two sets.
+ *
+ * @param c collection containing elements to be removed from this set
+ * @return <tt>true</tt> if this set changed as a result of the call
+ * @throws UnsupportedOperationException if the <tt>removeAll</tt> operation
+ * is not supported by this set
+ * @throws ClassCastException if the class of an element of this set
+ * is incompatible with the specified collection
+ * (<a href="Collection.html#optional-restrictions">optional</a>)
+ * @throws NullPointerException if this set contains a null element and the
+ * specified collection does not permit null elements
+ * (<a href="Collection.html#optional-restrictions">optional</a>),
+ * or if the specified collection is null
+ * @see #remove(Object)
+ * @see #contains(Object)
+ */
+ @Override
+ public boolean removeAll(Collection<?> c) {
+ return delegate.removeAll(Collections2.transform(c, wrapper));
+ }
+
+ /**
+ * Removes all of the elements from this set (optional operation).
+ * The set will be empty after this call returns.
+ *
+ * @throws UnsupportedOperationException if the <tt>clear</tt> method
+ * is not supported by this set
+ */
+ @Override
+ public void clear() {
+ delegate.clear();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ EquivalenceHashSet that = (EquivalenceHashSet) o;
+
+ if (!delegate.equals(that.delegate)) return false;
+ if (!equivalence.equals(that.equivalence)) return false;
+
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ int result = equivalence.hashCode();
+ result = 31 * result + delegate.hashCode();
+ return result;
+ }
+
+ private class ElementWrapper {
+
+ private E delegate;
+
+ private ElementWrapper(E delegate) {
+ this.delegate = delegate;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if(o instanceof EquivalenceHashSet<?>.ElementWrapper) {
+ return equivalence.equivalent(ElementWrapper.this.delegate, ((ElementWrapper)o).delegate);
+ } else {
+ return false;
+ }
+ }
+
+ @Override
+ public int hashCode() {
+ return equivalence.hash(delegate);
+ }
+ }
+
+ private class UnwrapFunction implements Function<ElementWrapper, E> {
+ @Override
+ public E apply(ElementWrapper input) {
+ return input.delegate;
+ }
+ }
+
+ private class WrapFunction implements Function<Object,ElementWrapper> {
+ @Override
+ public ElementWrapper apply(Object input) {
+ return new ElementWrapper((E)input);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/http/ETagGenerator.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/http/ETagGenerator.java b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/http/ETagGenerator.java
index d54a510..4d93807 100644
--- a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/http/ETagGenerator.java
+++ b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/http/ETagGenerator.java
@@ -18,13 +18,14 @@
package org.apache.marmotta.commons.http;
import org.apache.marmotta.commons.sesame.repository.ResourceUtils;
+import org.openrdf.model.BNode;
+import org.openrdf.model.Resource;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.RepositoryResult;
-
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
@@ -73,12 +74,25 @@ public class ETagGenerator {
return hasher.hash().toString();
}
- public static String getWeakETag(RepositoryConnection conn, String uri) throws RepositoryException {
- URI resource = ResourceUtils.getUriResource(conn, uri);
- return getWeakETag(conn, resource);
+ public static String getWeakETag(RepositoryConnection conn, String resource) throws RepositoryException {
+ if (resource.startsWith("http://")) {
+ return getWeakETag(conn, ResourceUtils.getUriResource(conn, resource));
+ } else {
+ return getWeakETag(conn, ResourceUtils.getAnonResource(conn, resource));
+ }
}
- public static String getWeakETag(RepositoryConnection conn, URI resource) throws RepositoryException {
+// public static String getWeakETag(RepositoryConnection conn, Resource resource) throws RepositoryException {
+// if (resource instanceof URI) {
+// return getWeakETag(conn, (URI)resource);
+// } else if (resource instanceof BNode) {
+// return getWeakETag(conn, (BNode)resource);
+// } else {
+// return null;
+// }
+// }
+
+ public static String getWeakETag(RepositoryConnection conn, Resource resource) throws RepositoryException {
if (resource == null) return "";
Hasher hasher = buildHasher();
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/http/UriUtil.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/http/UriUtil.java b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/http/UriUtil.java
index cc421bb..0fd360c 100644
--- a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/http/UriUtil.java
+++ b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/http/UriUtil.java
@@ -17,7 +17,7 @@
*/
package org.apache.marmotta.commons.http;
-import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang3.StringUtils;
import org.apache.commons.validator.routines.UrlValidator;
import java.net.URI;
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/locking/ObjectLocks.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/locking/ObjectLocks.java b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/locking/ObjectLocks.java
new file mode 100644
index 0000000..ebe4a1c
--- /dev/null
+++ b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/locking/ObjectLocks.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.marmotta.commons.locking;
+
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+import com.google.common.util.concurrent.Monitor;
+
+import java.util.HashSet;
+
+/**
+ * An implementation of dynamic name-based locks that allows more fine-grained locking methods based on a string name.
+ *
+ * @author Sebastian Schaffert (sschaffert@apache.org)
+ */
+public class ObjectLocks {
+
+ private LoadingCache<Object,Monitor> stringLocks;
+
+ public ObjectLocks() {
+ stringLocks = CacheBuilder.newBuilder().build(new LockCacheLoader());
+ }
+
+
+ public void lock(Object name) {
+ Monitor lock;
+ synchronized (stringLocks) {
+ lock = stringLocks.getUnchecked(name);
+ }
+ lock.enter();
+ }
+
+ public void unlock(Object name) {
+ Monitor lock;
+ synchronized (stringLocks) {
+ lock = stringLocks.getUnchecked(name);
+ }
+ lock.leave();
+ }
+
+ public boolean tryLock(Object name) {
+ Monitor lock;
+ synchronized (stringLocks) {
+ lock = stringLocks.getUnchecked(name);
+ }
+ return lock.tryEnter();
+ }
+
+ /**
+ * A simple Guava cache loader implementation for generating object-based locks
+ */
+ private static class LockCacheLoader extends CacheLoader<Object,Monitor> {
+ @Override
+ public Monitor load(Object key) throws Exception {
+ return new Monitor();
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/sesame/model/LiteralCommons.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/sesame/model/LiteralCommons.java b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/sesame/model/LiteralCommons.java
index ce26c26..5749e37 100644
--- a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/sesame/model/LiteralCommons.java
+++ b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/sesame/model/LiteralCommons.java
@@ -170,7 +170,8 @@ public class LiteralCommons {
} else if(Boolean.class.isAssignableFrom(javaClass) || boolean.class.isAssignableFrom(javaClass)) {
return Namespaces.NS_XSD+"boolean";
} else {
- return Namespaces.NS_XSD+"string";
+ // FIXME: MARMOTTA-39 (no default datatype before RDF-1.1)
+ return null; //Namespaces.NS_XSD+"string";
}
}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/sesame/model/LiteralKey.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/sesame/model/LiteralKey.java b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/sesame/model/LiteralKey.java
new file mode 100644
index 0000000..4a25b78
--- /dev/null
+++ b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/sesame/model/LiteralKey.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.marmotta.commons.sesame.model;
+
+/**
+ * Add file description here!
+ *
+ * @author Sebastian Schaffert (sschaffert@apache.org)
+ */
+public class LiteralKey {
+
+ private Object value;
+
+ private String type;
+
+ private String lang;
+
+ public LiteralKey(Object value, String type, String lang) {
+ this.value = value;
+ this.type = type;
+ this.lang = lang;
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ LiteralKey that = (LiteralKey) o;
+
+ if (lang != null ? !lang.equals(that.lang) : that.lang != null) return false;
+ if (type != null ? !type.equals(that.type) : that.type != null) return false;
+ if (!value.equals(that.value)) return false;
+
+ return true;
+ }
+
+ @Override
+ public int hashCode() {
+ int result = value.hashCode();
+ result = 31 * result + (type != null ? type.hashCode() : 0);
+ result = 31 * result + (lang != null ? lang.hashCode() : 0);
+ return result;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-marmotta/blob/582abb5b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/sesame/model/StatementCommons.java
----------------------------------------------------------------------
diff --git a/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/sesame/model/StatementCommons.java b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/sesame/model/StatementCommons.java
new file mode 100644
index 0000000..c04e7be
--- /dev/null
+++ b/commons/marmotta-commons/src/main/java/org/apache/marmotta/commons/sesame/model/StatementCommons.java
@@ -0,0 +1,190 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.marmotta.commons.sesame.model;
+
+import com.google.common.base.Equivalence;
+import com.google.common.base.Objects;
+import javolution.util.FastMap;
+import javolution.util.FastSet;
+import javolution.util.function.Equality;
+import org.apache.marmotta.commons.collections.EquivalenceHashMap;
+import org.apache.marmotta.commons.collections.EquivalenceHashSet;
+import org.openrdf.model.Statement;
+
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Provide some utility functions for managing statements (e.g. different forms of equivalence)
+ *
+ * @author Sebastian Schaffert (sschaffert@apache.org)
+ */
+public class StatementCommons {
+
+ private final static Equivalence<Statement> TRIPLE_EQUIVALENCE = new Equivalence<Statement>() {
+ @Override
+ protected boolean doEquivalent(Statement a, Statement b) {
+ if(a == b) return true;
+
+ if(!Objects.equal(a.getSubject(), b.getSubject())) return false;
+ if(!Objects.equal(a.getPredicate(), b.getPredicate())) return false;
+ if(!Objects.equal(a.getObject(), b.getObject())) return false;
+ return true;
+ }
+
+ @Override
+ protected int doHash(Statement statement) {
+ return Objects.hashCode(statement.getSubject(), statement.getPredicate(), statement.getObject());
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ return this.getClass().equals(obj.getClass());
+ }
+
+ @Override
+ public int hashCode() {
+ return this.getClass().hashCode();
+ }
+ };
+
+
+ private final static Equivalence<Statement> QUADRUPLE_EQUIVALENCE = new Equivalence<Statement>() {
+ @Override
+ protected boolean doEquivalent(Statement a, Statement b) {
+ if(a == b) return true;
+
+ if(!Objects.equal(a.getSubject(), b.getSubject())) return false;
+ if(!Objects.equal(a.getPredicate(), b.getPredicate())) return false;
+ if(!Objects.equal(a.getObject(), b.getObject())) return false;
+ if(!Objects.equal(a.getContext(), b.getContext())) return false;
+ return true;
+ }
+
+ @Override
+ protected int doHash(Statement statement) {
+ return Objects.hashCode(statement.getSubject(), statement.getPredicate(), statement.getObject(), statement.getContext());
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ return this.getClass().equals(obj.getClass());
+ }
+
+ @Override
+ public int hashCode() {
+ return this.getClass().hashCode();
+ }
+ };
+
+ /**
+ * Return triple equivalence, taking only into account subject, predicate and object
+ *
+ * @return
+ */
+ public static Equivalence<Statement> tripleEquivalence() {
+ return TRIPLE_EQUIVALENCE;
+ }
+
+ /**
+ * Return quadruple equivalence, taking into account subject, predicate, object, and context.
+ *
+ * @return
+ */
+ public static Equivalence<Statement> quadrupleEquivalence() {
+ return QUADRUPLE_EQUIVALENCE;
+
+ }
+
+ /**
+ * Create a new set for statements using the triple equivalence (based on subject, predicate, object only)
+ * @param <T>
+ * @return
+ */
+ public static <T extends Statement> Set<T> newTripleSet() {
+ //return new EquivalenceHashSet<>(tripleEquivalence());
+ return new FastSet<>(equivalenceEquality(tripleEquivalence()));
+ }
+
+ /**
+ * Create a new set for statements using the triple equivalence (based on subject, predicate, object and context)
+ * @param <T>
+ * @return
+ */
+ public static <T extends Statement> Set<T> newQuadrupleSet() {
+ //return new EquivalenceHashSet<>(quadrupleEquivalence());
+ return new FastSet<>(equivalenceEquality(quadrupleEquivalence()));
+ }
+
+ /**
+ * Create a new map where the keys are statements and the equivalence relation used for keys is triple
+ * equivalence (based on subject, predicate, object only)
+ *
+ * @param <K>
+ * @param <V>
+ * @return
+ */
+ public static <K extends Statement, V> Map<K,V> newTripleMap() {
+ //return new EquivalenceHashMap<>(tripleEquivalence());
+ return new FastMap<>(equivalenceEquality(tripleEquivalence()));
+ }
+
+ /**
+ * Create a new map where the keys are statements and the equivalence relation used for keys is triple
+ * equivalence (based on subject, predicate, object only)
+ *
+ * @param <K>
+ * @param <V>
+ * @return
+ */
+ public static <K extends Statement, V> Map<K,V> newQuadrupleMap() {
+ //return new EquivalenceHashMap<>(quadrupleEquivalence());
+ return new FastMap<>(equivalenceEquality(quadrupleEquivalence()));
+ }
+
+
+
+ private static <E> Equality<E> equivalenceEquality(final Equivalence<E> equivalence) {
+ return new Equality<E>() {
+ @Override
+ public int hashCodeOf(E object) {
+ return equivalence.hash(object);
+ }
+
+ @Override
+ public boolean areEqual(E left, E right) {
+ return equivalence.equivalent(left, right);
+ }
+
+ @Override
+ public int compare(E left, E right) {
+ return equivalence.hash(left) - equivalence.hash(right);
+ }
+
+ @Override
+ public int hashCode() {
+ return equivalence.hashCode();
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ return obj.hashCode() == hashCode();
+ }
+ };
+ }
+}