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