001/* 002 * Copyright (C) 2016 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.graph; 018 019import static com.google.common.base.Preconditions.checkNotNull; 020import static com.google.common.base.Preconditions.checkState; 021 022import com.google.common.annotations.Beta; 023import com.google.common.base.MoreObjects; 024import com.google.common.base.MoreObjects.ToStringHelper; 025import com.google.common.base.Objects; 026import com.google.common.collect.Maps; 027import com.google.common.collect.Ordering; 028import com.google.errorprone.annotations.Immutable; 029import java.util.Comparator; 030import java.util.Map; 031import javax.annotation.CheckForNull; 032 033/** 034 * Used to represent the order of elements in a data structure that supports different options for 035 * iteration order guarantees. 036 * 037 * <p>Example usage: 038 * 039 * <pre>{@code 040 * MutableGraph<Integer> graph = 041 * GraphBuilder.directed().nodeOrder(ElementOrder.<Integer>natural()).build(); 042 * }</pre> 043 * 044 * @author Joshua O'Madadhain 045 * @since 20.0 046 */ 047@Beta 048@Immutable 049public final class ElementOrder<T> { 050 private final Type type; 051 052 @SuppressWarnings("Immutable") // Hopefully the comparator provided is immutable! 053 @CheckForNull 054 private final Comparator<T> comparator; 055 056 /** 057 * The type of ordering that this object specifies. 058 * 059 * <ul> 060 * <li>UNORDERED: no order is guaranteed. 061 * <li>STABLE: ordering is guaranteed to follow a pattern that won't change between releases. 062 * Some methods may have stronger guarantees. 063 * <li>INSERTION: insertion ordering is guaranteed. 064 * <li>SORTED: ordering according to a supplied comparator is guaranteed. 065 * </ul> 066 */ 067 public enum Type { 068 UNORDERED, 069 STABLE, 070 INSERTION, 071 SORTED 072 } 073 074 private ElementOrder(Type type, @CheckForNull Comparator<T> comparator) { 075 this.type = checkNotNull(type); 076 this.comparator = comparator; 077 checkState((type == Type.SORTED) == (comparator != null)); 078 } 079 080 /** Returns an instance which specifies that no ordering is guaranteed. */ 081 public static <S> ElementOrder<S> unordered() { 082 return new ElementOrder<>(Type.UNORDERED, null); 083 } 084 085 /** 086 * Returns an instance which specifies that ordering is guaranteed to be always be the same across 087 * iterations, and across releases. Some methods may have stronger guarantees. 088 * 089 * <p>This instance is only useful in combination with {@code incidentEdgeOrder}, e.g. {@code 090 * graphBuilder.incidentEdgeOrder(ElementOrder.stable())}. 091 * 092 * <h3>In combination with {@code incidentEdgeOrder}</h3> 093 * 094 * <p>{@code incidentEdgeOrder(ElementOrder.stable())} guarantees the ordering of the returned 095 * collections of the following methods: 096 * 097 * <ul> 098 * <li>For {@link Graph} and {@link ValueGraph}: 099 * <ul> 100 * <li>{@code edges()}: Stable order 101 * <li>{@code adjacentNodes(node)}: Connecting edge insertion order 102 * <li>{@code predecessors(node)}: Connecting edge insertion order 103 * <li>{@code successors(node)}: Connecting edge insertion order 104 * <li>{@code incidentEdges(node)}: Edge insertion order 105 * </ul> 106 * <li>For {@link Network}: 107 * <ul> 108 * <li>{@code adjacentNodes(node)}: Stable order 109 * <li>{@code predecessors(node)}: Connecting edge insertion order 110 * <li>{@code successors(node)}: Connecting edge insertion order 111 * <li>{@code incidentEdges(node)}: Stable order 112 * <li>{@code inEdges(node)}: Edge insertion order 113 * <li>{@code outEdges(node)}: Edge insertion order 114 * <li>{@code adjacentEdges(edge)}: Stable order 115 * <li>{@code edgesConnecting(nodeU, nodeV)}: Edge insertion order 116 * </ul> 117 * </ul> 118 * 119 * @since 29.0 120 */ 121 public static <S> ElementOrder<S> stable() { 122 return new ElementOrder<>(Type.STABLE, null); 123 } 124 125 /** Returns an instance which specifies that insertion ordering is guaranteed. */ 126 public static <S> ElementOrder<S> insertion() { 127 return new ElementOrder<>(Type.INSERTION, null); 128 } 129 130 /** 131 * Returns an instance which specifies that the natural ordering of the elements is guaranteed. 132 */ 133 public static <S extends Comparable<? super S>> ElementOrder<S> natural() { 134 return new ElementOrder<>(Type.SORTED, Ordering.<S>natural()); 135 } 136 137 /** 138 * Returns an instance which specifies that the ordering of the elements is guaranteed to be 139 * determined by {@code comparator}. 140 */ 141 public static <S> ElementOrder<S> sorted(Comparator<S> comparator) { 142 return new ElementOrder<>(Type.SORTED, checkNotNull(comparator)); 143 } 144 145 /** Returns the type of ordering used. */ 146 public Type type() { 147 return type; 148 } 149 150 /** 151 * Returns the {@link Comparator} used. 152 * 153 * @throws UnsupportedOperationException if comparator is not defined 154 */ 155 public Comparator<T> comparator() { 156 if (comparator != null) { 157 return comparator; 158 } 159 throw new UnsupportedOperationException("This ordering does not define a comparator."); 160 } 161 162 @Override 163 public boolean equals(@CheckForNull Object obj) { 164 if (obj == this) { 165 return true; 166 } 167 if (!(obj instanceof ElementOrder)) { 168 return false; 169 } 170 171 ElementOrder<?> other = (ElementOrder<?>) obj; 172 return (type == other.type) && Objects.equal(comparator, other.comparator); 173 } 174 175 @Override 176 public int hashCode() { 177 return Objects.hashCode(type, comparator); 178 } 179 180 @Override 181 public String toString() { 182 ToStringHelper helper = MoreObjects.toStringHelper(this).add("type", type); 183 if (comparator != null) { 184 helper.add("comparator", comparator); 185 } 186 return helper.toString(); 187 } 188 189 /** Returns an empty mutable map whose keys will respect this {@link ElementOrder}. */ 190 <K extends T, V> Map<K, V> createMap(int expectedSize) { 191 switch (type) { 192 case UNORDERED: 193 return Maps.newHashMapWithExpectedSize(expectedSize); 194 case INSERTION: 195 case STABLE: 196 return Maps.newLinkedHashMapWithExpectedSize(expectedSize); 197 case SORTED: 198 return Maps.newTreeMap(comparator()); 199 } 200 throw new AssertionError(); 201 } 202 203 @SuppressWarnings("unchecked") 204 <T1 extends T> ElementOrder<T1> cast() { 205 return (ElementOrder<T1>) this; 206 } 207}