Line data Source code
1 : // Copyright (C) 2015 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.account; 16 : 17 : import com.google.common.collect.ComparisonChain; 18 : import com.google.common.collect.Ordering; 19 : import com.google.gerrit.extensions.common.AccountInfo; 20 : import java.util.Comparator; 21 : 22 : public class AccountInfoComparator extends Ordering<AccountInfo> 23 : implements Comparator<AccountInfo> { 24 104 : public static final AccountInfoComparator ORDER_NULLS_FIRST = new AccountInfoComparator(); 25 104 : public static final AccountInfoComparator ORDER_NULLS_LAST = 26 104 : new AccountInfoComparator().setNullsLast(); 27 : 28 : private boolean nullsLast; 29 : 30 : private AccountInfoComparator() {} 31 : 32 : private AccountInfoComparator setNullsLast() { 33 104 : this.nullsLast = true; 34 104 : return this; 35 : } 36 : 37 : @Override 38 : public int compare(AccountInfo a, AccountInfo b) { 39 35 : return ComparisonChain.start() 40 35 : .compare(a.name, b.name, createOrdering()) 41 35 : .compare(a.email, b.email, createOrdering()) 42 35 : .compare(a._accountId, b._accountId, createOrdering()) 43 35 : .result(); 44 : } 45 : 46 : private <S extends Comparable<?>> Ordering<S> createOrdering() { 47 35 : if (nullsLast) { 48 3 : return Ordering.natural().nullsLast(); 49 : } 50 32 : return Ordering.natural().nullsFirst(); 51 : } 52 : }