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

io.datakernel.crdt.primitives.LWWObject 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.Nullable;

public class LWWObject implements CrdtType> {
	private final long timestamp;
	private final T object;

	public LWWObject(long timestamp, T object) {
		this.timestamp = timestamp;
		this.object = object;
	}

	public T getObject() {
		return object;
	}

	public long getTimestamp() {
		return timestamp;
	}

	@Override
	public LWWObject merge(LWWObject other) {
		if (timestamp < other.timestamp) {
			return other;
		}
		return this;
	}

	@Override
	@Nullable
	public LWWObject extract(long timestamp) {
		if (timestamp >= this.timestamp) {
			return null;
		}
		return this;
	}

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

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

		@Override
		public void encode(BinaryOutput out, LWWObject item) {
			out.writeLong(item.timestamp);
			valueSerializer.encode(out, item.object);
		}

		@Override
		public LWWObject decode(BinaryInput in) {
			return new LWWObject<>(in.readLong(), valueSerializer.decode(in));
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy