com.shigengyu.common.ListHashMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hyperion Show documentation
Show all versions of hyperion Show documentation
A lightweight workflow engine
The newest version!
/*******************************************************************************
* Copyright 2013-2014 Gengyu Shi
*
* 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 com.shigengyu.common;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.io.IOUtils;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
public class ListHashMap implements Iterable>> {
public static ListHashMap newListHashMap() {
return new ListHashMap();
}
private final Map> map = new ConcurrentHashMap>();
public ListHashMap addAll(Iterable values, Function keySelector) {
for (V value : values) {
K key = keySelector.apply(value);
addItem(key, value);
}
return this;
}
public ListHashMap addItem(K key, V value) {
if (!map.containsKey(key)) {
map.put(key, new ArrayList());
}
map.get(key).add(value);
return this;
}
public boolean containsItems(String key, V item) {
return map.containsKey(key) && map.get(key).contains(item);
}
public boolean containsKey(String key) {
return map.containsKey(key);
}
public List get(K key) {
List mutable = getMutable(key);
if (mutable == null) {
return null;
}
return ImmutableList.copyOf(mutable);
}
public List getMutable(K key) {
return map.get(key);
}
@Override
public Iterator>> iterator() {
return map.entrySet().iterator();
}
public List remove(String key) {
return map.remove(key);
}
public boolean removeItem(String key, V value) {
if (!map.containsKey(key)) {
return false;
}
return map.get(key).remove(value);
}
public int size() {
return map.size();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
int count = 0;
for (Entry> entry : map.entrySet()) {
sb.append(entry.getKey() + " (" + entry.getKey().hashCode() + ") = " + entry.getValue()
+ IOUtils.LINE_SEPARATOR);
++count;
if (count == 10) {
sb.append("...");
return sb.toString();
}
}
return sb.toString();
}
}