001/* 002 * Copyright 2002-2019 the original author or 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 * https://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 org.springframework.util.comparator; 018 019import java.util.Comparator; 020 021import org.springframework.lang.Nullable; 022import org.springframework.util.Assert; 023 024/** 025 * Compares objects based on an arbitrary class order. Allows objects to be sorted based 026 * on the types of class that they inherit, for example: this comparator can be used to 027 * sort a list {@code Number}s such that {@code Long}s occur before {@code Integer}s. 028 * 029 * <p>Only the specified {@code instanceOrder} classes are considered during comparison. 030 * If two objects are both instances of the ordered type this comparator will return a 031 * {@code 0}. Consider combining with {@link Comparator#thenComparing(Comparator)} 032 * if additional sorting is required. 033 * 034 * @author Phillip Webb 035 * @since 3.2 036 * @param <T> the type of objects that may be compared by this comparator 037 * @see Comparator#thenComparing(Comparator) 038 */ 039public class InstanceComparator<T> implements Comparator<T> { 040 041 private final Class<?>[] instanceOrder; 042 043 044 /** 045 * Create a new {@link InstanceComparator} instance. 046 * @param instanceOrder the ordered list of classes that should be used when comparing 047 * objects. Classes earlier in the list will be given a higher priority. 048 */ 049 public InstanceComparator(Class<?>... instanceOrder) { 050 Assert.notNull(instanceOrder, "'instanceOrder' array must not be null"); 051 this.instanceOrder = instanceOrder; 052 } 053 054 055 @Override 056 public int compare(T o1, T o2) { 057 int i1 = getOrder(o1); 058 int i2 = getOrder(o2); 059 return (Integer.compare(i1, i2)); 060 } 061 062 private int getOrder(@Nullable T object) { 063 if (object != null) { 064 for (int i = 0; i < this.instanceOrder.length; i++) { 065 if (this.instanceOrder[i].isInstance(object)) { 066 return i; 067 } 068 } 069 } 070 return this.instanceOrder.length; 071 } 072 073}