1 /* 2 * Copyright 2006 - 2012 Christina Bohk and Roland Ewald 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package p3j.pppm.parameters; 17 18 import java.io.Serializable; 19 import java.util.Comparator; 20 import java.util.List; 21 22 /** 23 * Compares {@link ParameterInstance} objects based on their comparison index. 24 * The comparison index is used in the GUI to always keep the same order of 25 * parameters. This makes it much easier for the user to find parameters. 26 * 27 * 28 * Created on February 4, 2007 29 * 30 * @author Christina Bohk 31 * @author Roland Ewald 32 * 33 */ 34 public class ParameterInstanceComparator implements 35 Comparator<ParameterInstance>, Serializable { 36 37 /** The Constant serialVersionUID. */ 38 private static final long serialVersionUID = -3663405828303366226L; 39 40 @Override 41 public int compare(ParameterInstance instance1, ParameterInstance instance2) { 42 return instance1.getComparisonIndex() - instance2.getComparisonIndex(); 43 } 44 45 /** 46 * Inserts instance in a sorted parameter instance list. 47 * 48 * @param parameterInstances 49 * list of parameter instances 50 * @param insertInstance 51 * parameter instance to be inserted in this list 52 */ 53 public static void insert(List<ParameterInstance> parameterInstances, 54 ParameterInstance insertInstance) { 55 int insertIndex = 0; 56 for (int i = 0; i < parameterInstances.size(); i++) { 57 ParameterInstance instance = parameterInstances.get(i); 58 if (instance.getComparisonIndex() > insertInstance.getComparisonIndex()) { 59 break; 60 } 61 insertIndex++; 62 } 63 parameterInstances.add(insertIndex, insertInstance); 64 65 } 66 67 }