com.fastasyncworldedit.core.util.collection.AdaptedMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of FastAsyncWorldEdit-Core Show documentation
Show all versions of FastAsyncWorldEdit-Core Show documentation
Blazingly fast Minecraft world manipulation for artists, builders and everyone else.
package com.fastasyncworldedit.core.util.collection;
import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
@SuppressWarnings({"unchecked", "rawtypes"})
public class AdaptedMap implements IAdaptedMap {
private final Map parent;
private final Function value2;
private final Function key2;
private final Function value;
private final Function key;
private static final Function SAME = o -> o;
private static final Function IMMUTABLE = o -> {
throw new UnsupportedOperationException("Immutable");
};
public static AdaptedMap keys(Map parent, Function key, Function key2) {
return new AdaptedMap(parent, key, key2, SAME, SAME);
}
public static AdaptedMap values(Map parent, Function value, Function value2) {
return new AdaptedMap(parent, SAME, SAME, value, value2);
}
public static AdaptedMap immutable(
Map parent,
Function key,
Function value
) {
return new AdaptedMap(parent, IMMUTABLE, key, IMMUTABLE, value);
}
public AdaptedMap(
Map parent,
Function key,
Function key2,
Function value,
Function value2
) {
this.parent = parent;
this.key = key;
this.value = value;
this.key2 = key2;
this.value2 = value2;
}
@Override
public Map getParent() {
return this.parent;
}
@Override
public K2 adaptKey(K key) {
return this.key.apply(key);
}
@Override
public V2 adaptValue(V value) {
return this.value.apply(value);
}
@Override
public K adaptKey2(K2 key) {
return this.key2.apply(key);
}
@Override
public V adaptValue2(V2 value) {
return value2.apply(value);
}
@Nonnull
@Override
public Set> entrySet() {
if (isEmpty()) {
return Collections.emptySet();
}
return new AdaptedSetCollection<>(
getParent().entrySet(),
new com.google.common.base.Function<>() {
private final AdaptedPair entry = new AdaptedPair();
@Override
public Entry apply(@javax.annotation.Nullable Entry input) {
entry.input = input;
return entry;
}
}
);
}
public class AdaptedPair implements Entry {
private Entry input;
@Override
public K getKey() {
return adaptKey2(input.getKey());
}
@Override
public V getValue() {
return adaptValue2(input.getValue());
}
@Override
public V setValue(V value) {
return adaptValue2(input.setValue(adaptValue(value)));
}
@Override
public boolean equals(Object o) {
if (o instanceof Entry) {
return Objects.equals(((Entry) o).getKey(), getKey()) && Objects.equals(((Entry) o).getValue(), getValue());
}
return false;
}
@Override
public int hashCode() {
return 1337;
}
}
}