io.activej.serializer.stream.SizedCollectorsKV Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of activej-serializer Show documentation
Show all versions of activej-serializer Show documentation
Extremely fast and space-efficient serializers. Implemented using bytecode engineering.
package io.activej.serializer.stream;
import io.activej.common.Utils;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.IntFunction;
import static io.activej.common.Utils.newHashMap;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
public class SizedCollectorsKV {
public static > SizedCollectorKV toMap(IntFunction extends M> factory) {
return new SizedCollectorKV() {
@Override
public M accumulator(int size) {
return factory.apply(size);
}
@Override
public void accumulate(M map, int index, K key, V value) {
map.put(key, value);
}
@Override
public M result(M map) {
return map;
}
};
}
public static SizedCollectorKV> toMap() {
return new SizedCollectorKV, Map>() {
@Override
public Map create0() {
return emptyMap();
}
@Override
public Map create1(K key, V value) {
return singletonMap(key, value);
}
@Override
public HashMap accumulator(int size) {
return newHashMap(size);
}
@Override
public void accumulate(HashMap accumulator, int index, K key, V value) {
accumulator.put(key, value);
}
@Override
public Map result(HashMap accumulator) {
return accumulator;
}
};
}
public static SizedCollectorKV> toHashMap() {
return toMap(Utils::newHashMap);
}
public static SizedCollectorKV> toLinkedHashMap() {
return toMap(Utils::newLinkedHashMap);
}
public static , V> SizedCollectorKV> toEnumMap(Class type) {
return toMap(size -> new EnumMap<>(type));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy