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.util; 018 019 import java.util.List; 020 021 import org.apache.camel.model.ChoiceType; 022 import org.apache.camel.model.ProcessorType; 023 import org.apache.camel.model.WhenType; 024 025 /** 026 * Helper class for ProcessorType and the other model classes. 027 */ 028 public final class ProcessorTypeHelper { 029 030 private ProcessorTypeHelper() { 031 } 032 033 /** 034 * Looks for the given type in the list of outputs and recurring all the children as well. 035 * Will stop at first found and return it. 036 * 037 * @param outputs list of outputs, can be null or empty. 038 * @param type the type to look for 039 * @return the first found type, or <tt>null</tt> if not found 040 */ 041 @SuppressWarnings("unchecked") 042 public static <T> T findFirstTypeInOutputs(List<ProcessorType<?>> outputs, Class<T> type) { 043 if (outputs == null || outputs.isEmpty()) { 044 return null; 045 } 046 047 for (ProcessorType out : outputs) { 048 if (type.isInstance(out)) { 049 return type.cast(out); 050 } 051 052 // special for choice 053 if (out instanceof ChoiceType) { 054 ChoiceType choice = (ChoiceType) out; 055 for (WhenType when : choice.getWhenClauses()) { 056 List<ProcessorType<?>> children = when.getOutputs(); 057 T child = findFirstTypeInOutputs(children, type); 058 if (child != null) { 059 return child; 060 } 061 } 062 063 List<ProcessorType<?>> children = choice.getOtherwise().getOutputs(); 064 T child = findFirstTypeInOutputs(children, type); 065 if (child != null) { 066 return child; 067 } 068 } 069 070 // try children as well 071 List<ProcessorType<?>> children = out.getOutputs(); 072 T child = findFirstTypeInOutputs(children, type); 073 if (child != null) { 074 return child; 075 } 076 } 077 078 return null; 079 } 080 081 }