| 1 | /** |
| 2 | * |
| 3 | * Copyright (C) 2011 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 4 | * |
| 5 | * ==================================================================== |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * 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, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * ==================================================================== |
| 18 | */ |
| 19 | /** |
| 20 | * |
| 21 | * Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com> |
| 22 | * |
| 23 | * ==================================================================== |
| 24 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 25 | * you may not use this file except in compliance with the License. |
| 26 | * You may obtain a copy of the License at |
| 27 | * |
| 28 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 29 | * |
| 30 | * Unless required by applicable law or agreed to in writing, software |
| 31 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 32 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 33 | * See the License for the specific language governing permissions and |
| 34 | * limitations under the License. |
| 35 | * ==================================================================== |
| 36 | */ |
| 37 | |
| 38 | |
| 39 | package org.jclouds.collect; |
| 40 | |
| 41 | import static com.google.common.base.Preconditions.checkNotNull; |
| 42 | |
| 43 | import java.io.IOException; |
| 44 | import java.util.AbstractMap; |
| 45 | import java.util.AbstractSet; |
| 46 | import java.util.Iterator; |
| 47 | import java.util.Map; |
| 48 | import java.util.Set; |
| 49 | |
| 50 | import com.google.common.base.Function; |
| 51 | import com.google.common.base.Throwables; |
| 52 | import com.google.common.io.InputSupplier; |
| 53 | |
| 54 | /** |
| 55 | * |
| 56 | * |
| 57 | * @author Adrian Cole |
| 58 | * |
| 59 | */ |
| 60 | public class InputSupplierMap<K, V> extends AbstractMap<K, V> { |
| 61 | final Map<K, InputSupplier<V>> toMap; |
| 62 | final Function<V, InputSupplier<V>> putFunction; |
| 63 | |
| 64 | public InputSupplierMap(Map<K, InputSupplier<V>> toMap, Function<V, InputSupplier<V>> putFunction) { |
| 65 | this.toMap = checkNotNull(toMap); |
| 66 | this.putFunction = checkNotNull(putFunction); |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public int size() { |
| 71 | return toMap.size(); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public V put(K key, V value) { |
| 76 | V old = get(key); |
| 77 | toMap.put(key, value != null ? putFunction.apply(value) : null); |
| 78 | return old; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public boolean containsKey(Object key) { |
| 83 | return toMap.containsKey(key); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public V get(Object key) { |
| 88 | InputSupplier<V> value = toMap.get(key); |
| 89 | try { |
| 90 | return (value != null || toMap.containsKey(key)) ? value.getInput() : null; |
| 91 | } catch (IOException e) { |
| 92 | Throwables.propagate(e); |
| 93 | return null; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public V remove(Object key) { |
| 99 | try { |
| 100 | return toMap.containsKey(key) ? toMap.remove(key).getInput() : null; |
| 101 | } catch (IOException e) { |
| 102 | Throwables.propagate(e); |
| 103 | return null; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void clear() { |
| 109 | toMap.clear(); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public Set<Entry<K, V>> entrySet() { |
| 114 | return new EntrySet(); |
| 115 | |
| 116 | } |
| 117 | |
| 118 | class EntrySet extends AbstractSet<Entry<K, V>> { |
| 119 | @Override |
| 120 | public int size() { |
| 121 | return InputSupplierMap.this.size(); |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public Iterator<Entry<K, V>> iterator() { |
| 126 | final Iterator<java.util.Map.Entry<K, InputSupplier<V>>> mapIterator = toMap.entrySet().iterator(); |
| 127 | |
| 128 | return new Iterator<Entry<K, V>>() { |
| 129 | public boolean hasNext() { |
| 130 | return mapIterator.hasNext(); |
| 131 | } |
| 132 | |
| 133 | public Entry<K, V> next() { |
| 134 | final java.util.Map.Entry<K, InputSupplier<V>> entry = mapIterator.next(); |
| 135 | return new AbstractMapEntry<K, V>() { |
| 136 | @Override |
| 137 | public K getKey() { |
| 138 | return entry.getKey(); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public V getValue() { |
| 143 | try { |
| 144 | return entry.getValue().getInput(); |
| 145 | } catch (IOException e) { |
| 146 | Throwables.propagate(e); |
| 147 | return null; |
| 148 | } |
| 149 | } |
| 150 | }; |
| 151 | } |
| 152 | |
| 153 | public void remove() { |
| 154 | mapIterator.remove(); |
| 155 | } |
| 156 | }; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void clear() { |
| 161 | toMap.clear(); |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public boolean contains(Object o) { |
| 166 | if (!(o instanceof Entry)) { |
| 167 | return false; |
| 168 | } |
| 169 | Entry<?, ?> entry = (Entry<?, ?>) o; |
| 170 | Object entryKey = entry.getKey(); |
| 171 | Object entryValue = entry.getValue(); |
| 172 | V mapValue = InputSupplierMap.this.get(entryKey); |
| 173 | if (mapValue != null) { |
| 174 | return mapValue.equals(entryValue); |
| 175 | } |
| 176 | return entryValue == null && containsKey(entryKey); |
| 177 | } |
| 178 | |
| 179 | @Override |
| 180 | public boolean remove(Object o) { |
| 181 | if (contains(o)) { |
| 182 | Entry<?, ?> entry = (Entry<?, ?>) o; |
| 183 | Object key = entry.getKey(); |
| 184 | toMap.remove(key); |
| 185 | return true; |
| 186 | } |
| 187 | return false; |
| 188 | } |
| 189 | } |
| 190 | } |