All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.activej.serializer.stream.SizedCollectorsKV Maven / Gradle / Ivy

Go to download

Extremely fast and space-efficient serializers. Implemented using bytecode engineering.

There is a newer version: 6.0-rc2
Show newest version
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 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