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