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.wicket.util.visit;
018
019/**
020 * Implementation of {@link IVisit} used by traversal algorithms
021 * 
022 * @author igor.vaynberg
023 * 
024 * @param <R>
025 *            type of object that should be returned by the visit/traversal
026 */
027public class Visit<R> implements IVisit<R>
028{
029        private static enum Action {
030                CONTINUE, CONTINUE_BUT_DONT_GO_DEEPER, STOP
031        }
032
033        private R result;
034        private Action action = Action.CONTINUE;
035
036        /** {@inheritDoc} */
037        @Override
038        public void stop()
039        {
040                stop(null);
041        }
042
043        /** {@inheritDoc} */
044        @Override
045        public void stop(final R result)
046        {
047                action = Action.STOP;
048                this.result = result;
049        }
050
051        /** {@inheritDoc} */
052        @Override
053        public void dontGoDeeper()
054        {
055                action = Action.CONTINUE_BUT_DONT_GO_DEEPER;
056        }
057
058        /**
059         * Checks if the visit/traversal has been stopped
060         * 
061         * @return {@code true} if the visit/traversal has been stopped
062         */
063        public boolean isStopped()
064        {
065                return action == Action.STOP;
066        }
067
068        /**
069         * Checks if the visit/traversal should continue
070         * 
071         * @return {@code true} if the visit/traversal should continue
072         */
073        public boolean isContinue()
074        {
075                return action == Action.CONTINUE;
076        }
077
078        /**
079         * Checks if the visit/traversal has been stopped from visiting children of the currently
080         * visited object
081         * 
082         * @return {@code true} if the visit/traversal should not visit children of the currently
083         *         visited object
084         */
085        public boolean isDontGoDeeper()
086        {
087                return action == Action.CONTINUE_BUT_DONT_GO_DEEPER;
088        }
089
090        /**
091         * Gets the result of the visit/traversal. This value is set using {@link #stop(Object)} or
092         * remains {@code null} if visit/traversal has ended in any other way
093         * 
094         * @return value that should be returned to the method that initiated the visit/traversal
095         */
096        public R getResult()
097        {
098                return result;
099        }
100}