1 /* 2 * ************************************************************************************************************************************************************* 3 * 4 * SteelBlue: DCI User Interfaces 5 * http://tidalwave.it/projects/steelblue 6 * 7 * Copyright (C) 2015 - 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/steelblue-src 22 * git clone https://github.com/tidalwave-it/steelblue-src 23 * 24 * ************************************************************************************************************************************************************* 25 */ 26 package it.tidalwave.ui.javafx.impl.common; 27 28 import jakarta.annotation.Nonnull; 29 import java.util.ArrayList; 30 import java.util.Collection; 31 import java.util.HashMap; 32 import java.util.List; 33 import java.util.Map; 34 import java.util.Optional; 35 import it.tidalwave.util.As; 36 import it.tidalwave.ui.core.role.UserAction; 37 import lombok.Getter; 38 import lombok.ToString; 39 import static java.util.Collections.*; 40 import static it.tidalwave.ui.core.role.UserActionProvider._UserActionProvider_; 41 42 /*************************************************************************************************************************************************************** 43 * 44 * @author Fabrizio Giudici 45 * 46 **************************************************************************************************************************************************************/ 47 @ToString 48 public class RoleBag 49 { 50 private final Map<Class<?>, List<Object>> map = new HashMap<>(); 51 52 /** The default user action, which is the first action of the first {@link it.tidalwave.ui.core.role.UserActionProvider}. */ 53 @Getter 54 private final Optional<UserAction> defaultUserAction; 55 56 public RoleBag() 57 { 58 defaultUserAction = Optional.empty(); 59 } 60 61 public RoleBag (@Nonnull final As source, @Nonnull final Collection<Class<?>> roleTypes) 62 { 63 // TODO: assert not FX thread 64 roleTypes.forEach(roleType -> copyRoles(source, roleType)); 65 // computed NOW because we are in the background thread 66 // TODO: perhaps it could be associated to a dummy key, instead of being returned by a getter? 67 defaultUserAction = getMany(_UserActionProvider_).stream().flatMap(a -> a.getOptionalDefaultAction().stream()).findFirst(); 68 } 69 70 public <ROLE_TYPE> void put (@Nonnull final Class<ROLE_TYPE> roleClass, @Nonnull final ROLE_TYPE role) 71 { 72 putMany(roleClass, singletonList(role)); 73 } 74 75 public <ROLE_TYPE> void putMany (@Nonnull final Class<ROLE_TYPE> roleClass, 76 @Nonnull final Collection<? extends ROLE_TYPE> roles) 77 { 78 map.put(roleClass, new ArrayList<>(roles)); 79 } 80 81 @Nonnull 82 public <ROLE_TYPE> Optional<ROLE_TYPE> get (@Nonnull final Class<ROLE_TYPE> roleClass) 83 { 84 return getMany(roleClass).stream().findFirst(); 85 } 86 87 @Nonnull 88 public <ROLE_TYPE> List<ROLE_TYPE> getMany (@Nonnull final Class<ROLE_TYPE> roleClass) 89 { 90 return unmodifiableList((List<ROLE_TYPE>)map.getOrDefault(roleClass, emptyList())); 91 } 92 93 94 private <ROLE_TYPE> void copyRoles (@Nonnull final As item, @Nonnull final Class<ROLE_TYPE> roleClass) 95 { 96 putMany(roleClass, item.asMany(roleClass)); 97 } 98 }