RoleBag.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * SteelBlue: DCI User Interfaces
  5.  * http://tidalwave.it/projects/steelblue
  6.  *
  7.  * Copyright (C) 2015 - 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/steelblue-src
  23.  * git clone https://github.com/tidalwave-it/steelblue-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.role.ui.javafx.impl.common;

  28. import javax.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.role.ui.UserAction;
  37. import lombok.Getter;
  38. import lombok.ToString;
  39. import static java.util.Collections.*;
  40. import static it.tidalwave.role.ui.UserActionProvider._UserActionProvider_;

  41. /***********************************************************************************************************************
  42.  *
  43.  * @author  Fabrizio Giudici
  44.  *
  45.  **********************************************************************************************************************/
  46. @ToString
  47. public class RoleBag
  48.   {
  49.     private final Map<Class<?>, List<Object>> map = new HashMap<>();

  50.     /** The default user action, which is he first action of the first
  51.      *  {@link it.tidalwave.role.ui.UserActionProvider}. */
  52.     @Getter
  53.     private final Optional<UserAction> defaultUserAction;

  54.     public RoleBag()
  55.       {
  56.         defaultUserAction = Optional.empty();
  57.       }

  58.     public RoleBag (@Nonnull final As source, @Nonnull final Collection<Class<?>> roleTypes)
  59.       {
  60.         // TODO: assert not FX thread
  61.         roleTypes.forEach(roleType -> copyRoles(source, roleType));
  62.         // computed NOW because we are in the background thread
  63.         // TODO: perhaps it could be associated to a dummy key, instead of being returned by a getter?
  64.         defaultUserAction = getMany(_UserActionProvider_).stream()
  65.                                             .flatMap(a -> a.getOptionalDefaultAction().stream())
  66.                                             .findFirst();
  67.       }

  68.     public <ROLE_TYPE> void put (@Nonnull final Class<ROLE_TYPE> roleClass, @Nonnull final ROLE_TYPE role)
  69.       {
  70.         putMany(roleClass, singletonList(role));
  71.       }

  72.     public <ROLE_TYPE> void putMany (@Nonnull final Class<ROLE_TYPE> roleClass,
  73.                                      @Nonnull final Collection<? extends ROLE_TYPE> roles)
  74.       {
  75.         map.put(roleClass, new ArrayList<>(roles));
  76.       }

  77.     @Nonnull
  78.     public <ROLE_TYPE> Optional<ROLE_TYPE> get (@Nonnull final Class<ROLE_TYPE> roleClass)
  79.       {
  80.         return getMany(roleClass).stream().findFirst();
  81.       }

  82.     @Nonnull
  83.     public <ROLE_TYPE> List<ROLE_TYPE> getMany (@Nonnull final Class<ROLE_TYPE> roleClass)
  84.       {
  85.         return unmodifiableList((List<ROLE_TYPE>)map.getOrDefault(roleClass, emptyList()));
  86.       }


  87.     private <ROLE_TYPE> void copyRoles (@Nonnull final As item, @Nonnull final Class<ROLE_TYPE> roleClass)
  88.       {
  89.         putMany(roleClass, item.asMany(roleClass));
  90.       }
  91.   }