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

io.datakernel.crdt.primitives.GMap Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
package io.datakernel.crdt.primitives;

import io.datakernel.serializer.BinarySerializer;
import io.datakernel.serializer.util.BinaryInput;
import io.datakernel.serializer.util.BinaryOutput;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public final class GMap> implements Map, CrdtMergable> {

	private final Map map;

	private GMap(Map map) {
		this.map = map;
	}

	public GMap() {
		this(new HashMap<>());
	}

	@Override
	public GMap merge(GMap other) {
		HashMap newMap = new HashMap<>(map);
		other.map.forEach((k, v) -> newMap.merge(k, v, CrdtMergable::merge));
		return new GMap<>(newMap);
	}

	@Override
	public int size() {
		return map.size();
	}

	@Override
	public boolean isEmpty() {
		return map.isEmpty();
	}

	@Override
	public boolean containsKey(Object key) {
		return map.containsKey(key);
	}

	@Override
	public boolean containsValue(Object value) {
		return map.containsValue(value);
	}

	@Override
	public V get(Object key) {
		return map.get(key);
	}

	@Nullable
	@Override
	public V put(K key, V value) {
		return map.merge(key, value, CrdtMergable::merge);
	}

	@Override
	public V remove(Object key) {
		throw new UnsupportedOperationException("GMap is a grow-only map");
	}

	@Override
	public void putAll(@NotNull Map m) {
		m.forEach(this::put);
	}

	@Override
	public void clear() {
		throw new UnsupportedOperationException("GMap is a grow-only map");
	}

	@NotNull
	@Override
	public Set keySet() {
		throw new UnsupportedOperationException("GMap#keySet is not implemented yet");
	}

	@NotNull
	@Override
	public Collection values() {
		throw new UnsupportedOperationException("GMap#values is not implemented yet");
	}

	@NotNull
	@Override
	public Set> entrySet() {
		throw new UnsupportedOperationException("GMap#entrySet is not implemented yet");
	}

	public static class Serializer> implements BinarySerializer> {
		private final BinarySerializer keySerializer;
		private final BinarySerializer valueSerializer;

		public Serializer(BinarySerializer keySerializer, BinarySerializer valueSerializer) {
			this.keySerializer = keySerializer;
			this.valueSerializer = valueSerializer;
		}

		@Override
		public void encode(BinaryOutput out, GMap item) {
			out.writeVarInt(item.map.size());
			for (Entry entry : item.map.entrySet()) {
				keySerializer.encode(out, entry.getKey());
				valueSerializer.encode(out, entry.getValue());
			}
		}

		@Override
		public GMap decode(BinaryInput in) {
			int size = in.readVarInt();
			Map map = new HashMap<>(size);
			for (int i = 0; i < size; i++) {
				map.put(keySerializer.decode(in), valueSerializer.decode(in));
			}
			return new GMap<>(map);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy