DefaultPresentationModel.java

  1. /*
  2.  * *********************************************************************************************************************
  3.  *
  4.  * TheseFoolishThings: Miscellaneous utilities
  5.  * http://tidalwave.it/projects/thesefoolishthings
  6.  *
  7.  * Copyright (C) 2009 - 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/thesefoolishthings-src
  23.  * git clone https://github.com/tidalwave-it/thesefoolishthings-src
  24.  *
  25.  * *********************************************************************************************************************
  26.  */
  27. package it.tidalwave.role.ui.impl;

  28. import javax.annotation.Nonnull;
  29. import java.beans.PropertyChangeSupport;
  30. import java.util.Collection;
  31. import java.util.Optional;
  32. import it.tidalwave.util.As;
  33. import it.tidalwave.util.AsException;
  34. import it.tidalwave.util.Callback;
  35. import it.tidalwave.util.NamedCallback;
  36. import it.tidalwave.role.ui.PresentationModel;
  37. import lombok.ToString;
  38. import lombok.experimental.Delegate;
  39. import lombok.extern.slf4j.Slf4j;

  40. /***********************************************************************************************************************
  41.  *
  42.  * A default implementation of {@link PresentationModel}.
  43.  *
  44.  * @author  Fabrizio Giudici
  45.  *
  46.  **********************************************************************************************************************/
  47. @ToString(exclude = {"as", "pcs"}) @Slf4j
  48. public class DefaultPresentationModel implements PresentationModel
  49.   {
  50.     @Nonnull
  51.     private final Object owner;

  52.     @Delegate
  53.     private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);

  54.     private final As as;

  55.     /*******************************************************************************************************************
  56.      *
  57.      *
  58.      *
  59.      ******************************************************************************************************************/
  60.     public DefaultPresentationModel (@Nonnull final Object owner, @Nonnull final Collection<Object> roles)
  61.       {
  62.         this.owner = owner;
  63.         as = As.forObject(owner, roles);
  64.       }

  65.     /*******************************************************************************************************************
  66.      *
  67.      * {@inheritDoc}
  68.      *
  69.      ******************************************************************************************************************/
  70.     @Override @Nonnull
  71.     public <T> T as (@Nonnull final Class<? extends T> roleType)
  72.       {
  73.         return maybeAs(roleType).orElseThrow(() -> new AsException(roleType));
  74.       }

  75.     /*******************************************************************************************************************
  76.      *
  77.      * {@inheritDoc}
  78.      *
  79.      ******************************************************************************************************************/
  80.     @SuppressWarnings("ConstantValue")
  81.     @Override @Nonnull
  82.     public <T> Optional<T> maybeAs (@Nonnull final Class<? extends T> roleType)
  83.       {
  84.         // Undocumented feature: for instance Zephyr needs to fire property events
  85.         if (roleType.equals(PropertyChangeSupport.class))
  86.           {
  87.             return Optional.of(roleType.cast(pcs));
  88.           }

  89.         final Optional<T> t = as.maybeAs(roleType);

  90.         if (t.isPresent())
  91.           {
  92.             return t;
  93.           }

  94.         if (owner instanceof As)
  95.           {
  96.             try
  97.               {
  98.                 final var role = ((As)owner).as(roleType);

  99.                 if (role != null) // do check it for improper implementations or partial mocks
  100.                   {
  101.                     return Optional.of(role);
  102.                   }
  103.               }
  104.             catch (AsException e)
  105.               {
  106.                 // fallback
  107.               }
  108.           }

  109.         return Optional.empty();
  110.       }

  111.     /*******************************************************************************************************************
  112.      *
  113.      * {@inheritDoc}
  114.      *
  115.      ******************************************************************************************************************/
  116.     @Override @Nonnull
  117.     public <T> Collection<T> asMany (@Nonnull final Class<? extends T> roleType)
  118.       {
  119.         final Collection<T> result = as.asMany(roleType);

  120.         // The problem here is that we want only to add local roles in owner; but calling owner.as() will also
  121.         // find again the global roles that were discovered by AsSupport.
  122.         if (roleType.isAssignableFrom(owner.getClass()))
  123.           {
  124.             result.add(roleType.cast(owner));
  125.           }

  126.         if (owner instanceof As)
  127.           {
  128.             result.addAll(((As)owner).asMany(roleType));
  129.           }

  130.         return result;
  131.       }

  132.     /*******************************************************************************************************************
  133.      *
  134.      * {@inheritDoc}
  135.      *
  136.      ******************************************************************************************************************/
  137.     @Override
  138.     public void dispose()
  139.       {
  140.         for (final var listener : pcs.getPropertyChangeListeners().clone())
  141.           {
  142.             pcs.removePropertyChangeListener(listener);
  143.           }

  144.         asMany(NamedCallback.class).stream()
  145.                                    .filter(c -> c.getName().equals(CALLBACK_DISPOSE))
  146.                                    .forEach(callback -> wrap(callback, "While calling 'dispose' callbacks"));
  147.       }

  148.     /*******************************************************************************************************************
  149.      *
  150.      ******************************************************************************************************************/
  151.     private static void wrap (@Nonnull final Callback callback, @Nonnull final String logMessage)
  152.       {
  153.         try
  154.           {
  155.             callback.call();
  156.           }
  157.         catch (Throwable t)
  158.           {
  159.             log.error(logMessage, t);
  160.           }
  161.       }
  162.   }