/*
- * (C) Copyright 2016 Pantheon Technologies, s.r.o. and others.
+ * (C) Copyright 2017 Pantheon Technologies, s.r.o. and others.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
import java.util.Map.Entry;
-interface KVNode<K, V> {
- Entry<K, V> kvPair();
+/**
+ * Common marker interface for nodes which act as an immutable {@link Entry}.
+ *
+ * @author Robert Varga
+ *
+ * @param <K> the type of key
+ * @param <V> the type of value
+ */
+interface EntryNode<K, V> extends Entry<K, V> {
+ @Override
+ default public V setValue(final V value) {
+ throw new UnsupportedOperationException();
+ }
}
--- /dev/null
+/*
+ * (C) Copyright 2017 Pantheon Technologies, s.r.o. and others.
+ *
+ * Licensed 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.opendaylight.yangtools.triemap;
+
+import java.util.Map.Entry;
+
+/**
+ * Utility methods for implementing {@link Entry} contract.
+ *
+ * @author Robert Varga
+ */
+final class EntryUtil {
+ private EntryUtil() {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * Utility implementing {@link Entry#equals(Object)}.
+ */
+ static boolean equal(final Object o, final Object key, final Object value) {
+ if (!(o instanceof Entry)) {
+ return false;
+ }
+
+ final Entry<?,?> e = (Entry<?,?>)o;
+ return key.equals(e.getKey()) && value.equals(e.getValue());
+ }
+
+ /**
+ * Utility implementing {@link Entry#hashCode()}.
+ */
+ static int hash(final Object key, final Object value) {
+ return key.hashCode() ^ value.hashCode();
+ }
+
+ /**
+ * Utility implementing {@link Entry#toString()}.
+ */
+ static String string(final Object key, final Object value) {
+ return key + "=" + value;
+ }
+}
*/
package org.opendaylight.yangtools.triemap;
-import java.util.AbstractMap.SimpleImmutableEntry;
-import java.util.Map.Entry;
-
-final class SNode<K, V> extends BasicNode implements KVNode<K, V> {
+final class SNode<K, V> extends BasicNode implements EntryNode<K, V> {
final K k;
final V v;
final int hc;
return new SNode<>(k, v, hc);
}
- @Override
- public Entry<K, V> kvPair() {
- return new SimpleImmutableEntry<>(k, v);
- }
-
@Override
String string(final int lev) {
// (" " * lev) + "SNode(%s, %s, %x)".format(k, v, hc);
return "SNode";
}
+
+ @Override
+ public K getKey() {
+ return k;
+ }
+
+ @Override
+ public V getValue() {
+ return v;
+ }
+
+ @Override
+ public int hashCode() {
+ return EntryUtil.hash(k, v);
+ }
+
+ @Override
+ public boolean equals(final Object o) {
+ return EntryUtil.equal(o, k, v);
+ }
+
+ @Override
+ public String toString() {
+ return EntryUtil.string(k, v);
+ }
}
\ No newline at end of file
*/
package org.opendaylight.yangtools.triemap;
-import java.util.AbstractMap.SimpleImmutableEntry;
-import java.util.Map.Entry;
-
-final class TNode<K, V> extends MainNode<K, V> implements KVNode<K, V> {
+final class TNode<K, V> extends MainNode<K, V> implements EntryNode<K, V> {
final K k;
final V v;
final int hc;
return new SNode<>(k, v, hc);
}
- @Override
- public Entry<K, V> kvPair () {
- return new SimpleImmutableEntry<>(k, v);
- }
-
@Override
int cachedSize(final TrieMap<K, V> ct) {
return 1;
// (" " * lev) + "TNode(%s, %s, %x, !)".format(k, v, hc);
return "TNode";
}
+
+ @Override
+ public K getKey() {
+ return k;
+ }
+
+ @Override
+ public V getValue() {
+ return v;
+ }
+
+ @Override
+ public int hashCode() {
+ return EntryUtil.hash(k, v);
+ }
+
+ @Override
+ public boolean equals(final Object o) {
+ return EntryUtil.equal(o, k, v);
+ }
+
+ @Override
+ public String toString() {
+ return EntryUtil.string(k, v);
+ }
}
\ No newline at end of file
private final int[] stackpos = new int[7];
private int depth = -1;
private Iterator<Entry<K, V>> subiter = null;
- private KVNode<K, V> current = null;
+ private EntryNode<K, V> current = null;
private Entry<K, V> lastReturned = null;
TrieMapIterator (final int level, final TrieMap<K, V> ct, final boolean mustInit) {
r = subiter.next ();
checkSubiter ();
} else {
- r = current.kvPair ();
+ r = current;
advance ();
}
import org.junit.Test;
-public class ListMapTest {
+public class LNodeEntriesTest {
/**
* Test if Listmap.get() does not cause stack overflow.
*/