• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.caliper.memory;
18 
19 /**
20  * A visitor that controls an object traversal. Implementations
21  * of this interface are passed to {@link ObjectExplorer} exploration methods.
22  *
23  * @param <T> the type of the result that this visitor returns
24  * (can be defined as {@code Void} to denote no result}.
25  *
26  * @see ObjectExplorer
27  */
28 public interface ObjectVisitor<T> {
29   /**
30    * Visits an explored value (the whole chain from the root object
31    * leading to the value is provided), and decides whether to continue
32    * the exploration of that value.
33    *
34    * <p>In case the explored value is either primitive or {@code null}
35    * (e.g., if {@code chain.isPrimitive() || chain.getValue() == null}),
36    * the return value is meaningless and is ignored.
37    *
38    * @param chain the chain that leads to the explored value.
39    * @return {@link Traversal#EXPLORE} to denote that the visited object
40    * should be further explored, or {@link Traversal#SKIP} to avoid
41    * exploring it.
42    */
visit(Chain chain)43   Traversal visit(Chain chain);
44 
45   /**
46    * Returns an arbitrary value (presumably constructed during the object
47    * graph traversal).
48    */
result()49   T result();
50 
51   /**
52    * Constants that denote how the traversal of a given object (chain)
53    * should continue.
54    */
55   enum Traversal {
56     /**
57      * The visited object should be further explored.
58      */
59     EXPLORE,
60 
61     /**
62      * The visited object should not be explored.
63      */
64     SKIP
65   }
66 }
67