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.model; 018 019import java.util.Comparator; 020 021import javax.xml.bind.annotation.XmlAccessType; 022import javax.xml.bind.annotation.XmlAccessorType; 023import javax.xml.bind.annotation.XmlAttribute; 024import javax.xml.bind.annotation.XmlRootElement; 025import javax.xml.bind.annotation.XmlTransient; 026 027import org.apache.camel.Expression; 028import org.apache.camel.model.language.ExpressionDefinition; 029import org.apache.camel.spi.Metadata; 030 031/** 032 * Sorts the contents of the message 033 */ 034@Metadata(label = "eip,routing") 035@XmlRootElement(name = "sort") 036@XmlAccessorType(XmlAccessType.FIELD) 037public class SortDefinition<T> extends ExpressionNode { 038 @XmlTransient 039 private Comparator<? super T> comparator; 040 @XmlAttribute 041 private String comparatorRef; 042 043 public SortDefinition() { 044 } 045 046 public SortDefinition(Expression expression) { 047 setExpression(ExpressionNodeHelper.toExpressionDefinition(expression)); 048 } 049 050 public SortDefinition(Expression expression, Comparator<? super T> comparator) { 051 this(expression); 052 this.comparator = comparator; 053 } 054 055 @Override 056 public String toString() { 057 return "sort[" + getExpression() + " by: " + (comparatorRef != null ? "ref:" + comparatorRef : comparator) + "]"; 058 } 059 060 @Override 061 public String getShortName() { 062 return "sort"; 063 } 064 065 @Override 066 public String getLabel() { 067 return "sort[" + getExpression() + "]"; 068 } 069 070 /** 071 * Optional expression to sort by something else than the message body 072 */ 073 @Override 074 public void setExpression(ExpressionDefinition expression) { 075 // override to include javadoc what the expression is used for 076 super.setExpression(expression); 077 } 078 079 public Comparator<? super T> getComparator() { 080 return comparator; 081 } 082 083 public void setComparator(Comparator<T> comparator) { 084 this.comparator = comparator; 085 } 086 087 public String getComparatorRef() { 088 return comparatorRef; 089 } 090 091 public void setComparatorRef(String comparatorRef) { 092 this.comparatorRef = comparatorRef; 093 } 094 095 /** 096 * Sets the comparator to use for sorting 097 * 098 * @param comparator the comparator to use for sorting 099 * @return the builder 100 */ 101 public SortDefinition<T> comparator(Comparator<T> comparator) { 102 setComparator(comparator); 103 return this; 104 } 105 106 /** 107 * Sets a reference to lookup for the comparator to use for sorting 108 * 109 * @param ref reference for the comparator 110 * @return the builder 111 */ 112 public SortDefinition<T> comparatorRef(String ref) { 113 setComparatorRef(ref); 114 return this; 115 } 116}