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