DefaultPresentationModel.java

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

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

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

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

  53.     private final As as;

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

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

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

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

  83.         if (t.isPresent())
  84.           {
  85.             return t;
  86.           }

  87.         if (owner instanceof As)
  88.           {
  89.             try
  90.               {
  91.                 final var role = ((As)owner).as(roleType);

  92.                 if (role != null) // do check it for improper implementations or partial mocks
  93.                   {
  94.                     return Optional.of(role);
  95.                   }
  96.               }
  97.             catch (AsException ignore)
  98.               {
  99.                 // fallback
  100.               }
  101.           }

  102.         return Optional.empty();
  103.       }

  104.     /***********************************************************************************************************************************************************
  105.      * {@inheritDoc}
  106.      **********************************************************************************************************************************************************/
  107.     @Override @Nonnull
  108.     public <T> Collection<T> asMany (@Nonnull final Class<? extends T> roleType)
  109.       {
  110.         final Collection<T> result = as.asMany(roleType);

  111.         // The problem here is that we want only to add local roles in owner; but calling owner.as() will also
  112.         // find again the global roles that were discovered by AsSupport.
  113.         if (roleType.isAssignableFrom(owner.getClass()))
  114.           {
  115.             result.add(roleType.cast(owner));
  116.           }

  117.         if (owner instanceof As)
  118.           {
  119.             result.addAll(((As)owner).asMany(roleType));
  120.           }

  121.         return result;
  122.       }

  123.     /***********************************************************************************************************************************************************
  124.      * {@inheritDoc}
  125.      **********************************************************************************************************************************************************/
  126.     @Override
  127.     public void dispose()
  128.       {
  129.         for (final var listener : pcs.getPropertyChangeListeners().clone())
  130.           {
  131.             pcs.removePropertyChangeListener(listener);
  132.           }

  133.         asMany(NamedCallback.class).stream()
  134.                                    .filter(c -> c.getName().equals(CALLBACK_DISPOSE))
  135.                                    .forEach(callback -> wrap(callback, "While calling 'dispose' callbacks"));
  136.       }

  137.     /***********************************************************************************************************************************************************
  138.      *
  139.      **********************************************************************************************************************************************************/
  140.     private static void wrap (@Nonnull final Callback callback, @Nonnull final String logMessage)
  141.       {
  142.         try
  143.           {
  144.             callback.call();
  145.           }
  146.         catch (Throwable t)
  147.           {
  148.             log.error(logMessage, t);
  149.           }
  150.       }
  151.   }