View Javadoc

1   /**
2    * Copyright (c) 2008-2012, http://www.snakeyaml.org
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.yaml.snakeyaml.issues.issue103;
17  
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.Iterator;
21  import java.util.Map;
22  import java.util.Set;
23  
24  /**
25   * Implements map interface, but behaves like collection. It just collects
26   * whatever you put(...) in here. Needed to track duplications in merge
27   * procedure.
28   * 
29   * @see issue100 & issue103
30   */
31  public class FakeMap<K, V> implements Map<K, V> {
32  
33      class FakeEntry implements java.util.Map.Entry<K, V> {
34  
35          private final K key;
36          private V val;
37  
38          public FakeEntry(K key, V val) {
39              this.key = key;
40              this.val = val;
41          }
42  
43          public K getKey() {
44              return key;
45          }
46  
47          public V getValue() {
48              return val;
49          }
50  
51          public V setValue(V newV) {
52              V old = val;
53              val = newV;
54              return old;
55          }
56  
57      }
58  
59      ArrayList<java.util.Map.Entry<K, V>> entries = new ArrayList<Map.Entry<K, V>>();
60  
61      public void clear() {
62          entries.clear();
63      }
64  
65      public boolean containsKey(Object arg0) {
66          for (java.util.Map.Entry<K, V> entry : entries) {
67              if (entry.getKey().equals(arg0))
68                  return true;
69          }
70          return false;
71      }
72  
73      public boolean containsValue(Object arg0) {
74          for (java.util.Map.Entry<K, V> entry : entries) {
75              if (entry.getValue().equals(arg0))
76                  return true;
77          }
78          return false;
79      }
80  
81      public Set<java.util.Map.Entry<K, V>> entrySet() {
82          throw new UnsupportedOperationException();
83      }
84  
85      public V get(Object arg0) {
86          for (java.util.Map.Entry<K, V> entry : entries) {
87              if (entry.getKey().equals(arg0))
88                  return entry.getValue();
89          }
90          return null;
91      }
92  
93      public boolean isEmpty() {
94          return values().isEmpty();
95      }
96  
97      public Set<K> keySet() {
98          throw new UnsupportedOperationException();
99      }
100 
101     public V put(K key, V val) {
102         entries.add(new FakeEntry(key, val));
103         return null;
104     }
105 
106     public void putAll(Map<? extends K, ? extends V> arg0) {
107         throw new UnsupportedOperationException();
108     }
109 
110     public V remove(Object arg0) {
111         for (Iterator<java.util.Map.Entry<K, V>> iter = entries.iterator(); iter.hasNext();) {
112             java.util.Map.Entry<K, V> entry = iter.next();
113             if (entry.getKey().equals(arg0)) {
114                 iter.remove();
115                 return entry.getValue();
116             }
117         }
118         return null;
119     }
120 
121     public int size() {
122         return entries.size();
123     }
124 
125     public Collection<V> values() {
126         throw new UnsupportedOperationException();
127     }
128 }