View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.orchestra.dynaForm.lib;
20  
21  import java.io.Serializable;
22  import java.util.Collection;
23  import java.util.Map;
24  import java.util.Set;
25  
26  public class DefaultValueMap<K, V> implements Map<K, V>, Serializable
27  {
28      private static final long serialVersionUID = -1311019539599397096L;
29  
30      private final Map<K, V> original;
31      private final DefaultValueFactory<V> defaultValueFactory;
32  
33      public static interface DefaultValueFactory<V> extends Serializable
34      {
35          public V create();
36      }
37  
38      public DefaultValueMap(Map<K, V> original, DefaultValueFactory<V> defaultValueFactory)
39      {
40          this.original = original;
41          this.defaultValueFactory = defaultValueFactory;
42      }
43  
44      public void clear()
45      {
46          original.clear();
47      }
48  
49      public boolean containsKey(Object key)
50      {
51          return original.containsKey(key);
52      }
53  
54      public boolean containsValue(Object value)
55      {
56          return original.containsValue(value);
57      }
58  
59      public Set<Entry<K, V>> entrySet()
60      {
61          return original.entrySet();
62      }
63  
64      public boolean equals(Object o)
65      {
66          return original.equals(o);
67      }
68  
69      @SuppressWarnings("unchecked")
70      public V get(Object key)
71      {
72          V ret = original.get(key);
73          if (ret == null)
74          {
75              ret = defaultValueFactory.create();
76              original.put((K) key, ret);
77          }
78  
79          return ret;
80      }
81  
82      public int hashCode()
83      {
84          return original.hashCode();
85      }
86  
87      public boolean isEmpty()
88      {
89          return original.isEmpty();
90      }
91  
92      public Set<K> keySet()
93      {
94          return original.keySet();
95      }
96  
97      public V put(K key, V value)
98      {
99          return original.put(key, value);
100     }
101 
102     public void putAll(Map<? extends K, ? extends V> t)
103     {
104         original.putAll(t);
105     }
106 
107     public V remove(Object key)
108     {
109         return original.remove(key);
110     }
111 
112     public int size()
113     {
114         return original.size();
115     }
116 
117     public Collection<V> values()
118     {
119         return original.values();
120     }
121 }