1 /*
2 * *************************************************************************************************************************************************************
3 *
4 * TheseFoolishThings: Miscellaneous utilities
5 * http://tidalwave.it/projects/thesefoolishthings
6 *
7 * Copyright (C) 2009 - 2024 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 javax.annotation.Nonnull;
29 import java.util.Collections;
30 import java.util.Map;
31 import java.util.Optional;
32 import java.util.Set;
33 import it.tidalwave.role.impl.MapAggregate;
34
35 /***************************************************************************************************************************************************************
36 *
37 * The role of an aggregate object, that is an object which contains other named objects.
38 *
39 * @stereotype Role
40 *
41 * @author Fabrizio Giudici
42 *
43 **************************************************************************************************************************************************************/
44 @FunctionalInterface
45 public interface Aggregate<T>
46 {
47 public static final Class<Aggregate> _Aggregate_ = Aggregate.class;
48
49 /***********************************************************************************************************************************************************
50 * Returns an object given its name.
51 * @param name the name
52 * @return the object
53 **********************************************************************************************************************************************************/
54 @Nonnull
55 public Optional<T> getByName (@Nonnull String name);
56
57 /***********************************************************************************************************************************************************
58 * Returns the names of contained objects.
59 * @return the names of the objects
60 * @since 3.1-ALPHA-8
61 **********************************************************************************************************************************************************/
62 @Nonnull
63 public default Set<String> getNames()
64 {
65 return Collections.emptySet();
66 }
67
68 /***********************************************************************************************************************************************************
69 * Returns a new instance with the specified (name, value) pairs.
70 * @param <T> the static type of the value
71 * @param mapByName the map containing the pairs
72 * @return the new instance
73 * @since 3.2-ALPHA-1
74 **********************************************************************************************************************************************************/
75 @Nonnull
76 public static <T> Aggregate<T> of (@Nonnull final Map<String, T> mapByName)
77 {
78 return new MapAggregate<>(mapByName);
79 }
80
81 /***********************************************************************************************************************************************************
82 * Returns a new empty instance that will be populated by means of {@link #with(String, Object)}.
83 * @param <T> the static type of the aggregate
84 * @return the new instance
85 * @since 3.2-ALPHA-2
86 **********************************************************************************************************************************************************/
87 @Nonnull
88 public static <T> Aggregate<T> newInstance()
89 {
90 return new MapAggregate<>();
91 }
92
93 /***********************************************************************************************************************************************************
94 * Returns a new instance with the specified (name, value) pair.
95 * @param <T> the static type of the aggregate
96 * @param name the name in the pair
97 * @param value the value in the pair
98 * @return the new instance
99 * @since 3.2-ALPHA-1
100 **********************************************************************************************************************************************************/
101 @Nonnull
102 public static <T> Aggregate<T> of (@Nonnull final String name, @Nonnull final T value)
103 {
104 return new MapAggregate<T>().with(name, value);
105 }
106
107 /***********************************************************************************************************************************************************
108 * Returns a new instance with the specified (name, value) pair.
109 * @param name the name in the pair
110 * @param value the value in the pair
111 * @return the new instance
112 * @since 3.2-ALPHA-1
113 **********************************************************************************************************************************************************/
114 @Nonnull
115 public default Aggregate<T> with (@Nonnull final String name, @Nonnull final T value)
116 {
117 return new MapAggregate<T>().with(name, value);
118 }
119 }