1/**
2 * Copyright (C) 2014 Google Inc.
3 * Licensed to The Android Open Source Project.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.mail.utils;
19
20import com.google.android.mail.common.base.Function;
21import com.google.common.collect.ImmutableMap;
22
23import java.util.Comparator;
24import java.util.Map;
25
26/**
27 * Compares objects of the type {@code T} based on predefined order of the ranks of the type
28 * {@code K}. The ranks that are not in the predefined order will be considered larger than other
29 * ranks (if this comparator is used for sorting those elements will be at the end).
30 *
31 * @param <T> Type to be compared.
32 * @param <K> Type of the ranks used for comparison.
33 */
34public class RankedComparator<T, K> implements Comparator<T> {
35
36    private final Map<K, Integer> mRankOrder;
37
38    private final Function<T, K> mRankExtractorFunction;
39
40    /**
41     * Creates a comparator that compares objects of type {@code T} by extracting the ranks of the
42     * objects using {@code rankExtractorFunction} and comparing them by their position in
43     * {@code rankOrder}. Ranks not present in {@code rankOrder} are considered larger than the
44     * ranks present in the {@code rankOrder}.
45     *
46     * @param rankOrder             Order of the ranks.
47     * @param rankExtractorFunction Function that extracts rank from the object.
48     */
49    public RankedComparator(K[] rankOrder, Function<T, K> rankExtractorFunction) {
50        final ImmutableMap.Builder<K, Integer> orderBuilder = ImmutableMap.builder();
51        for (int i = 0; i < rankOrder.length; i++) {
52            orderBuilder.put(rankOrder[i], i);
53        }
54        mRankOrder = orderBuilder.build();
55
56        mRankExtractorFunction = rankExtractorFunction;
57    }
58
59    private int getOrder(T object) {
60        final K key = mRankExtractorFunction.apply(object);
61
62        if (mRankOrder.containsKey(key)) {
63            return mRankOrder.get(key);
64        }
65        return Integer.MAX_VALUE;
66    }
67
68    @Override
69    public int compare(T lhs, T rhs) {
70        final int orderLhs = getOrder(lhs);
71        final int orderRhs = getOrder(rhs);
72        // Order can be Integer.MAX_VALUE so subtraction should not be used.
73        return orderLhs < orderRhs ? -1 : (orderLhs == orderRhs ? 0 : 1);
74    }
75}
76