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 */ 017 package org.apache.camel.component.file; 018 019 import java.util.Comparator; 020 021 import org.apache.camel.CamelContext; 022 import org.apache.camel.Expression; 023 import org.apache.camel.spi.Language; 024 import org.apache.camel.util.ObjectHelper; 025 026 /** 027 * Default file sorter. 028 */ 029 public final class GenericFileDefaultSorter { 030 031 private GenericFileDefaultSorter() { 032 } 033 034 /** 035 * Returns a new sort by name 036 */ 037 public static Comparator<GenericFile> sortByName(final boolean reverse) { 038 return new Comparator<GenericFile>() { 039 public int compare(GenericFile o1, GenericFile o2) { 040 int answer = o1.getFileName().compareTo(o2.getFileName()); 041 return reverse ? -1 * answer : answer; 042 } 043 }; 044 } 045 046 /** 047 * Returns a new sort by path name 048 */ 049 public static Comparator<GenericFile> sortByPathName(final boolean reverse) { 050 return new Comparator<GenericFile>() { 051 public int compare(GenericFile o1, GenericFile o2) { 052 int answer = o1.getParent().compareTo(o2.getParent()); 053 return reverse ? -1 * answer : answer; 054 } 055 }; 056 } 057 058 /** 059 * Returns a new sort by last modified (newest first) 060 */ 061 public static Comparator<GenericFile> sortByLastModified( 062 final boolean reverse) { 063 return new Comparator<GenericFile>() { 064 public int compare(GenericFile o1, GenericFile o2) { 065 long delta = o1.getLastModified() - o2.getLastModified(); 066 if (delta == 0) { 067 return 0; 068 } 069 int answer = delta > 0 ? 1 : -1; 070 return reverse ? -1 * answer : answer; 071 } 072 }; 073 } 074 075 /** 076 * Returns a new sort by file size (smallest first) 077 */ 078 public static Comparator<GenericFile> sortBySize(final boolean reverse) { 079 return new Comparator<GenericFile>() { 080 public int compare(GenericFile o1, GenericFile o2) { 081 long delta = o1.getFileLength() - o2.getFileLength(); 082 if (delta == 0) { 083 return 0; 084 } 085 int answer = delta > 0 ? 1 : -1; 086 return reverse ? -1 * answer : answer; 087 } 088 }; 089 } 090 091 /** 092 * Returns a new sory by file language expression 093 * 094 * @param context the camel context 095 * @param expression the file language expression 096 * @param reverse true to reverse order 097 * @return the comparator 098 */ 099 public static Comparator<GenericFileExchange> sortByFileLanguage( 100 CamelContext context, String expression, boolean reverse) { 101 return sortByFileLanguage(context, expression, reverse, false, null); 102 } 103 104 /** 105 * Returns a new sory by file language expression 106 * 107 * @param context the camel context 108 * @param expression the file language expression 109 * @param reverse true to reverse order 110 * @param ignoreCase ignore case if comparing strings 111 * @return the comparator 112 */ 113 public static Comparator<GenericFileExchange> sortByFileLanguage( 114 CamelContext context, String expression, boolean reverse, boolean ignoreCase) { 115 return sortByFileLanguage(context, expression, reverse, ignoreCase, null); 116 } 117 118 /** 119 * Returns a new sort by file language expression 120 * 121 * @param context the camel context 122 * @param expression the file language expression 123 * @param reverse true to reverse order 124 * @param ignoreCase ignore case if comparing strings 125 * @param nested nested comparator for sub group sorting, can be null 126 * @return the comparator 127 */ 128 public static Comparator<GenericFileExchange> sortByFileLanguage( 129 final CamelContext context, final String expression, final boolean reverse, 130 final boolean ignoreCase, final Comparator<GenericFileExchange> nested) { 131 return new Comparator<GenericFileExchange>() { 132 public int compare(GenericFileExchange o1, GenericFileExchange o2) { 133 Language language = context.resolveLanguage("file"); 134 final Expression exp = language.createExpression(expression); 135 Object result1 = exp.evaluate(o1, Object.class); 136 Object result2 = exp.evaluate(o2, Object.class); 137 int answer = ObjectHelper.compare(result1, result2, ignoreCase); 138 // if equal then sub sort by nested comparator 139 if (answer == 0 && nested != null) { 140 answer = nested.compare(o1, o2); 141 } 142 return reverse ? -1 * answer : answer; 143 } 144 145 public String toString() { 146 return expression + (nested != null ? ";" + nested.toString() : ""); 147 } 148 }; 149 } 150 151 }