Skip to content

Package: Composite$1

Composite$1

nameinstructionbranchcomplexitylinemethod
findChildren()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
{...}
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2025 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 the License.
12: * 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 an "AS IS" BASIS, WITHOUT WARRANTIES OR
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
22: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.role;
27:
28: import jakarta.annotation.Nonnull;
29: import java.util.Optional;
30: import java.util.function.Consumer;
31: import java.util.stream.Stream;
32: import it.tidalwave.util.Finder;
33:
34: /***************************************************************************************************************************************************************
35: *
36: * The role of a composite object, that is an object which contains children. They are exposed by means of a
37: * {@link Finder}.
38: *
39: * @stereotype Role
40: *
41: * @author Fabrizio Giudici
42: * @it.tidalwave.javadoc.stable
43: *
44: **************************************************************************************************************************************************************/
45: @FunctionalInterface
46: public interface Composite<T, F extends Finder<? extends T>>
47: {
48: /** Shortcut for {@link it.tidalwave.util.As}. */
49: public static final Class<Composite> _Composite_ = Composite.class;
50:
51: /***********************************************************************************************************************************************************
52: * A default <code>Composite</code> with no children.
53: **********************************************************************************************************************************************************/
54: public static final Composite<Object, Finder<Object>> DEFAULT = new Composite<>()
55: {
56: @Override @Nonnull
57: public Finder<Object> findChildren()
58: {
59: return Finder.empty();
60: }
61: };
62:
63: /***********************************************************************************************************************************************************
64: * {@return the children of this object}.
65: **********************************************************************************************************************************************************/
66: @Nonnull
67: public F findChildren();
68:
69: /***********************************************************************************************************************************************************
70: * {@return a stream of children}.
71: * @since 3.2-ALPHA-23
72: **********************************************************************************************************************************************************/
73: @Nonnull
74: public default Stream<? extends T> stream()
75: {
76: return findChildren().stream();
77: }
78:
79: /***********************************************************************************************************************************************************
80: * Iterates through children.
81: * @param consumer the consumer
82: * @since 3.2-ALPHA-23
83: **********************************************************************************************************************************************************/
84: public default void forEach (@Nonnull final Consumer<? super T> consumer)
85: {
86: stream().forEach(consumer);
87: }
88:
89: /***********************************************************************************************************************************************************
90: * A visitor that can travel through the {@code Composite} items.
91: * @param <T> the type of the composite
92: * @param <R> the type of the result
93: **********************************************************************************************************************************************************/
94: @SuppressWarnings("EmptyMethod")
95: public static interface Visitor<T, R>
96: {
97: /*******************************************************************************************************************************************************
98: * Visits an object. This method is called before visiting children (pre-order).
99: * @param object the visited object
100: ******************************************************************************************************************************************************/
101: public default void preVisit (@Nonnull final T object)
102: {
103: }
104:
105: /*******************************************************************************************************************************************************
106: * Visits an object. This method is actually called just after {@link #preVisit(Object)}, it makes sense to implement it when you don't need to
107: * distinguish between pre-order and post-order traversal.
108: * @param object the visited object
109: ******************************************************************************************************************************************************/
110: public default void visit (@Nonnull final T object)
111: {
112: }
113:
114: /*******************************************************************************************************************************************************
115: * Visits an object. This method is called after visiting children (post-order).
116: * @param object the visited object
117: ******************************************************************************************************************************************************/
118: public default void postVisit (@Nonnull final T object)
119: {
120: }
121:
122: /*******************************************************************************************************************************************************
123: * {@return the value of this visitor}.
124: ******************************************************************************************************************************************************/
125: @Nonnull
126: public default Optional<R> getValue()
127: {
128: return Optional.empty();
129: }
130: }
131: }