PresentationModelAsDelegateDecorator.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.common;

  30. import javax.annotation.Nonnull;
  31. import java.util.ArrayList;
  32. import java.util.Collection;
  33. import java.util.List;
  34. import it.tidalwave.role.ui.PresentationModel;
  35. import it.tidalwave.util.As;
  36. import it.tidalwave.util.AsException;
  37. import lombok.AccessLevel;
  38. import lombok.experimental.Delegate;
  39. import lombok.RequiredArgsConstructor;
  40. import lombok.ToString;

  41. /***********************************************************************************************************************
  42.  *
  43.  * A decorator for {@link PresentationModel} that also searches for roles in a specified delegate as a fallback.
  44.  *
  45.  * FIME: move to TFT
  46.  *
  47.  * @stereotype Decorator
  48.  * @author  Fabrizio Giudici
  49.  *
  50.  **********************************************************************************************************************/
  51. @RequiredArgsConstructor(access = AccessLevel.PRIVATE) @ToString
  52. public class PresentationModelAsDelegateDecorator implements PresentationModel
  53.   {
  54.     @Delegate(excludes = As.class) @Nonnull
  55.     private final PresentationModel pmDelegate;

  56.     private final As asDelegate;

  57.     @Nonnull
  58.     public static PresentationModel decorating (@Nonnull final PresentationModel pmDelegate,
  59.                                                 @Nonnull final As asDelegate)
  60.       {
  61.         return new PresentationModelAsDelegateDecorator(pmDelegate, asDelegate);
  62.       }

  63.     @Override @Nonnull
  64.     public <T> T as (@Nonnull final Class<T> type)
  65.       {
  66.         try
  67.           {
  68.             return pmDelegate.as(type);
  69.           }
  70.         catch (AsException e)
  71.           {
  72.             return asDelegate.as(type);
  73.           }
  74.       }

  75.     @Override @Nonnull
  76.     public <T> T as (@Nonnull final Class<T> type, @Nonnull final NotFoundBehaviour<T> notFoundBehaviour)
  77.       {
  78.         throw new UnsupportedOperationException("Not implemented yet");
  79.       }

  80.     @Override @Nonnull
  81.     public <T> Collection<T> asMany (@Nonnull final Class<T> type)
  82.       {
  83.         final List<T> results = new ArrayList<>();
  84.         results.addAll(pmDelegate.asMany(type));
  85.         results.addAll(asDelegate.asMany(type));

  86.         return results;
  87.       }
  88.   }