PresentationModelAsDelegateDecorator.java

  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. import jakarta.annotation.Nonnull;
  28. import java.util.ArrayList;
  29. import java.util.Collection;
  30. import java.util.List;
  31. import java.util.Optional;
  32. import it.tidalwave.ui.core.role.PresentationModel;
  33. import it.tidalwave.util.As;
  34. import it.tidalwave.util.AsException;
  35. import lombok.AccessLevel;
  36. import lombok.RequiredArgsConstructor;
  37. import lombok.ToString;
  38. import lombok.experimental.Delegate;

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

  54.     private final As asDelegate;

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

  61.     @Override @Nonnull
  62.     public <T> T as (@Nonnull final Class<? extends T> type)
  63.       {
  64.         return maybeAs(type).orElseThrow(() -> new AsException(type));
  65.       }

  66.     @Override @Nonnull
  67.     public <T> Optional<T> maybeAs (@Nonnull final Class<? extends T> type)
  68.       {
  69.         final Optional<T> t = pmDelegate.maybeAs(type);
  70.         return t.isPresent() ? t : asDelegate.maybeAs(type);
  71.         // .or(() -> (Optional<T>)asDelegate.maybeAs(type));
  72.       }

  73.     @Override @Nonnull
  74.     public <T> Collection<T> asMany (@Nonnull final Class<? extends T> type)
  75.       {
  76.         final List<T> results = new ArrayList<>();
  77.         results.addAll(pmDelegate.asMany(type));
  78.         results.addAll(asDelegate.asMany(type));

  79.         return results;
  80.       }
  81.   }