Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
package com.bbn.bue.common.collections;
import com.bbn.bue.common.StringUtils;
import com.bbn.bue.common.collections.IterableUtils.ZipPair;
import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Ordering;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import static com.google.common.base.Preconditions.checkNotNull;
public final class MapUtils {
private MapUtils() {
throw new UnsupportedOperationException();
}
/**
* Pairs up the values of a map by their common keys.
*/
public static PairedMapValues zipValues(final Map left, final Map right) {
checkNotNull(left);
checkNotNull(right);
final ImmutableList.Builder> pairedValues =
ImmutableList.builder();
final ImmutableList.Builder leftOnly = ImmutableList.builder();
final ImmutableList.Builder rightOnly = ImmutableList.builder();
for (final Map.Entry leftEntry : left.entrySet()) {
final K key = leftEntry.getKey();
if (right.containsKey(key)) {
pairedValues.add(ZipPair.from(leftEntry.getValue(), right.get(key)));
} else {
leftOnly.add(leftEntry.getValue());
}
}
for (final Map.Entry rightEntry : right.entrySet()) {
if (!left.containsKey(rightEntry.getKey())) {
rightOnly.add(rightEntry.getValue());
}
}
return new PairedMapValues(pairedValues.build(), leftOnly.build(),
rightOnly.build());
}
/**
* Return a copy of the input map with keys transformed by {@code keyInjection} and values
* transformed by {@code valueFunction}. Beware: {@code keyInjection} must be an injection over
* all the keys of the input map. If two original keys are mapped to the same value, an {@link
* java.lang.IllegalArgumentException} will be returned.
*
* Neither {@code keyInjection} nor {@code valueFunction} may return null. If one does, an
* exception will be thrown.
*/
public static ImmutableMap copyWithTransformedEntries(Map input,
Function super K1, K2> keyInjection, Function super V1, V2> valueFunction) {
final ImmutableMap.Builder ret = ImmutableMap.builder();
for (final Map.Entry entry : input.entrySet()) {
ret.put(keyInjection.apply(entry.getKey()), valueFunction.apply(entry.getValue()));
}
return ret.build();
}
public static class PairedMapValues {
public PairedMapValues(final List> pairedValues, final List leftOnly,
final List rightOnly) {
this.pairedValues = ImmutableList.copyOf(pairedValues);
this.leftOnly = ImmutableList.copyOf(leftOnly);
this.rightOnly = ImmutableList.copyOf(rightOnly);
}
public List> pairedValues() {
return pairedValues;
}
public List leftOnly() {
return leftOnly;
}
public List rightOnly() {
return rightOnly;
}
public boolean perfectlyAligned() {
return leftOnly.isEmpty() && rightOnly.isEmpty();
}
private final List> pairedValues;
private final List leftOnly;
private final List rightOnly;
}
public static ImmutableSet allKeys(final Iterable extends Map> maps) {
final ImmutableSet.Builder builder = ImmutableSet.builder();
for (final Map map : maps) {
builder.addAll(map.keySet());
}
return builder.build();
}
public static Function, V> getEntryValue() {
return new Function, V>() {
@Override
public V apply(final Map.Entry entry) {
return entry.getValue();
}
};
}
public static Function, K> getEntryKey() {
return new Function, K>() {
@Override
public K apply(final Map.Entry entry) {
return entry.getKey();
}
};
}
public static Function