Nothing in this package is needed here, remove it.
JIRA: CONTROLLER-2071
Change-Id: I92b26b9010bbf00fe7f2b9d3ddb9b380b06c80d9
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
import io.atomix.utils.serializer.serializers.ImmutableListSerializer;
import io.atomix.utils.serializer.serializers.ImmutableMapSerializer;
import io.atomix.utils.serializer.serializers.ImmutableSetSerializer;
-import io.atomix.utils.time.LogicalTimestamp;
-import io.atomix.utils.time.WallClockTimestamp;
import java.time.Duration;
import java.util.ArrayDeque;
.register(String[].class)
.register(boolean[].class)
.register(Object[].class)
- .register(LogicalTimestamp.class)
- .register(WallClockTimestamp.class)
.register(Version.class)
.build("BASIC");
+++ /dev/null
-/*
- * Copyright 2017-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.atomix.utils.time;
-
-/**
- * Clock.
- */
-public interface Clock<T extends Timestamp> {
-
- /**
- * Returns the current time of the clock.
- *
- * @return the current time
- */
- T getTime();
-
-}
+++ /dev/null
-/*
- * Copyright 2017-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.atomix.utils.time;
-
-/**
- * Epoch.
- * <p>
- * An epoch is a specific type of {@link LogicalTimestamp} that represents a long term section of logical time.
- */
-public class Epoch extends LogicalTimestamp {
-
- /**
- * Returns a new logical timestamp for the given logical time.
- *
- * @param value the logical time for which to create a new logical timestamp
- * @return the logical timestamp
- */
- public static Epoch of(long value) {
- return new Epoch(value);
- }
-
- /**
- * Creates a new epoch timestamp.
- *
- * @param value the epoch value
- */
- public Epoch(long value) {
- super(value);
- }
-
-}
+++ /dev/null
-/*
- * Copyright 2017-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.atomix.utils.time;
-
-import static com.google.common.base.MoreObjects.toStringHelper;
-
-/**
- * Logical clock.
- */
-public class LogicalClock implements Clock<LogicalTimestamp> {
- private LogicalTimestamp currentTimestamp;
-
- public LogicalClock() {
- this(new LogicalTimestamp(0));
- }
-
- public LogicalClock(LogicalTimestamp currentTimestamp) {
- this.currentTimestamp = currentTimestamp;
- }
-
- @Override
- public LogicalTimestamp getTime() {
- return currentTimestamp;
- }
-
- /**
- * Increments the clock and returns the new timestamp.
- *
- * @return the updated clock time
- */
- public LogicalTimestamp increment() {
- return update(new LogicalTimestamp(currentTimestamp.value() + 1));
- }
-
- /**
- * Updates the clock using the given timestamp.
- *
- * @param timestamp the timestamp with which to update the clock
- * @return the updated clock time
- */
- public LogicalTimestamp update(LogicalTimestamp timestamp) {
- if (timestamp.value() > currentTimestamp.value()) {
- this.currentTimestamp = timestamp;
- }
- return currentTimestamp;
- }
-
- /**
- * Increments the clock and updates it using the given timestamp.
- *
- * @param timestamp the timestamp with which to update the clock
- * @return the updated clock time
- */
- public LogicalTimestamp incrementAndUpdate(LogicalTimestamp timestamp) {
- long nextValue = currentTimestamp.value() + 1;
- if (timestamp.value() > nextValue) {
- return update(timestamp);
- }
- return increment();
- }
-
- @Override
- public String toString() {
- return toStringHelper(this)
- .add("time", getTime())
- .toString();
- }
-}
+++ /dev/null
-/*
- * Copyright 2016-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.atomix.utils.time;
-
-import com.google.common.base.Preconditions;
-import com.google.common.collect.ComparisonChain;
-
-import java.util.Objects;
-
-import static com.google.common.base.MoreObjects.toStringHelper;
-
-/**
- * Timestamp based on logical sequence value.
- * <p>
- * LogicalTimestamps are ordered by their sequence values.
- */
-public class LogicalTimestamp implements Timestamp {
-
- /**
- * Returns a new logical timestamp for the given logical time.
- *
- * @param value the logical time for which to create a new logical timestamp
- * @return the logical timestamp
- */
- public static LogicalTimestamp of(long value) {
- return new LogicalTimestamp(value);
- }
-
- private final long value;
-
- public LogicalTimestamp(long value) {
- this.value = value;
- }
-
- /**
- * Returns the sequence value.
- *
- * @return sequence value
- */
- public long value() {
- return this.value;
- }
-
- /**
- * Returns the timestamp as a version.
- *
- * @return the timestamp as a version
- */
- public Version asVersion() {
- return new Version(value);
- }
-
- @Override
- public int compareTo(Timestamp o) {
- Preconditions.checkArgument(o instanceof LogicalTimestamp,
- "Must be LogicalTimestamp", o);
- LogicalTimestamp that = (LogicalTimestamp) o;
-
- return ComparisonChain.start()
- .compare(this.value, that.value)
- .result();
- }
-
- @Override
- public int hashCode() {
- return Long.hashCode(value);
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (!(obj instanceof LogicalTimestamp)) {
- return false;
- }
- LogicalTimestamp that = (LogicalTimestamp) obj;
- return Objects.equals(this.value, that.value);
- }
-
- @Override
- public String toString() {
- return toStringHelper(getClass())
- .add("value", value)
- .toString();
- }
-}
+++ /dev/null
-/*
- * Copyright 2015-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.atomix.utils.time;
-
-import com.google.common.base.Preconditions;
-import com.google.common.collect.ComparisonChain;
-
-import java.util.Objects;
-
-import static com.google.common.base.MoreObjects.toStringHelper;
-
-/**
- * A logical timestamp that derives its value from two input values. The first
- * value always takes precedence over the second value when comparing timestamps.
- */
-public class MultiValuedTimestamp<T extends Comparable<T>, U extends Comparable<U>> implements Timestamp {
- private final T value1;
- private final U value2;
-
- /**
- * Creates a new timestamp based on two values. The first value has higher
- * precedence than the second when comparing timestamps.
- *
- * @param value1 first value
- * @param value2 second value
- */
- public MultiValuedTimestamp(T value1, U value2) {
- this.value1 = Preconditions.checkNotNull(value1);
- this.value2 = Preconditions.checkNotNull(value2);
- }
-
- @Override
- public int compareTo(Timestamp o) {
- Preconditions.checkArgument(o instanceof MultiValuedTimestamp,
- "Must be MultiValuedTimestamp", o);
- MultiValuedTimestamp that = (MultiValuedTimestamp) o;
-
- return ComparisonChain.start()
- .compare(this.value1, that.value1)
- .compare(this.value2, that.value2)
- .result();
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(value1, value2);
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (!(obj instanceof MultiValuedTimestamp)) {
- return false;
- }
- MultiValuedTimestamp that = (MultiValuedTimestamp) obj;
- return Objects.equals(this.value1, that.value1)
- && Objects.equals(this.value2, that.value2);
- }
-
- @Override
- public String toString() {
- return toStringHelper(getClass())
- .add("value1", value1)
- .add("value2", value2)
- .toString();
- }
-
- /**
- * Returns the first value.
- *
- * @return first value
- */
- public T value1() {
- return value1;
- }
-
- /**
- * Returns the second value.
- *
- * @return second value
- */
- public U value2() {
- return value2;
- }
-
- // Default constructor for serialization
- @SuppressWarnings("unused")
- private MultiValuedTimestamp() {
- this.value1 = null;
- this.value2 = null;
- }
-}
+++ /dev/null
-/*
- * Copyright 2014-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.atomix.utils.time;
-
-import com.google.common.base.Preconditions;
-
-/**
- * Opaque version structure.
- * <p>
- * Classes implementing this interface must also implement
- * {@link #hashCode()} and {@link #equals(Object)}.
- */
-public interface Timestamp extends Comparable<Timestamp> {
-
- @Override
- int hashCode();
-
- @Override
- boolean equals(Object obj);
-
- /**
- * Tests if this timestamp is newer than the specified timestamp.
- *
- * @param other timestamp to compare against
- * @return true if this instance is newer
- */
- default boolean isNewerThan(Timestamp other) {
- return this.compareTo(Preconditions.checkNotNull(other)) > 0;
- }
-
- /**
- * Tests if this timestamp is older than the specified timestamp.
- *
- * @param other timestamp to compare against
- * @return true if this instance is older
- */
- default boolean isOlderThan(Timestamp other) {
- return this.compareTo(Preconditions.checkNotNull(other)) < 0;
- }
-}
+++ /dev/null
-/*
- * Copyright 2017-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.atomix.utils.time;
-
-import com.google.common.annotations.Beta;
-import io.atomix.utils.Identifier;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
-import static com.google.common.base.MoreObjects.toStringHelper;
-
-/**
- * Vector clock.
- */
-@Beta
-public class VectorClock<T extends Identifier> implements Clock<VectorTimestamp<T>> {
- private final T localIdentifier;
- private final Map<T, VectorTimestamp<T>> vector = new HashMap<>();
-
- public VectorClock(T localIdentifier) {
- this(new VectorTimestamp<T>(localIdentifier, 0));
- }
-
- public VectorClock(VectorTimestamp<T> localTimestamp) {
- this(localTimestamp, Collections.emptyList());
- }
-
- public VectorClock(VectorTimestamp<T> localTimestamp, Collection<VectorTimestamp<T>> vector) {
- this.localIdentifier = localTimestamp.identifier();
- this.vector.put(localTimestamp.identifier(), localTimestamp);
- for (VectorTimestamp<T> timestamp : vector) {
- this.vector.put(timestamp.identifier(), timestamp);
- }
- }
-
- @Override
- public VectorTimestamp<T> getTime() {
- return vector.get(localIdentifier);
- }
-
- /**
- * Returns the local logical timestamp.
- *
- * @return the logical timestamp for the local identifier
- */
- public LogicalTimestamp getLocalTimestamp() {
- return getTime();
- }
-
- /**
- * Returns the logical timestamp for the given identifier.
- *
- * @param identifier the identifier for which to return the timestamp
- * @return the logical timestamp for the given identifier
- */
- public LogicalTimestamp getTimestamp(T identifier) {
- return vector.get(identifier);
- }
-
- /**
- * Returns a collection of identifier-timestamp pairs.
- *
- * @return a collection of identifier-timestamp pairs
- */
- public Collection<VectorTimestamp<T>> getTimestamps() {
- return vector.values();
- }
-
- /**
- * Updates the given timestamp.
- *
- * @param timestamp the timestamp to update
- */
- public void update(VectorTimestamp<T> timestamp) {
- VectorTimestamp<T> currentTimestamp = vector.get(timestamp.identifier());
- if (currentTimestamp == null || currentTimestamp.value() < timestamp.value()) {
- vector.put(timestamp.identifier(), timestamp);
- }
- }
-
- /**
- * Updates the vector clock.
- *
- * @param clock the vector clock with which to update this clock
- */
- public void update(VectorClock<T> clock) {
- for (VectorTimestamp<T> timestamp : clock.vector.values()) {
- update(timestamp);
- }
- }
-
- @Override
- public String toString() {
- return toStringHelper(this)
- .add("time", getTime())
- .add("vector", getTimestamps())
- .toString();
- }
-}
+++ /dev/null
-/*
- * Copyright 2017-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.atomix.utils.time;
-
-import com.google.common.collect.ComparisonChain;
-import io.atomix.utils.Identifier;
-
-import java.util.Objects;
-
-import static com.google.common.base.MoreObjects.toStringHelper;
-import static com.google.common.base.Preconditions.checkArgument;
-
-/**
- * Vector clock timestamp.
- */
-public class VectorTimestamp<T extends Identifier> extends LogicalTimestamp {
- private final T identifier;
-
- public VectorTimestamp(T identifier, long value) {
- super(value);
- this.identifier = identifier;
- }
-
- /**
- * Returns the timestamp identifier.
- *
- * @return the timestamp identifier
- */
- public T identifier() {
- return identifier;
- }
-
- @Override
- public int compareTo(Timestamp o) {
- checkArgument(o instanceof VectorTimestamp, "Must be VectorTimestamp", o);
- VectorTimestamp that = (VectorTimestamp) o;
-
- return ComparisonChain.start()
- .compare(this.identifier.id(), that.identifier.id())
- .compare(this.value(), that.value())
- .result();
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(identifier(), value());
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (!(obj instanceof VectorTimestamp)) {
- return false;
- }
- VectorTimestamp that = (VectorTimestamp) obj;
- return Objects.equals(this.identifier, that.identifier)
- && Objects.equals(this.value(), that.value());
- }
-
- @Override
- public String toString() {
- return toStringHelper(this)
- .add("identifier", identifier())
- .add("value", value())
- .toString();
- }
-}
+++ /dev/null
-/*
- * Copyright 2017-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.atomix.utils.time;
-
-import com.google.common.collect.ComparisonChain;
-
-import java.util.Objects;
-
-import static com.google.common.base.MoreObjects.toStringHelper;
-import static com.google.common.base.Preconditions.checkArgument;
-
-/**
- * Logical timestamp for versions.
- * <p>
- * The version is a logical timestamp that represents a point in logical time at which an event occurs.
- * This is used in both pessimistic and optimistic locking protocols to ensure that the state of a shared resource
- * has not changed at the end of a transaction.
- */
-public class Version implements Timestamp {
- private final long version;
-
- public Version(long version) {
- this.version = version;
- }
-
- /**
- * Returns the version.
- *
- * @return the version
- */
- public long value() {
- return this.version;
- }
-
- @Override
- public int compareTo(Timestamp o) {
- checkArgument(o instanceof Version,
- "Must be LockVersion", o);
- Version that = (Version) o;
-
- return ComparisonChain.start()
- .compare(this.version, that.version)
- .result();
- }
-
- @Override
- public int hashCode() {
- return Long.hashCode(version);
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (!(obj instanceof Version)) {
- return false;
- }
- Version that = (Version) obj;
- return Objects.equals(this.version, that.version);
- }
-
- @Override
- public String toString() {
- return toStringHelper(getClass())
- .add("version", version)
- .toString();
- }
-}
+++ /dev/null
-/*
- * Copyright 2015-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package io.atomix.utils.time;
-
-import com.google.common.base.MoreObjects;
-import com.google.common.base.Objects;
-import io.atomix.utils.misc.ArraySizeHashPrinter;
-import io.atomix.utils.misc.TimestampPrinter;
-
-import java.util.function.Function;
-
-/**
- * Versioned value.
- *
- * @param <V> value type.
- */
-public class Versioned<V> {
- private final V value;
- private final long version;
- private final long creationTime;
-
- /**
- * Constructs a new versioned value.
- *
- * @param value value
- * @param version version
- * @param creationTime milliseconds of the creation event
- * from the Java epoch of 1970-01-01T00:00:00Z
- */
- public Versioned(V value, long version, long creationTime) {
- this.value = value;
- this.version = version;
- this.creationTime = creationTime;
- }
-
- /**
- * Constructs a new versioned value.
- *
- * @param value value
- * @param version version
- */
- public Versioned(V value, long version) {
- this(value, version, System.currentTimeMillis());
- }
-
- /**
- * Returns the value.
- *
- * @return value.
- */
- public V value() {
- return value;
- }
-
- /**
- * Returns the version.
- *
- * @return version
- */
- public long version() {
- return version;
- }
-
- /**
- * Returns the system time when this version was created.
- * <p>
- * Care should be taken when relying on creationTime to
- * implement any behavior in a distributed setting. Due
- * to the possibility of clock skew it is likely that
- * even creationTimes of causally related versions can be
- * out or order.
- *
- * @return creation time
- */
- public long creationTime() {
- return creationTime;
- }
-
- /**
- * Maps this instance into another after transforming its
- * value while retaining the same version and creationTime.
- *
- * @param transformer function for mapping the value
- * @param <U> value type of the returned instance
- * @return mapped instance
- */
- public synchronized <U> Versioned<U> map(Function<V, U> transformer) {
- return new Versioned<>(value != null ? transformer.apply(value) : null, version, creationTime);
- }
-
- /**
- * Returns the value of the specified Versioned object if non-null or else returns
- * a default value.
- *
- * @param versioned versioned object
- * @param defaultValue default value to return if versioned object is null
- * @param <U> type of the versioned value
- * @return versioned value or default value if versioned object is null
- */
- public static <U> U valueOrElse(Versioned<U> versioned, U defaultValue) {
- return versioned == null ? defaultValue : versioned.value();
- }
-
- /**
- * Returns the value of the specified Versioned object if non-null or else returns null.
- *
- * @param versioned versioned object
- * @param <U> type of the versioned value
- * @return versioned value or null if versioned object is null
- */
- public static <U> U valueOrNull(Versioned<U> versioned) {
- return valueOrElse(versioned, null);
- }
-
- @Override
- public int hashCode() {
- return Objects.hashCode(value, version, creationTime);
- }
-
- @Override
- public boolean equals(Object other) {
- if (!(other instanceof Versioned)) {
- return false;
- }
- Versioned<V> that = (Versioned) other;
- return Objects.equal(this.value, that.value)
- && Objects.equal(this.version, that.version)
- && Objects.equal(this.creationTime, that.creationTime);
- }
-
- @Override
- public String toString() {
- return MoreObjects.toStringHelper(this)
- .add("value", value instanceof byte[] ? ArraySizeHashPrinter.of((byte[]) value) : value)
- .add("version", version)
- .add("creationTime", new TimestampPrinter(creationTime))
- .toString();
- }
-}
+++ /dev/null
-/*
- * Copyright 2017-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.atomix.utils.time;
-
-import static com.google.common.base.MoreObjects.toStringHelper;
-
-/**
- * Wall clock.
- */
-public class WallClock implements Clock<WallClockTimestamp> {
- @Override
- public WallClockTimestamp getTime() {
- return new WallClockTimestamp();
- }
-
- @Override
- public String toString() {
- return toStringHelper(this)
- .add("time", getTime())
- .toString();
- }
-}
+++ /dev/null
-/*
- * Copyright 2015-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.atomix.utils.time;
-
-import com.google.common.collect.ComparisonChain;
-import io.atomix.utils.misc.TimestampPrinter;
-
-import java.util.Objects;
-
-import static com.google.common.base.Preconditions.checkArgument;
-
-/**
- * A Timestamp that derives its value from the prevailing
- * wallclock time on the controller where it is generated.
- */
-public class WallClockTimestamp implements Timestamp {
-
- /**
- * Returns a new wall clock timestamp for the given unix timestamp.
- *
- * @param unixTimestamp the unix timestamp for which to create a new wall clock timestamp
- * @return the wall clock timestamp
- */
- public static WallClockTimestamp from(long unixTimestamp) {
- return new WallClockTimestamp(unixTimestamp);
- }
-
- private final long unixTimestamp;
-
- public WallClockTimestamp() {
- unixTimestamp = System.currentTimeMillis();
- }
-
- public WallClockTimestamp(long timestamp) {
- unixTimestamp = timestamp;
- }
-
- @Override
- public int compareTo(Timestamp o) {
- checkArgument(o instanceof WallClockTimestamp,
- "Must be WallClockTimestamp", o);
- WallClockTimestamp that = (WallClockTimestamp) o;
-
- return ComparisonChain.start()
- .compare(this.unixTimestamp, that.unixTimestamp)
- .result();
- }
-
- @Override
- public int hashCode() {
- return Long.hashCode(unixTimestamp);
- }
-
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (!(obj instanceof WallClockTimestamp)) {
- return false;
- }
- WallClockTimestamp that = (WallClockTimestamp) obj;
- return Objects.equals(this.unixTimestamp, that.unixTimestamp);
- }
-
- @Override
- public String toString() {
- return new TimestampPrinter(unixTimestamp).toString();
- }
-
- /**
- * Returns the unixTimestamp.
- *
- * @return unix timestamp
- */
- public long unixTimestamp() {
- return unixTimestamp;
- }
-}
+++ /dev/null
-/*
- * Copyright 2017-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-/**
- * Provides classes and interfaces for representing and operating on both logical and physical representations of time.
- */
-package io.atomix.utils.time;
+++ /dev/null
-/*
- * Copyright 2017-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.atomix.utils.time;
-
-import org.junit.Test;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
-/**
- * Logical timestamp test.
- */
-public class EpochTest {
- @Test
- public void testLogicalTimestamp() throws Exception {
- Epoch epoch = Epoch.of(1);
- assertEquals(1, epoch.value());
- assertTrue(epoch.isNewerThan(Epoch.of(0)));
- assertFalse(epoch.isNewerThan(Epoch.of(2)));
- assertTrue(epoch.isOlderThan(Epoch.of(2)));
- assertFalse(epoch.isOlderThan(Epoch.of(0)));
- }
-}
+++ /dev/null
-/*
- * Copyright 2017-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.atomix.utils.time;
-
-import org.junit.Test;
-
-import static org.junit.Assert.assertEquals;
-
-/**
- * Logical clock test.
- */
-public class LogicalClockTest {
- @Test
- public void testLogicalClock() throws Exception {
- LogicalClock clock = new LogicalClock();
- assertEquals(1, clock.increment().value());
- assertEquals(1, clock.getTime().value());
- assertEquals(2, clock.increment().value());
- assertEquals(2, clock.getTime().value());
- assertEquals(5, clock.update(LogicalTimestamp.of(5)).value());
- assertEquals(5, clock.getTime().value());
- assertEquals(5, clock.update(LogicalTimestamp.of(3)).value());
- }
-}
+++ /dev/null
-/*
- * Copyright 2017-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.atomix.utils.time;
-
-import org.junit.Test;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
-/**
- * Logical timestamp test.
- */
-public class LogicalTimestampTest {
- @Test
- public void testLogicalTimestamp() throws Exception {
- LogicalTimestamp timestamp = LogicalTimestamp.of(1);
- assertEquals(1, timestamp.value());
- assertTrue(timestamp.isNewerThan(LogicalTimestamp.of(0)));
- assertFalse(timestamp.isNewerThan(LogicalTimestamp.of(2)));
- assertTrue(timestamp.isOlderThan(LogicalTimestamp.of(2)));
- assertFalse(timestamp.isOlderThan(LogicalTimestamp.of(0)));
- }
-}
+++ /dev/null
-/*
- * Copyright 2015-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.atomix.utils.time;
-
-import com.google.common.testing.EqualsTester;
-import org.junit.Assert;
-import org.junit.Test;
-
-import java.lang.reflect.Constructor;
-import java.util.Arrays;
-
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.notNullValue;
-
-/**
- * MultiValuedTimestamp unit tests.
- */
-public class MultiValuedTimestampTest {
- private final MultiValuedTimestamp<Integer, Integer> stats1 = new MultiValuedTimestamp<>(1, 3);
- private final MultiValuedTimestamp<Integer, Integer> stats2 = new MultiValuedTimestamp<>(1, 2);
-
- /**
- * Tests the creation of the MapEvent object.
- */
- @Test
- public void testConstruction() {
- assertThat(stats1.value1(), is(1));
- assertThat(stats1.value2(), is(3));
- }
-
- /**
- * Tests the toCompare function.
- */
- @Test
- public void testToCompare() {
- assertThat(stats1.compareTo(stats2), is(1));
- }
-
- /**
- * Tests the equals, hashCode and toString methods using Guava EqualsTester.
- */
- @Test
- public void testEquals() {
- new EqualsTester()
- .addEqualityGroup(stats1, stats1)
- .addEqualityGroup(stats2)
- .testEquals();
- }
-
- /**
- * Tests that the empty argument list constructor for serialization
- * is present and creates a proper object.
- */
- @Test
- public void testSerializerConstructor() {
- try {
- Constructor[] constructors = MultiValuedTimestamp.class.getDeclaredConstructors();
- assertThat(constructors, notNullValue());
- Arrays.stream(constructors).filter(ctor ->
- ctor.getParameterTypes().length == 0)
- .forEach(noParamsCtor -> {
- try {
- noParamsCtor.setAccessible(true);
- MultiValuedTimestamp stats =
- (MultiValuedTimestamp) noParamsCtor.newInstance();
- assertThat(stats, notNullValue());
- } catch (Exception e) {
- Assert.fail("Exception instantiating no parameters constructor");
- }
- });
- } catch (Exception e) {
- Assert.fail("Exception looking up constructors");
- }
- }
-}
+++ /dev/null
-/*
- * Copyright 2017-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.atomix.utils.time;
-
-import org.junit.Test;
-
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
-/**
- * Version test.
- */
-public class VersionTest {
- @Test
- public void testVersion() {
- Version version1 = new Version(1);
- Version version2 = new Version(1);
- assertTrue(version1.equals(version2));
- assertTrue(version1.hashCode() == version2.hashCode());
- assertTrue(version1.value() == version2.value());
-
- Version version3 = new Version(2);
- assertFalse(version1.equals(version3));
- assertFalse(version1.hashCode() == version3.hashCode());
- assertFalse(version1.value() == version3.value());
- }
-}
+++ /dev/null
-/*
- * Copyright 2015-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.atomix.utils.time;
-
-import com.google.common.testing.EqualsTester;
-import org.junit.Test;
-
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.is;
-
-/**
- * Versioned unit tests.
- */
-public class VersionedTest {
-
- private final Versioned<Integer> stats1 = new Versioned<>(1, 2, 3);
-
- private final Versioned<Integer> stats2 = new Versioned<>(1, 2);
-
- /**
- * Tests the creation of the MapEvent object.
- */
- @Test
- public void testConstruction() {
- assertThat(stats1.value(), is(1));
- assertThat(stats1.version(), is(2L));
- assertThat(stats1.creationTime(), is(3L));
- }
-
- /**
- * Maps an Integer to a String - Utility function to test the map function.
- *
- * @param a Actual Integer parameter.
- * @return String Mapped valued.
- */
- public static String transform(Integer a) {
- return Integer.toString(a);
- }
-
- /**
- * Tests the map function.
- */
- @Test
- public void testMap() {
- Versioned<String> tempObj = stats1.map(VersionedTest::transform);
- assertThat(tempObj.value(), is("1"));
- }
-
- /**
- * Tests the valueOrElse method.
- */
- @Test
- public void testOrElse() {
- Versioned<String> vv = new Versioned<>("foo", 1);
- Versioned<String> nullVV = null;
- assertThat(Versioned.valueOrElse(vv, "bar"), is("foo"));
- assertThat(Versioned.valueOrElse(nullVV, "bar"), is("bar"));
- }
-
- /**
- * Tests the equals, hashCode and toString methods using Guava EqualsTester.
- */
- @Test
- public void testEquals() {
- new EqualsTester()
- .addEqualityGroup(stats1, stats1)
- .addEqualityGroup(stats2)
- .testEquals();
- }
-
-}
+++ /dev/null
-/*
- * Copyright 2017-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.atomix.utils.time;
-
-import org.junit.Test;
-
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-
-/**
- * Wall clock test.
- */
-public class WallClockTest {
- @Test
- public void testWallClock() throws Exception {
- WallClock clock = new WallClock();
- WallClockTimestamp time = clock.getTime();
- assertNotNull(time);
- Thread.sleep(5);
- assertTrue(clock.getTime().unixTimestamp() > time.unixTimestamp());
- }
-}
+++ /dev/null
-/*
- * Copyright 2015-present Open Networking Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package io.atomix.utils.time;
-
-import org.junit.Test;
-
-import static org.junit.Assert.assertTrue;
-
-/**
- * Tests for {@link WallClockTimestamp}.
- */
-public class WallClockTimestampTest {
- @Test
- public final void testBasic() throws InterruptedException {
- WallClockTimestamp ts1 = new WallClockTimestamp();
- Thread.sleep(50);
- WallClockTimestamp ts2 = new WallClockTimestamp();
- long stamp = System.currentTimeMillis() + 10000;
- WallClockTimestamp ts3 = new WallClockTimestamp(stamp);
-
- assertTrue(ts1.compareTo(ts1) == 0);
- assertTrue(ts2.compareTo(ts1) > 0);
- assertTrue(ts1.compareTo(ts2) < 0);
- assertTrue(ts3.unixTimestamp() == stamp);
- }
-}