jetbrick.collection.multimap.AbstractMultiValueMap Maven / Gradle / Ivy
/**
* Copyright 2013-2016 Guoqiang Chen, Shanghai, China. All rights reserved.
*
* Author: Guoqiang Chen
* Email: [email protected]
* WebURL: https://github.com/subchen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package jetbrick.collection.multimap;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public abstract class AbstractMultiValueMap implements MultiValueMap, Serializable {
private static final long serialVersionUID = 1L;
private final Map> map;
protected AbstractMultiValueMap(Map> map) {
this.map = map;
}
@Override
public int size() {
int total = 0;
for (Map.Entry> entry : map.entrySet()) {
total += entry.getValue().size();
}
return total;
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return map.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
for (Map.Entry> entry : map.entrySet()) {
if (entry.getValue().contains(value)) {
return true;
}
}
return false;
}
@Override
public V get(Object key) {
List values = map.get(key);
return values != null ? values.get(0) : null;
}
@Override
public V put(K key, V value) {
List values = map.get(key);
if (values == null) {
values = new ArrayList();
map.put(key, values);
}
values.add(value);
return null;
}
@Override
public V remove(Object key) {
map.remove(key);
return null;
}
@Override
public void putAll(Map extends K, ? extends V> m) {
for (Map.Entry extends K, ? extends V> entry : m.entrySet()) {
put(entry.getKey(), entry.getValue());
}
}
@Override
public void clear() {
map.clear();
}
@Override
public Set keySet() {
return map.keySet();
}
@Override
public Collection values() {
List values = new ArrayList(map.size() * 3);
for (Map.Entry> entry : map.entrySet()) {
values.addAll(entry.getValue());
}
return values;
}
@Override
public Set> entrySet() {
Set> entrys = new HashSet>(map.size() * 3);
for (final Map.Entry> entry : map.entrySet()) {
final K key = entry.getKey();
for (final V value : entry.getValue()) {
entrys.add(new Map.Entry() {
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
@Override
public V setValue(V value) {
throw new UnsupportedOperationException();
}
});
}
}
return entrys;
}
@Override
public boolean containsValue(Object key, Object value) {
List values = map.get(key);
return values == null ? false : values.contains(value);
}
@Override
public Collection> valuesList() {
return map.values();
}
@Override
public List getList(Object key) {
return map.get(key);
}
@Override
public Set>> multiEntrySet() {
return map.entrySet();
}
@Override
public boolean equals(Object o) {
return this == o || map.equals(o);
}
@Override
public int hashCode() {
return map.hashCode();
}
@Override
public String toString() {
return map.toString();
}
}