001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.component.file; 018 019import java.util.Comparator; 020 021import org.apache.camel.CamelContext; 022import org.apache.camel.Exchange; 023import org.apache.camel.Expression; 024import org.apache.camel.spi.Language; 025import org.apache.camel.util.ObjectHelper; 026 027/** 028 * Default file sorter. 029 */ 030public final class GenericFileDefaultSorter { 031 032 private GenericFileDefaultSorter() { 033 } 034 035 /** 036 * Returns a new sory by file language expression 037 * 038 * @param context the camel context 039 * @param expression the file language expression 040 * @param reverse true to reverse order 041 * @return the comparator 042 */ 043 public static Comparator<Exchange> sortByFileLanguage( 044 CamelContext context, String expression, boolean reverse) { 045 return sortByFileLanguage(context, expression, reverse, false, null); 046 } 047 048 /** 049 * Returns a new sory by file language expression 050 * 051 * @param context the camel context 052 * @param expression the file language expression 053 * @param reverse true to reverse order 054 * @param ignoreCase ignore case if comparing strings 055 * @return the comparator 056 */ 057 public static Comparator<Exchange> sortByFileLanguage( 058 CamelContext context, String expression, boolean reverse, boolean ignoreCase) { 059 return sortByFileLanguage(context, expression, reverse, ignoreCase, null); 060 } 061 062 /** 063 * Returns a new sort by file language expression 064 * 065 * @param context the camel context 066 * @param expression the file language expression 067 * @param reverse true to reverse order 068 * @param ignoreCase ignore case if comparing strings 069 * @param nested nested comparator for sub group sorting, can be null 070 * @return the comparator 071 */ 072 public static Comparator<Exchange> sortByFileLanguage( 073 final CamelContext context, final String expression, final boolean reverse, 074 final boolean ignoreCase, final Comparator<Exchange> nested) { 075 076 // the expression should be enclosed by ${ } 077 String text = expression; 078 if (!expression.startsWith("${")) { 079 text = "${" + text; 080 } 081 if (!expression.endsWith("}")) { 082 text = text + "}"; 083 } 084 Language language = context.resolveLanguage("file"); 085 final Expression exp = language.createExpression(text); 086 087 return new Comparator<Exchange>() { 088 public int compare(Exchange o1, Exchange o2) { 089 Object result1 = exp.evaluate(o1, Object.class); 090 Object result2 = exp.evaluate(o2, Object.class); 091 int answer = ObjectHelper.compare(result1, result2, ignoreCase); 092 // if equal then sub sort by nested comparator 093 if (answer == 0 && nested != null) { 094 answer = nested.compare(o1, o2); 095 } 096 return reverse ? -1 * answer : answer; 097 } 098 099 public String toString() { 100 return expression + (nested != null ? ";" + nested.toString() : ""); 101 } 102 }; 103 } 104 105}