RoleBag.java

  1. /*
  2.  * #%L
  3.  * *********************************************************************************************************************
  4.  *
  5.  * SteelBlue
  6.  * http://steelblue.tidalwave.it - git clone git@bitbucket.org:tidalwave/steelblue-src.git
  7.  * %%
  8.  * Copyright (C) 2015 - 2021 Tidalwave s.a.s. (http://tidalwave.it)
  9.  * %%
  10.  *
  11.  * *********************************************************************************************************************
  12.  *
  13.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  14.  * the License. You may obtain a copy of the License at
  15.  *
  16.  *     http://www.apache.org/licenses/LICENSE-2.0
  17.  *
  18.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  19.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  20.  * specific language governing permissions and limitations under the License.
  21.  *
  22.  * *********************************************************************************************************************
  23.  *
  24.  *
  25.  *
  26.  * *********************************************************************************************************************
  27.  * #L%
  28.  */
  29. package it.tidalwave.role.ui.javafx.impl;

  30. import javax.annotation.Nonnull;
  31. import java.util.ArrayList;
  32. import java.util.Collection;
  33. import java.util.HashMap;
  34. import java.util.List;
  35. import java.util.Map;
  36. import java.util.Optional;
  37. import it.tidalwave.util.As;
  38. import lombok.NoArgsConstructor;
  39. import lombok.ToString;
  40. import static java.util.Collections.*;

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

  50.     public RoleBag (final @Nonnull As source, final @Nonnull List<Class<?>> roleTypes)
  51.       {
  52.         roleTypes.forEach(roleType -> copyRoles(source, roleType));
  53.       }

  54.     public <ROLE_TYPE> void put (final @Nonnull Class<ROLE_TYPE> roleClass, final @Nonnull ROLE_TYPE role)
  55.       {
  56.         putMany(roleClass, singletonList(role));
  57.       }

  58.     public <ROLE_TYPE> void putMany (final @Nonnull Class<ROLE_TYPE> roleClass,
  59.                                      final @Nonnull Collection<? extends ROLE_TYPE> roles)
  60.       {
  61.         map.put(roleClass, new ArrayList<>(roles));
  62.       }

  63.     @Nonnull
  64.     public <ROLE_TYPE> Optional<ROLE_TYPE> get (final @Nonnull Class<ROLE_TYPE> roleClass)
  65.       {
  66.         return getMany(roleClass).stream().findFirst();
  67.       }

  68.     @Nonnull
  69.     public <ROLE_TYPE> List<ROLE_TYPE> getMany (final @Nonnull Class<ROLE_TYPE> roleClass)
  70.       {
  71.         return unmodifiableList((List<ROLE_TYPE>)map.getOrDefault(roleClass, emptyList()));
  72.       }

  73.     private <ROLE_TYPE> void copyRoles (final @Nonnull As item, final @Nonnull Class<ROLE_TYPE> roleClass)
  74.       {
  75.         putMany(roleClass, item.asMany(roleClass));
  76.       }
  77.   }