| 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 | package org.jclouds.collect; |
| 20 | |
| 21 | import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | |
| 23 | import java.util.AbstractMap; |
| 24 | import java.util.AbstractSet; |
| 25 | import java.util.Iterator; |
| 26 | import java.util.Map; |
| 27 | import java.util.Set; |
| 28 | |
| 29 | import com.google.common.base.Function; |
| 30 | |
| 31 | /** |
| 32 | * A map that transforms values on the way in and out. Inspired by the guava method |
| 33 | * {@code Maps.transformValues}. |
| 34 | * |
| 35 | * @author Adrian Cole |
| 36 | * |
| 37 | */ |
| 38 | public class TransformingMap<K, V1, V2> extends AbstractMap<K, V2> { |
| 39 | final Map<K, V1> fromMap; |
| 40 | final Function<? super V1, V2> getFunction; |
| 41 | final Function<? super V2, V1> putFunction; |
| 42 | |
| 43 | public TransformingMap(Map<K, V1> fromMap, Function<? super V1, V2> getFunction, Function<? super V2, V1> putFunction) { |
| 44 | this.fromMap = checkNotNull(fromMap); |
| 45 | this.getFunction = checkNotNull(getFunction); |
| 46 | this.putFunction = checkNotNull(putFunction); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public int size() { |
| 51 | return fromMap.size(); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public V2 put(K key, V2 value) { |
| 56 | V2 old = get(key); |
| 57 | fromMap.put(key, value != null ? putFunction.apply(value) : null); |
| 58 | return old; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public boolean containsKey(Object key) { |
| 63 | return fromMap.containsKey(key); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public V2 get(Object key) { |
| 68 | V1 value = fromMap.get(key); |
| 69 | return (value != null || fromMap.containsKey(key)) ? getFunction.apply(value) : null; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public V2 remove(Object key) { |
| 74 | return fromMap.containsKey(key) ? getFunction.apply(fromMap.remove(key)) : null; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void clear() { |
| 79 | fromMap.clear(); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public Set<Entry<K, V2>> entrySet() { |
| 84 | return new EntrySet(); |
| 85 | |
| 86 | } |
| 87 | |
| 88 | class EntrySet extends AbstractSet<Entry<K, V2>> { |
| 89 | @Override |
| 90 | public int size() { |
| 91 | return TransformingMap.this.size(); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public Iterator<Entry<K, V2>> iterator() { |
| 96 | final Iterator<Entry<K, V1>> mapIterator = fromMap.entrySet().iterator(); |
| 97 | |
| 98 | return new Iterator<Entry<K, V2>>() { |
| 99 | public boolean hasNext() { |
| 100 | return mapIterator.hasNext(); |
| 101 | } |
| 102 | |
| 103 | public Entry<K, V2> next() { |
| 104 | final Entry<K, V1> entry = mapIterator.next(); |
| 105 | return new AbstractMapEntry<K, V2>() { |
| 106 | @Override |
| 107 | public K getKey() { |
| 108 | return entry.getKey(); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public V2 getValue() { |
| 113 | return getFunction.apply(entry.getValue()); |
| 114 | } |
| 115 | }; |
| 116 | } |
| 117 | |
| 118 | public void remove() { |
| 119 | mapIterator.remove(); |
| 120 | } |
| 121 | }; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void clear() { |
| 126 | fromMap.clear(); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public boolean contains(Object o) { |
| 131 | if (!(o instanceof Entry)) { |
| 132 | return false; |
| 133 | } |
| 134 | Entry<?, ?> entry = (Entry<?, ?>) o; |
| 135 | Object entryKey = entry.getKey(); |
| 136 | Object entryValue = entry.getValue(); |
| 137 | V2 mapValue = TransformingMap.this.get(entryKey); |
| 138 | if (mapValue != null) { |
| 139 | return mapValue.equals(entryValue); |
| 140 | } |
| 141 | return entryValue == null && containsKey(entryKey); |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public boolean remove(Object o) { |
| 146 | if (contains(o)) { |
| 147 | Entry<?, ?> entry = (Entry<?, ?>) o; |
| 148 | Object key = entry.getKey(); |
| 149 | fromMap.remove(key); |
| 150 | return true; |
| 151 | } |
| 152 | return false; |
| 153 | } |
| 154 | } |
| 155 | } |