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.model;
018    
019    import java.util.Collections;
020    import java.util.Comparator;
021    import java.util.List;
022    import javax.xml.bind.annotation.XmlAccessType;
023    import javax.xml.bind.annotation.XmlAccessorType;
024    import javax.xml.bind.annotation.XmlAttribute;
025    import javax.xml.bind.annotation.XmlElement;
026    import javax.xml.bind.annotation.XmlRootElement;
027    import javax.xml.bind.annotation.XmlTransient;
028    
029    import org.apache.camel.Expression;
030    import org.apache.camel.Processor;
031    import org.apache.camel.processor.SortProcessor;
032    import org.apache.camel.spi.RouteContext;
033    import org.apache.camel.util.ObjectHelper;
034    import static org.apache.camel.builder.ExpressionBuilder.bodyExpression;
035    
036    /**
037     * Represents an XML <sort/> element
038     */
039    @XmlRootElement(name = "sort")
040    @XmlAccessorType(XmlAccessType.FIELD)
041    public class SortDefinition extends OutputDefinition<SortDefinition> {
042        @XmlTransient
043        private Comparator comparator;
044        @XmlAttribute(required = false)
045        private String comparatorRef;
046        @XmlElement(name = "expression", required = false)
047        private ExpressionSubElementDefinition expression;
048    
049        public SortDefinition() {
050        }
051    
052        public SortDefinition(Expression expression) {
053            setExpression(expression);
054        }
055    
056        public SortDefinition(Expression expression, Comparator comparator) {
057            this(expression);
058            this.comparator = comparator;
059        }
060    
061        @Override
062        public String toString() {
063            return "sort[" + getExpression() + " by: " + (comparatorRef != null ? "ref:" + comparatorRef : comparator) + "]";
064        }
065    
066        @Override
067        public String getShortName() {
068            return "sort";
069        }
070    
071        @Override
072        public Processor createProcessor(RouteContext routeContext) throws Exception {
073            // lookup in registry
074            if (ObjectHelper.isNotEmpty(comparatorRef)) {
075                comparator = routeContext.getCamelContext().getRegistry().lookup(comparatorRef, Comparator.class);
076            }
077    
078            // if no comparator then default on to string representation
079            if (comparator == null) {
080                comparator = new Comparator() {
081                    public int compare(Object o1, Object o2) {
082                        return ObjectHelper.compare(o1, o2);
083                    }
084                };
085            }
086    
087            // if no expression provided then default to body expression
088            if (getExpression() == null) {
089                setExpression(bodyExpression());
090            }
091    
092            return new SortProcessor(getExpression(), getComparator());
093        }
094    
095        @Override
096        @SuppressWarnings("unchecked")
097        public List<ProcessorDefinition> getOutputs() {
098            return Collections.EMPTY_LIST;
099        }
100    
101        public Comparator getComparator() {
102            return comparator;
103        }
104    
105        public void setComparator(Comparator comparator) {
106            this.comparator = comparator;
107        }
108    
109        public String getComparatorRef() {
110            return comparatorRef;
111        }
112    
113        public void setComparatorRef(String comparatorRef) {
114            this.comparatorRef = comparatorRef;
115        }
116    
117        public Expression getExpression() {
118            if (expression == null) {
119                return null;
120            }
121            if (expression.getExpression() != null) {
122                return expression.getExpression();
123            }
124            return expression.getExpressionType();
125        }
126    
127        public void setExpression(Expression expression) {
128            this.expression = new ExpressionSubElementDefinition(expression);
129        }
130    
131        /**
132         * Sets the comparator to use for sorting
133         *
134         * @param comparator  the comparator to use for sorting
135         * @return the builder
136         */
137        public SortDefinition comparator(Comparator comparator) {
138            setComparator(comparator);
139            return this;
140        }
141    
142        /**
143         * Sets a reference to lookup for the comparator to use for sorting
144         *
145         * @param ref reference for the comparator
146         * @return the builder
147         */
148        public SortDefinition comparatorRef(String ref) {
149            setComparatorRef(ref);
150            return this;
151        }
152    
153    
154    }