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