/*
 * Copyright (C) 2016 The Guava Authors
 *
 * 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 com.google.common.graph;

import com.google.common.annotations.Beta;
import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.Ordering;

import java.util.Comparator;

import javax.annotation.Nullable;

/**
 * Used to represent the order of elements in a data structure that supports different options
 * for iteration order guarantees.
 *
 * <p>Example usage:
 * <pre><code>
 *   MutableGraph<Integer> graph
 *       = GraphBuilder.directed().nodeOrder(ElementOrder.<Integer>natural()).build();
 * </code></pre>
 *
 * @author Joshua O'Madadhain
 * @since 20.0
 */
@Beta
public final class ElementOrder<T> {
  private final Type type;
  @Nullable private final Comparator<T> comparator;

  /**
   * The type of ordering that this object specifies.
   * <ul>
   * <li>UNORDERED: no order is guaranteed.
   * <li>INSERTION: insertion ordering is guaranteed.
   * <li>SORTED: ordering according to a supplied comparator is guaranteed.
   * </ul>
   */
  public enum Type {
    UNORDERED,
    INSERTION,
    SORTED
  }

  private ElementOrder(Type type, @Nullable Comparator<T> comparator) {
    this.type = Preconditions.checkNotNull(type);
    Preconditions.checkArgument((type == Type.SORTED) == (comparator != null),
        "if the type is SORTED, the comparator should be non-null; otherwise, it should be null");
    this.comparator = comparator;
  }

  /**
   * Returns the type of ordering used.
   */
  public Type type() {
    return type;
  }

  /**
   * Returns the {@link Comparator} used.
   *
   * @throws IllegalStateException if no comparator is defined
   */
  public Comparator<T> comparator() {
    if (comparator != null) {
      return comparator;
    }
    throw new IllegalStateException("This ordering does not define a comparator");
  }

  @Override
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    }
    if (!(obj instanceof ElementOrder)) {
      return false;
    }

    ElementOrder<?> other = (ElementOrder<?>) obj;
    return other.type == this.type
        && Objects.equal(other.comparator, this.comparator);
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(this.type, this.comparator);
  }

  @Override
  public String toString() {
    ToStringHelper helper = MoreObjects.toStringHelper(this).add("type", this.type);
    if (this.comparator != null) {
      helper.add("comparator", this.comparator);
    }
    return helper.toString();
  }

  /**
   * Returns an instance which specifies that no ordering is guaranteed.
   */
  public static final <S> ElementOrder<S> unordered() {
    return new ElementOrder<S>(Type.UNORDERED, null);
  }

  /**
   * Returns an instance which specifies that insertion ordering is guaranteed.
   */
  public static final <S> ElementOrder<S> insertion() {
    return new ElementOrder<S>(Type.INSERTION, null);
  }

  /**
   * Returns an instance which specifies that the natural ordering of the elements is guaranteed.
   */
  public static final <S extends Comparable<? super S>> ElementOrder<S> natural() {
    return new ElementOrder<S>(Type.SORTED, Ordering.<S>natural());
  }

  /**
   * Returns an instance which specifies that the ordering of the elements is guaranteed to be
   * determined by {@code comparator}.
   */
  public static final <S> ElementOrder<S> sorted(Comparator<S> comparator) {
    return new ElementOrder<S>(Type.SORTED, comparator);
  }
}

@@ -2114,24 +2114,959 @@
 rator;%0A  %7D%0A%0A
+  /**%0A   * Returns an instance which specifies that no ordering is guaranteed.%0A   */%0A  public static %3CS%3E ElementOrder%3CS%3E unordered() %7B%0A    return new ElementOrder%3CS%3E(Type.UNORDERED, null);%0A  %7D%0A%0A  /**%0A   * Returns an instance which specifies that insertion ordering is guaranteed.%0A   */%0A  public static %3CS%3E ElementOrder%3CS%3E insertion() %7B%0A    return new ElementOrder%3CS%3E(Type.INSERTION, null);%0A  %7D%0A%0A  /**%0A   * Returns an instance which specifies that the natural ordering of the elements is guaranteed.%0A   */%0A  public static %3CS extends Comparable%3C? super S%3E%3E ElementOrder%3CS%3E natural() %7B%0A    return new ElementOrder%3CS%3E(Type.SORTED, Ordering.%3CS%3Enatural());%0A  %7D%0A%0A  /**%0A   * Returns an instance which specifies that the ordering of the elements is guaranteed to be%0A   * determined by %7B@code comparator%7D.%0A   */%0A  public static %3CS%3E ElementOrder%3CS%3E sorted(Comparator%3CS%3E comparator) %7B%0A    return new ElementOrder%3CS%3E(Type.SORTED, comparator);%0A  %7D%0A%0A
   /**%0A   * R
@@ -4131,966 +4131,7 @@
 %0A  %7D
-%0A%0A  /**%0A   * Returns an instance which specifies that no ordering is guaranteed.%0A   */%0A  public static final %3CS%3E ElementOrder%3CS%3E unordered() %7B%0A    return new ElementOrder%3CS%3E(Type.UNORDERED, null);%0A  %7D%0A%0A  /**%0A   * Returns an instance which specifies that insertion ordering is guaranteed.%0A   */%0A  public static final %3CS%3E ElementOrder%3CS%3E insertion() %7B%0A    return new ElementOrder%3CS%3E(Type.INSERTION, null);%0A  %7D%0A%0A  /**%0A   * Returns an instance which specifies that the natural ordering of the elements is guaranteed.%0A   */%0A  public static final %3CS extends Comparable%3C? super S%3E%3E ElementOrder%3CS%3E natural() %7B%0A    return new ElementOrder%3CS%3E(Type.SORTED, Ordering.%3CS%3Enatural());%0A  %7D%0A%0A  /**%0A   * Returns an instance which specifies that the ordering of the elements is guaranteed to be%0A   * determined by %7B@code comparator%7D.%0A   */%0A  public static final %3CS%3E ElementOrder%3CS%3E sorted(Comparator%3CS%3E comparator) %7B%0A    return new ElementOrder%3CS%3E(Type.SORTED, comparator);%0A  %7D
 %0A%7D%0A

Move the static "constructor" methods in ElementOrder above the class methods (as done in e.g. Endpoints). Also, remove "final" from the static methods, since it's not standard practice to include on static methods (and the class itself is final). ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=125901101