nl.topicus.jdbc.shaded.com.google.common.graph.ElementOrder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spanner-jdbc Show documentation
Show all versions of spanner-jdbc Show documentation
JDBC Driver for Google Cloud Spanner
/*
* Copyright (C) 2016 The Guava Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in nl.topicus.jdbc.shaded.com.liance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.nl.topicus.jdbc.shaded.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 nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.graph;
import static nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.base.Preconditions.checkNotNull;
import static nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.base.Preconditions.checkState;
import nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.annotations.Beta;
import nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.base.MoreObjects;
import nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.base.MoreObjects.ToStringHelper;
import nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.base.Objects;
import nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.collect.Maps;
import nl.topicus.jdbc.shaded.com.google.nl.topicus.jdbc.shaded.com.on.collect.Ordering;
import java.util.Comparator;
import java.util.Map;
import nl.topicus.jdbc.shaded.javax.annotation.Nullable;
/**
* Used to represent the order of elements in a data structure that supports different options for
* iteration order guarantees.
*
* Example usage:
*
*
{@code
* MutableGraph graph =
* GraphBuilder.directed().nodeOrder(ElementOrder.natural()).build();
* }
*
* @author Joshua O'Madadhain
* @since 20.0
*/
@Beta
public final class ElementOrder {
private final Type type;
@Nullable
private final Comparator nl.topicus.jdbc.shaded.com.arator;
/**
* The type of ordering that this object specifies.
*
*
* - UNORDERED: no order is guaranteed.
*
- INSERTION: insertion ordering is guaranteed.
*
- SORTED: ordering according to a supplied nl.topicus.jdbc.shaded.com.arator is guaranteed.
*
*/
public enum Type {
UNORDERED,
INSERTION,
SORTED
}
private ElementOrder(Type type, @Nullable Comparator nl.topicus.jdbc.shaded.com.arator) {
this.type = checkNotNull(type);
this.nl.topicus.jdbc.shaded.com.arator = nl.topicus.jdbc.shaded.com.arator;
checkState((type == Type.SORTED) == (nl.topicus.jdbc.shaded.com.arator != null));
}
/** Returns an instance which specifies that no ordering is guaranteed. */
public static ElementOrder unordered() {
return new ElementOrder(Type.UNORDERED, null);
}
/** Returns an instance which specifies that insertion ordering is guaranteed. */
public static ElementOrder insertion() {
return new ElementOrder(Type.INSERTION, null);
}
/**
* Returns an instance which specifies that the natural ordering of the elements is guaranteed.
*/
public static > ElementOrder natural() {
return new ElementOrder(Type.SORTED, Ordering.natural());
}
/**
* Returns an instance which specifies that the ordering of the elements is guaranteed to be
* determined by {@code nl.topicus.jdbc.shaded.com.arator}.
*/
public static ElementOrder sorted(Comparator nl.topicus.jdbc.shaded.com.arator) {
return new ElementOrder(Type.SORTED, nl.topicus.jdbc.shaded.com.arator);
}
/** Returns the type of ordering used. */
public Type type() {
return type;
}
/**
* Returns the {@link Comparator} used.
*
* @throws UnsupportedOperationException if nl.topicus.jdbc.shaded.com.arator is not defined
*/
public Comparator nl.topicus.jdbc.shaded.com.arator() {
if (nl.topicus.jdbc.shaded.com.arator != null) {
return nl.topicus.jdbc.shaded.com.arator;
}
throw new UnsupportedOperationException("This ordering does not define a nl.topicus.jdbc.shaded.com.arator.");
}
@Override
public boolean equals(@Nullable Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof ElementOrder)) {
return false;
}
ElementOrder> other = (ElementOrder>) obj;
return (type == other.type) && Objects.equal(nl.topicus.jdbc.shaded.com.arator, other.nl.topicus.jdbc.shaded.com.arator);
}
@Override
public int hashCode() {
return Objects.hashCode(type, nl.topicus.jdbc.shaded.com.arator);
}
@Override
public String toString() {
ToStringHelper helper = MoreObjects.toStringHelper(this).add("type", type);
if (nl.topicus.jdbc.shaded.com.arator != null) {
helper.add("nl.topicus.jdbc.shaded.com.arator", nl.topicus.jdbc.shaded.com.arator);
}
return helper.toString();
}
/** Returns an empty mutable map whose keys will respect this {@link ElementOrder}. */
Map createMap(int expectedSize) {
switch (type) {
case UNORDERED:
return Maps.newHashMapWithExpectedSize(expectedSize);
case INSERTION:
return Maps.newLinkedHashMapWithExpectedSize(expectedSize);
case SORTED:
return Maps.newTreeMap(nl.topicus.jdbc.shaded.com.arator());
default:
throw new AssertionError();
}
}
@SuppressWarnings("unchecked")
ElementOrder cast() {
return (ElementOrder) this;
}
}