001/* 002 * Copyright 2019 Anyware Services 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 * http://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 */ 016package org.ametys.runtime.model; 017 018import java.util.Comparator; 019 020/** 021 * Comparator for the element definition wrappers using their position 022 * If no position is set, compares the definitions themselves 023 * @param <T> type of the {@link CategorizedElementDefinitionWrapper} to compare 024 */ 025public class CategorizedElementDefinitionWrapperComparator<T extends CategorizedElementDefinitionWrapper> implements Comparator<T> 026{ 027 public int compare(T wrapper1, T wrapper2) 028 { 029 int positionComparison = CategorizedElementDefinitionHelper.compareWrapperPositions(wrapper1, wrapper2); 030 if (positionComparison != 0) 031 { 032 return positionComparison; 033 } 034 else 035 { 036 return compareWrappersWithSamePositions(wrapper1, wrapper2); 037 } 038 } 039 040 /** 041 * Compares the given wrappers that have the same (maybe not set) positions 042 * @param wrapper1 the first wrapper 043 * @param wrapper2 the second wrapper 044 * @return a negative integer, zero, or a positive integer as the first wrapper 045 * is less than, equal to, or greater than the second. 046 */ 047 protected int compareWrappersWithSamePositions(T wrapper1, T wrapper2) 048 { 049 return wrapper1.getDefinition().compareTo(wrapper2.getDefinition()); 050 } 051}