001/* 002 * Copyright 2013 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 */ 016package org.springframework.batch.core.job.flow.support; 017 018import org.springframework.util.StringUtils; 019 020import java.util.Comparator; 021 022/** 023 * Sorts by decreasing specificity of pattern, based on just counting 024 * wildcards (with * taking precedence over ?). If wildcard counts are equal 025 * then falls back to alphabetic comparison. Hence * > foo* > ??? > 026 * fo? > foo. 027 * 028 * @see Comparator 029 * @author Michael Minella 030 * @since 3.0 031 */ 032public class DefaultStateTransitionComparator implements Comparator<StateTransition> { 033 public static final String STATE_TRANSITION_COMPARATOR = "batch_state_transition_comparator"; 034 035 /* (non-Javadoc) 036 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) 037 */ 038 @Override 039 public int compare(StateTransition arg0, StateTransition arg1) { 040 String value = arg1.getPattern(); 041 if (arg0.getPattern().equals(value)) { 042 return 0; 043 } 044 int patternCount = StringUtils.countOccurrencesOf(arg0.getPattern(), "*"); 045 int valueCount = StringUtils.countOccurrencesOf(value, "*"); 046 if (patternCount > valueCount) { 047 return 1; 048 } 049 if (patternCount < valueCount) { 050 return -1; 051 } 052 patternCount = StringUtils.countOccurrencesOf(arg0.getPattern(), "?"); 053 valueCount = StringUtils.countOccurrencesOf(value, "?"); 054 if (patternCount > valueCount) { 055 return 1; 056 } 057 if (patternCount < valueCount) { 058 return -1; 059 } 060 return arg0.getPattern().compareTo(value); 061 } 062}