View Javadoc

    1   /**
    2    * Copyright (c) 2008-2011, 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
    17  package org.yaml.snakeyaml.issues.issue103;
    18
    19  import java.util.ArrayList;
    20  import java.util.Collection;
    21  import java.util.Iterator;
    22  import java.util.Map;
    23  import java.util.Set;
    24
    25  /**
    26   * Implements map interface, but behaves like collection. It just collects
    27   * whatever you put(...) in here. Needed to track duplications in merge
    28   * procedure.
    29   *
    30   * @see issue100 & issue103
    31   */
    32  public class FakeMap<K, V> implements Map<K, V> {
    33
    34      class FakeEntry implements java.util.Map.Entry<K, V> {
    35
    36          private final K key;
    37          private V val;
    38
    39          public FakeEntry(K key, V val) {
    40              this.key = key;
    41              this.val = val;
    42          }
    43
    44          public K getKey() {
    45              return key;
    46          }
    47
    48          public V getValue() {
    49              return val;
    50          }
    51
    52          public V setValue(V newV) {
    53              V old = val;
    54              val = newV;
    55              return old;
    56          }
    57
    58      }
    59
    60      ArrayList<java.util.Map.Entry<K, V>> entries = new ArrayList<Map.Entry<K, V>>();
    61
    62      public void clear() {
    63          entries.clear();
    64      }
    65
    66      public boolean containsKey(Object arg0) {
    67          for (java.util.Map.Entry<K, V> entry : entries) {
    68              if (entry.getKey().equals(arg0))
    69                  return true;
    70          }
    71          return false;
    72      }
    73
    74      public boolean containsValue(Object arg0) {
    75          for (java.util.Map.Entry<K, V> entry : entries) {
    76              if (entry.getValue().equals(arg0))
    77                  return true;
    78          }
    79          return false;
    80      }
    81
    82      public Set<java.util.Map.Entry<K, V>> entrySet() {
    83          throw new UnsupportedOperationException();
    84      }
    85
    86      public V get(Object arg0) {
    87          for (java.util.Map.Entry<K, V> entry : entries) {
    88              if (entry.getKey().equals(arg0))
    89                  return entry.getValue();
    90          }
    91          return null;
    92      }
    93
    94      public boolean isEmpty() {
    95          return values().isEmpty();
    96      }
    97
    98      public Set<K> keySet() {
    99          throw new UnsupportedOperationException();
    100     }
    101
    102     public V put(K key, V val) {
    103         entries.add(new FakeEntry(key, val));
    104         return null;
    105     }
    106
    107     public void putAll(Map<? extends K, ? extends V> arg0) {
    108         throw new UnsupportedOperationException();
    109     }
    110
    111     public V remove(Object arg0) {
    112         for (Iterator<java.util.Map.Entry<K, V>> iter = entries.iterator(); iter.hasNext();) {
    113             java.util.Map.Entry<K, V> entry = iter.next();
    114             if (entry.getKey().equals(arg0)) {
    115                 iter.remove();
    116                 return entry.getValue();
    117             }
    118         }
    119         return null;
    120     }
    121
    122     public int size() {
    123         return entries.size();
    124     }
    125
    126     public Collection<V> values() {
    127         throw new UnsupportedOperationException();
    128     }
    129 }