1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/*
21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2007 The Guava Authors
3090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
4090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License");
5090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you may not use this file except in compliance with the License.
6090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * You may obtain a copy of the License at
7090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
8090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0
9090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
10090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Unless required by applicable law or agreed to in writing, software
11090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
12090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * See the License for the specific language governing permissions and
14090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * limitations under the License.
15090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
16090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
17090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpackage com.google.common.collect;
18090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
19090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtCompatible;
20090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
21090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.io.Serializable;
22090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.util.List;
23090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport javax.annotation.Nullable;
25090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
26090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/** An ordering that compares objects according to a given order. */
27090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson@GwtCompatible(serializable = true)
28090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonfinal class ExplicitOrdering<T> extends Ordering<T> implements Serializable {
29090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  final ImmutableMap<T, Integer> rankMap;
30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  ExplicitOrdering(List<T> valuesInOrder) {
32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    this(buildRankMap(valuesInOrder));
33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  ExplicitOrdering(ImmutableMap<T, Integer> rankMap) {
36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    this.rankMap = rankMap;
37090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
391d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert  @Override public int compare(T left, T right) {
40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return rank(left) - rank(right); // safe because both are nonnegative
41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private int rank(T value) {
44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    Integer rank = rankMap.get(value);
45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (rank == null) {
46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      throw new IncomparableValueException(value);
47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return rank;
49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static <T> ImmutableMap<T, Integer> buildRankMap(
52090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      List<T> valuesInOrder) {
53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    ImmutableMap.Builder<T, Integer> builder = ImmutableMap.builder();
54090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    int rank = 0;
55090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    for (T value : valuesInOrder) {
56090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      builder.put(value, rank++);
57090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return builder.build();
59090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
60090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
61090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public boolean equals(@Nullable Object object) {
62090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    if (object instanceof ExplicitOrdering) {
63090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      ExplicitOrdering<?> that = (ExplicitOrdering<?>) object;
64090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson      return this.rankMap.equals(that.rankMap);
65090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    }
66090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return false;
67090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
68090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
69090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public int hashCode() {
70090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return rankMap.hashCode();
71090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
72090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
73090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  @Override public String toString() {
74090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return "Ordering.explicit(" + rankMap.keySet() + ")";
75090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
76090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
77090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private static final long serialVersionUID = 0;
78090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson}
79