1 package org.opendaylight.yangtools.triemap;
3 import java.util.Iterator;
4 import java.util.AbstractMap.SimpleImmutableEntry;
6 import java.util.Map.Entry;
9 * Mimic immutable ListMap in Scala
11 * @author Roman Levenstein <romixlev@gmail.com>
15 abstract class ListMap<K,V> {
19 static <K,V> ListMap<K, V> map(K k, V v, ListMap<K, V> tail){
20 return new Node<K, V> (k, v, tail);
23 static <K,V> ListMap<K, V> map(K k, V v){
24 return new Node<K, V> (k, v, null);
27 static <K,V> ListMap<K, V> map(K k1, V v1, K k2, V v2){
28 return new Node<K, V> (k1, v1, new Node<K,V>(k2,v2, null));
31 public abstract int size ();
33 public abstract boolean isEmpty() ;
35 abstract public boolean contains(K k, V v);
37 abstract public boolean contains(K key);
39 abstract public Option<V> get (K key);
41 abstract public ListMap<K, V> add (K key, V value);
43 abstract public ListMap<K, V> remove (K key);
45 abstract public Iterator<Map.Entry<K, V>> iterator();
48 static class EmptyListMap<K,V> extends ListMap<K, V> {
49 public ListMap<K,V> add (K key, V value) {
50 return ListMap.map(key, value, null);
53 public boolean contains(K k, V v) {
57 public boolean contains(K k) {
61 public ListMap<K,V> remove(K key) {
71 public boolean isEmpty () {
76 public Option<V> get (K key) {
77 return Option.makeOption (null);
81 public Iterator<Entry<K, V>> iterator () {
82 return new EmptyListMapIterator<K, V> ();
85 static class EmptyListMapIterator<K,V> implements Iterator<Entry<K, V>> {
88 public boolean hasNext () {
93 public Entry<K, V> next () {
98 public void remove () {
99 throw new RuntimeException("Operation not supported");
105 static class Node<K,V> extends ListMap<K, V> {
109 Node(K k, V v, ListMap<K,V> next) {
115 public ListMap<K,V> add (K key, V value) {
116 return ListMap.map(key, value, remove (key));
119 public boolean contains(K k, V v) {
120 if(k.equals (this.k) && v.equals (this.v))
122 else if(next != null)
123 return next.contains (k, v);
127 public boolean contains(K k) {
128 if(k.equals (this.k))
130 else if(next != null)
131 return next.contains (k);
135 public ListMap<K,V> remove(K key) {
142 private ListMap<K, V> remove0 (K key) {
143 ListMap<K, V> n = this;
144 ListMap<K, V> newN = null;
145 ListMap<K, V> lastN = null;
147 if(n instanceof EmptyListMap) {
151 Node<K, V> nn = (Node<K, V>)n;
152 if (key.equals (nn.k)) {
157 lastN.next = ListMap.map (nn.k, nn.v, null);
160 newN = ListMap.map (nn.k, nn.v, null);
174 return 1+next.size ();
178 public boolean isEmpty () {
183 public Option<V> get (K key) {
185 return Option.makeOption (v);
187 return next.get (key);
188 return Option.makeOption (null);
193 public Iterator<Entry<K, V>> iterator () {
194 return new NodeIterator<K, V> (this);
197 static class NodeIterator<K,V> implements Iterator<Entry<K, V>> {
200 public NodeIterator (Node<K, V> n) {
205 public boolean hasNext () {
206 // return n!= null && n.next != null && !(n.next instanceof EmptyListMap);
207 return n!= null && !(n instanceof EmptyListMap);
211 public Entry<K, V> next () {
212 if (n instanceof Node) {
213 Node<K, V> nn = (Node<K, V>) n;
214 Entry<K, V> res = new SimpleImmutableEntry<K, V> (nn.k, nn.v);
223 public void remove () {
224 throw new RuntimeException("Operation not supported");