001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.tang.util.walk; 020 021import org.apache.reef.tang.implementation.Constructor; 022import org.apache.reef.tang.implementation.InjectionPlan; 023import org.apache.reef.tang.implementation.Subplan; 024import org.apache.reef.tang.implementation.java.JavaInstance; 025 026/** 027 * Generic interface to traverse nodes of the injection plan. 028 * Dispatches between Constructor, Subplan, RequiredSingleton, and JavaInstance types. 029 * It is used e.g. in Walk.preorder() 030 */ 031public abstract class AbstractInjectionPlanNodeVisitor implements NodeVisitor<InjectionPlan<?>> { 032 033 /** 034 * Manually dispatch between different types of injection plan objects and call proper 035 * visit() method. Currently dispatches between Constructor, Subplan, RequiredSingleton, 036 * and JavaInstance types. 037 * 038 * @param node TANG injection plan node. 039 * @return true to proceed with the next node, false to cancel. 040 * @throws ClassCastException if argument is not one of Constructor, Subplan, 041 * RequiredSingleton, or JavaInstance. 042 */ 043 @Override 044 public boolean visit(final InjectionPlan<?> node) { 045 if (node instanceof Constructor<?>) { 046 return visit((Constructor<?>) node); 047 } else if (node instanceof Subplan<?>) { 048 return visit((Subplan<?>) node); 049 } else if (node instanceof JavaInstance<?>) { 050 return visit((JavaInstance<?>) node); 051 } 052 throw new ClassCastException( 053 "Node " + node.getClass() + " cannot be casted to one of the known subclasses." 054 + " Override this method to handle the case."); 055 } 056 057 /** 058 * Process current injection plan node of Constructor type. 059 * 060 * @param node Current injection plan node. 061 * @return true to proceed with the next node, false to cancel. 062 */ 063 public abstract boolean visit(Constructor<?> node); 064 065 /** 066 * Process current injection plan node of JavaInstance type. 067 * 068 * @param node Current injection plan node. 069 * @return true to proceed with the next node, false to cancel. 070 */ 071 public abstract boolean visit(JavaInstance<?> node); 072 073 /** 074 * Process current injection plan node of Subplan type. 075 * 076 * @param node Current injection plan node. 077 * @return true to proceed with the next node, false to cancel. 078 */ 079 public abstract boolean visit(Subplan<?> node); 080}