1 package org.opendaylight.yangtools.triemap;
3 import java.util.Iterator;
5 import java.util.Map.Entry;
8 * Mimic immutable ListMap in Scala
10 * @author Roman Levenstein <romixlev@gmail.com>
14 abstract class ListMap<K,V> {
18 static <K,V> ListMap<K, V> map(K k, V v, ListMap<K, V> tail){
19 return new Node<K, V> (k, v, tail);
22 static <K,V> ListMap<K, V> map(K k, V v){
23 return new Node<K, V> (k, v, null);
26 static <K,V> ListMap<K, V> map(K k1, V v1, K k2, V v2){
27 return new Node<K, V> (k1, v1, new Node<K,V>(k2,v2, null));
30 public abstract int size ();
32 public abstract boolean isEmpty() ;
34 abstract public boolean contains(K k, V v);
36 abstract public boolean contains(K key);
38 abstract public Option<V> get (K key);
40 abstract public ListMap<K, V> add (K key, V value);
42 abstract public ListMap<K, V> remove (K key);
44 abstract public Iterator<Map.Entry<K, V>> iterator();
47 static class EmptyListMap<K,V> extends ListMap<K, V> {
48 public ListMap<K,V> add (K key, V value) {
49 return ListMap.map(key, value, null);
52 public boolean contains(K k, V v) {
56 public boolean contains(K k) {
60 public ListMap<K,V> remove(K key) {
70 public boolean isEmpty () {
75 public Option<V> get (K key) {
76 return Option.makeOption (null);
80 public Iterator<Entry<K, V>> iterator () {
81 return new EmptyListMapIterator<K, V> ();
84 static class EmptyListMapIterator<K,V> implements Iterator<Entry<K, V>> {
87 public boolean hasNext () {
92 public Entry<K, V> next () {
97 public void remove () {
98 throw new RuntimeException("Operation not supported");
104 static class Node<K,V> extends ListMap<K, V> {
108 Node(K k, V v, ListMap<K,V> next) {
114 public ListMap<K,V> add (K key, V value) {
115 return ListMap.map(key, value, remove (key));
118 public boolean contains(K k, V v) {
119 if(k.equals (this.k) && v.equals (this.v))
121 else if(next != null)
122 return next.contains (k, v);
126 public boolean contains(K k) {
127 if(k.equals (this.k))
129 else if(next != null)
130 return next.contains (k);
134 public ListMap<K,V> remove(K key) {
141 private ListMap<K, V> remove0 (K key) {
142 ListMap<K, V> n = this;
143 ListMap<K, V> newN = null;
144 ListMap<K, V> lastN = null;
146 if(n instanceof EmptyListMap) {
150 Node<K, V> nn = (Node<K, V>)n;
151 if (key.equals (nn.k)) {
156 lastN.next = ListMap.map (nn.k, nn.v, null);
159 newN = ListMap.map (nn.k, nn.v, null);
173 return 1+next.size ();
177 public boolean isEmpty () {
182 public Option<V> get (K key) {
184 return Option.makeOption (v);
186 return next.get (key);
187 return Option.makeOption (null);
192 public Iterator<Entry<K, V>> iterator () {
193 return new NodeIterator<K, V> (this);
196 static class NodeIterator<K,V> implements Iterator<Entry<K, V>> {
199 public NodeIterator (Node<K, V> n) {
204 public boolean hasNext () {
205 // return n!= null && n.next != null && !(n.next instanceof EmptyListMap);
206 return n!= null && !(n instanceof EmptyListMap);
210 public Entry<K, V> next () {
211 if (n instanceof Node) {
212 Node<K, V> nn = (Node<K, V>) n;
213 Pair<K, V> res = new Pair<K, V> (nn.k, nn.v);
222 public void remove () {
223 throw new RuntimeException("Operation not supported");