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.types.ClassNode; 022import org.apache.reef.tang.types.NamedParameterNode; 023import org.apache.reef.tang.types.Node; 024import org.apache.reef.tang.types.PackageNode; 025 026/** 027 * Generic interface to traverse nodes of the class hierarchy. 028 * Dispatches between ClassNode, PackageNode, and NamedParameterNode types. 029 * It is used e.g. in Walk.preorder() 030 */ 031public abstract class AbstractClassHierarchyNodeVisitor implements NodeVisitor<Node> { 032 033 /** 034 * Manually dispatch between different types of Nodes and call a proper visit() method. 035 * Currently dispatches between ClassNode, PackageNode, and NamedParameterNode types. 036 * 037 * @param node TANG configuration node. 038 * @return true to proceed with the next node, false to cancel. 039 * @throws ClassCastException if Node is not one of ClassNode, PackageNode, 040 * or NamedParameterNode. 041 */ 042 @Override 043 public boolean visit(final Node node) { 044 if (node instanceof ClassNode) { 045 return visit((ClassNode<?>) node); 046 } else if (node instanceof PackageNode) { 047 return visit((PackageNode) node); 048 } else if (node instanceof NamedParameterNode) { 049 return visit((NamedParameterNode<?>) node); 050 } 051 throw new ClassCastException( 052 "Node " + node.getClass() + " cannot be casted to one of the known subclasses." 053 + " Override this method to handle the case."); 054 } 055 056 /** 057 * Process current configuration node of ClassNode type. 058 * 059 * @param node Current configuration node. 060 * @return true to proceed with the next node, false to cancel. 061 */ 062 public abstract boolean visit(ClassNode<?> node); 063 064 /** 065 * Process current configuration node of PackageNode type. 066 * 067 * @param node Current configuration node. 068 * @return true to proceed with the next node, false to cancel. 069 */ 070 public abstract boolean visit(PackageNode node); 071 072 /** 073 * Process current configuration node of NamedParameterNode type. 074 * 075 * @param node Current configuration node. 076 * @return true to proceed with the next node, false to cancel. 077 */ 078 public abstract boolean visit(NamedParameterNode<?> node); 079}