1 | /** |
2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
3 | * contributor license agreements. See the NOTICE file |
4 | * distributed with this work for additional information |
5 | * regarding copyright ownership. jclouds 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 | |
20 | package org.jclouds.collect; |
21 | |
22 | import static com.google.common.base.Preconditions.checkNotNull; |
23 | |
24 | import java.io.IOException; |
25 | import java.util.AbstractMap; |
26 | import java.util.AbstractSet; |
27 | import java.util.Iterator; |
28 | import java.util.Map; |
29 | import java.util.Set; |
30 | |
31 | import com.google.common.base.Function; |
32 | import com.google.common.base.Throwables; |
33 | import com.google.common.io.InputSupplier; |
34 | |
35 | /** |
36 | * |
37 | * |
38 | * @author Adrian Cole |
39 | * |
40 | */ |
41 | public class InputSupplierMap<K, V> extends AbstractMap<K, V> { |
42 | final Map<K, InputSupplier<V>> toMap; |
43 | final Function<V, InputSupplier<V>> putFunction; |
44 | |
45 | public InputSupplierMap(Map<K, InputSupplier<V>> toMap, Function<V, InputSupplier<V>> putFunction) { |
46 | this.toMap = checkNotNull(toMap); |
47 | this.putFunction = checkNotNull(putFunction); |
48 | } |
49 | |
50 | @Override |
51 | public int size() { |
52 | return toMap.size(); |
53 | } |
54 | |
55 | @Override |
56 | public V put(K key, V value) { |
57 | V old = get(key); |
58 | toMap.put(key, value != null ? putFunction.apply(value) : null); |
59 | return old; |
60 | } |
61 | |
62 | @Override |
63 | public boolean containsKey(Object key) { |
64 | return toMap.containsKey(key); |
65 | } |
66 | |
67 | @Override |
68 | public V get(Object key) { |
69 | InputSupplier<V> value = toMap.get(key); |
70 | try { |
71 | return (value != null || toMap.containsKey(key)) ? value.getInput() : null; |
72 | } catch (IOException e) { |
73 | Throwables.propagate(e); |
74 | return null; |
75 | } |
76 | } |
77 | |
78 | @Override |
79 | public V remove(Object key) { |
80 | try { |
81 | return toMap.containsKey(key) ? toMap.remove(key).getInput() : null; |
82 | } catch (IOException e) { |
83 | Throwables.propagate(e); |
84 | return null; |
85 | } |
86 | } |
87 | |
88 | @Override |
89 | public void clear() { |
90 | toMap.clear(); |
91 | } |
92 | |
93 | @Override |
94 | public Set<Entry<K, V>> entrySet() { |
95 | return new EntrySet(); |
96 | |
97 | } |
98 | |
99 | class EntrySet extends AbstractSet<Entry<K, V>> { |
100 | @Override |
101 | public int size() { |
102 | return InputSupplierMap.this.size(); |
103 | } |
104 | |
105 | @Override |
106 | public Iterator<Entry<K, V>> iterator() { |
107 | final Iterator<java.util.Map.Entry<K, InputSupplier<V>>> mapIterator = toMap.entrySet().iterator(); |
108 | |
109 | return new Iterator<Entry<K, V>>() { |
110 | public boolean hasNext() { |
111 | return mapIterator.hasNext(); |
112 | } |
113 | |
114 | public Entry<K, V> next() { |
115 | final java.util.Map.Entry<K, InputSupplier<V>> entry = mapIterator.next(); |
116 | return new AbstractMapEntry<K, V>() { |
117 | @Override |
118 | public K getKey() { |
119 | return entry.getKey(); |
120 | } |
121 | |
122 | @Override |
123 | public V getValue() { |
124 | try { |
125 | return entry.getValue().getInput(); |
126 | } catch (IOException e) { |
127 | Throwables.propagate(e); |
128 | return null; |
129 | } |
130 | } |
131 | }; |
132 | } |
133 | |
134 | public void remove() { |
135 | mapIterator.remove(); |
136 | } |
137 | }; |
138 | } |
139 | |
140 | @Override |
141 | public void clear() { |
142 | toMap.clear(); |
143 | } |
144 | |
145 | @Override |
146 | public boolean contains(Object o) { |
147 | if (!(o instanceof Entry)) { |
148 | return false; |
149 | } |
150 | Entry<?, ?> entry = (Entry<?, ?>) o; |
151 | Object entryKey = entry.getKey(); |
152 | Object entryValue = entry.getValue(); |
153 | V mapValue = InputSupplierMap.this.get(entryKey); |
154 | if (mapValue != null) { |
155 | return mapValue.equals(entryValue); |
156 | } |
157 | return entryValue == null && containsKey(entryKey); |
158 | } |
159 | |
160 | @Override |
161 | public boolean remove(Object o) { |
162 | if (contains(o)) { |
163 | Entry<?, ?> entry = (Entry<?, ?>) o; |
164 | Object key = entry.getKey(); |
165 | toMap.remove(key); |
166 | return true; |
167 | } |
168 | return false; |
169 | } |
170 | } |
171 | } |