Skip to contentMethod: preVisit(Object)
1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.role;
28:
29: import javax.annotation.Nonnull;
30: import it.tidalwave.util.Finder;
31: import it.tidalwave.util.NotFoundException;
32:
33: /***********************************************************************************************************************
34: *
35: * The role of a composite object, that is an object which contains children. They are exposed by means of a
36: * {@link Finder}.
37: *
38: * @stereotype Role
39: *
40: * @author Fabrizio Giudici
41: * @it.tidalwave.javadoc.stable
42: *
43: **********************************************************************************************************************/
44: @FunctionalInterface
45: public interface Composite<TYPE, SPECIALIZED_FINDER extends Finder<? extends TYPE>>
46: {
47: public static final Class<Composite> _Composite_ = Composite.class;
48:
49: /*******************************************************************************************************************
50: *
51: * A default <code>Composite</code> with no children.
52: *
53: ******************************************************************************************************************/
54: public static final Composite<Object, Finder<Object>> DEFAULT = new Composite<Object, Finder<Object>>()
55: {
56: @Override @Nonnull
57: public Finder<Object> findChildren()
58: {
59: return Finder.empty();
60: }
61: };
62:
63: /*******************************************************************************************************************
64: *
65: * Returns the children of this object.
66: *
67: * @return the children
68: *
69: ******************************************************************************************************************/
70: @Nonnull
71: public SPECIALIZED_FINDER findChildren();
72:
73: /*******************************************************************************************************************
74: *
75: *
76: ******************************************************************************************************************/
77: @SuppressWarnings("EmptyMethod")
78: public static interface Visitor<T, R>
79: {
80: /***************************************************************************************************************
81: *
82: * Visits an object. This method is called before visiting children (pre-order).
83: *
84: * @param object the visited object
85: *
86: **************************************************************************************************************/
87: public void preVisit (@Nonnull T object);
88:
89: /***************************************************************************************************************
90: *
91: * Visits an object. This method is actually called just after {@link #preVisit(Object)}, it makes sense to
92: * implement it when you don't need to distinguish between pre-order and post-order traversal.
93: *
94: * @param object the visited object
95: *
96: **************************************************************************************************************/
97: public void visit (@Nonnull T object);
98:
99: /***************************************************************************************************************
100: *
101: * Visits an object. This method is called after visiting children (post-order).
102: *
103: * @param object the visited object
104: *
105: **************************************************************************************************************/
106: public void postVisit (@Nonnull T object);
107:
108: /***************************************************************************************************************
109: *
110: * Returns the value of this visitor.
111: *
112: * @return the value
113: * @throws NotFoundException when no value has been found
114: *
115: **************************************************************************************************************/
116: @Nonnull
117: public R getValue()
118: throws NotFoundException;
119: }
120:
121: /*******************************************************************************************************************
122: *
123: * A support class for {@link Visitor} which provides default empty methods.
124: *
125: ******************************************************************************************************************/
126: public static class VisitorSupport<T, R> implements Visitor<T, R>
127: {
128: /** {@inheritDoc} */
129: @Override
130: public void preVisit (@Nonnull final T object)
131: {
132: }
133:
134: /** {@inheritDoc} */
135: @Override
136: public void visit (@Nonnull final T object)
137: {
138: }
139:
140: /** {@inheritDoc} */
141: @Override
142: public void postVisit (@Nonnull final T object)
143: {
144: }
145:
146: /** {@inheritDoc} */
147: @Override @Nonnull
148: public R getValue()
149: throws NotFoundException
150: {
151: throw new NotFoundException("Must be implemented by subclasses");
152: }
153: }
154: }