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 * 52 * @param name the name 53 * @return the object 54 **********************************************************************************************************************************************************/ 55 @Nonnull 56 public Optional<T> getByName (@Nonnull String name); 57 58 /*********************************************************************************************************************************************************** 59 * Returns the names of contained objects. 60 * 61 * @return the names of the objects 62 * @since 3.1-ALPHA-8 63 **********************************************************************************************************************************************************/ 64 @Nonnull 65 public default Set<String> getNames() 66 { 67 return Collections.emptySet(); 68 } 69 70 /*********************************************************************************************************************************************************** 71 * Returns a new instance with the specified (name, value) pairs. 72 * 73 * @param <T> the static type of the value 74 * @param mapByName the map containing the pairs 75 * @return the new instance 76 * @since 3.2-ALPHA-1 77 **********************************************************************************************************************************************************/ 78 @Nonnull 79 public static <T> Aggregate<T> of (@Nonnull final Map<String, T> mapByName) 80 { 81 return new MapAggregate<>(mapByName); 82 } 83 84 /*********************************************************************************************************************************************************** 85 * Returns a new empty instance that will be populated by means of {@link #with(String, Object)}. 86 * 87 * @param <T> the static type of the aggregate 88 * @return the new instance 89 * @since 3.2-ALPHA-2 90 **********************************************************************************************************************************************************/ 91 @Nonnull 92 public static <T> Aggregate<T> newInstance() 93 { 94 return new MapAggregate<>(); 95 } 96 97 /*********************************************************************************************************************************************************** 98 * Returns a new instance with the specified (name, value) pair. 99 * 100 * @param <T> the static type of the aggregate 101 * @param name the name in the pair 102 * @param value the value in the pair 103 * @return the new instance 104 * @since 3.2-ALPHA-1 105 **********************************************************************************************************************************************************/ 106 @Nonnull 107 public static <T> Aggregate<T> of (@Nonnull final String name, @Nonnull final T value) 108 { 109 return new MapAggregate<T>().with(name, value); 110 } 111 112 /*********************************************************************************************************************************************************** 113 * Returns a new instance with the specified (name, value) pair. 114 * 115 * @param name the name in the pair 116 * @param value the value in the pair 117 * @return the new instance 118 * @since 3.2-ALPHA-1 119 **********************************************************************************************************************************************************/ 120 @Nonnull 121 public default Aggregate<T> with (@Nonnull final String name, @Nonnull final T value) 122 { 123 return new MapAggregate<T>().with(name, value); 124 } 125 }