Displayable.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.core.role;

  27. import jakarta.annotation.Nonnull;
  28. import java.util.Comparator;
  29. import java.util.function.Consumer;
  30. import java.util.function.Function;
  31. import java.util.function.Supplier;
  32. import it.tidalwave.util.As;
  33. import it.tidalwave.ui.core.role.impl.AsDisplayableComparator;
  34. import it.tidalwave.ui.core.role.impl.DefaultDisplayable;
  35. import it.tidalwave.ui.core.role.impl.DisplayableComparator;
  36. import static it.tidalwave.util.BundleUtilities.getMessage;

  37. /***************************************************************************************************************************************************************
  38.  *
  39.  * The role of an object which can provide its own display name.
  40.  *
  41.  * @stereotype Role
  42.  *
  43.  * @since   2.0-ALPHA-1
  44.  * @author  Fabrizio Giudici
  45.  * @it.tidalwave.javadoc.stable
  46.  *
  47.  **************************************************************************************************************************************************************/
  48. @FunctionalInterface
  49. public interface Displayable
  50.   {
  51.     public static final Class<Displayable> _Displayable_ = Displayable.class;

  52.     /***********************************************************************************************************************************************************
  53.      * A default {@code Displayable} with an empty display name.
  54.      **********************************************************************************************************************************************************/
  55.     public static final Displayable DEFAULT = new DefaultDisplayable("", "DEFAULT");

  56.     /***********************************************************************************************************************************************************
  57.      * Returns the display name in the current {@link java.util.Locale}.
  58.      *
  59.      * @return  the display name
  60.      **********************************************************************************************************************************************************/
  61.     @Nonnull
  62.     public String getDisplayName();

  63.     /***********************************************************************************************************************************************************
  64.      * Sends the display name in the current {@link java.util.Locale} to a given customer.
  65.      *
  66.      * @param     consumer    the {@code Consumer}
  67.      * @since     3.2-ALPHA-15
  68.      **********************************************************************************************************************************************************/
  69.     @SuppressWarnings("BoundedWildcard")
  70.     public default void display (@Nonnull final Consumer<String> consumer)
  71.       {
  72.         consumer.accept(getDisplayName());
  73.       }

  74.     /***********************************************************************************************************************************************************
  75.      * Creates an instance with a given display name.
  76.      *
  77.      * @param  displayName    the display name
  78.      * @return                the new instance
  79.      * @since                 3.2-ALPHA-1 (was {@code DefaultDisplayable}
  80.      **********************************************************************************************************************************************************/
  81.     @Nonnull
  82.     public static Displayable of (@Nonnull final String displayName)
  83.       {
  84.         return of(displayName, "???");
  85.       }

  86.     /***********************************************************************************************************************************************************
  87.      * Creates an instance with a given display name iand an explicit label for  {@code toString()}.
  88.      *
  89.      * @param  displayName    the display name
  90.      * @param  toStringName   the name to be rendered when {@code toString()} is called
  91.      * @return                the new instance
  92.      * @since                 3.2-ALPHA-1 (was {@code DefaultDisplayable}
  93.      **********************************************************************************************************************************************************/
  94.     @Nonnull
  95.     public static Displayable of (@Nonnull final String displayName, @Nonnull final String toStringName)
  96.       {
  97.         return new DefaultDisplayable(displayName, toStringName);
  98.       }

  99.     /***********************************************************************************************************************************************************
  100.      * Creates an instance from a {@link Supplier}{@code <String>}. The supplier is invoked each time
  101.      * {@link #getDisplayName()} is called.
  102.      *
  103.      * @param   supplier      the {@code Supplier}
  104.      * @return                the new instance
  105.      * @since                 3.2-ALPHA-3
  106.      * @it.tidalwave.javadoc.experimental
  107.      **********************************************************************************************************************************************************/
  108.     @Nonnull
  109.     public static Displayable of (@Nonnull final Supplier<String> supplier)
  110.       {
  111.         return supplier::get;
  112.       }

  113.     /***********************************************************************************************************************************************************
  114.      * Creates an instance from a {@link Function}{@code <T, String>} and a generic object that the function is applied
  115.      * to. The function is invoked each time {@link #getDisplayName()} is called.
  116.      *
  117.      * @param   <T>           the type of the object
  118.      * @param   function      the {@code Function}
  119.      * @param   object        the object
  120.      * @return                the new instance
  121.      * @since                 3.2-ALPHA-3
  122.      * @it.tidalwave.javadoc.experimental
  123.      **********************************************************************************************************************************************************/
  124.     @Nonnull
  125.     public static <T> Displayable of (@Nonnull final Function<T, String> function, @Nonnull final T object)
  126.       {
  127.         return () -> function.apply(object);
  128.       }

  129.     /***********************************************************************************************************************************************************
  130.      * Creates a {@link LocalizedDisplayable} from a resource bundle. The bundle resource file is named
  131.      * {@code Bundle.properties} and it should be placed in the same package as the owner class.
  132.      *
  133.      * @param   ownerClass    the class that owns the bundle
  134.      * @param   key           the resource key
  135.      * @return                the new instance
  136.      * @since                 3.2-ALPHA-1 (was previously in {@code Displayable8}
  137.      **********************************************************************************************************************************************************/
  138.     @Nonnull
  139.     public static LocalizedDisplayable fromBundle (@Nonnull final Class<?> ownerClass, @Nonnull final String key)
  140.       {
  141.         return new DefaultDisplayable(getMessage(ownerClass, key));
  142.       }

  143.     /***********************************************************************************************************************************************************
  144.      * Returns a {@link Comparator} for comparing two instances of {@code Displayable}.
  145.      *
  146.      * @return  the {@code Comparator}
  147.      * @since   3.2-ALPHA-6
  148.      **********************************************************************************************************************************************************/
  149.     @Nonnull
  150.     public static Comparator<Displayable> comparing()
  151.       {
  152.         return DisplayableComparator.getInstance();
  153.       }

  154.     /***********************************************************************************************************************************************************
  155.      * Returns a {@link Comparator} for comparing two instances of objects implementing {@code As} that contain the
  156.      * {@code Displayable} role.
  157.      *
  158.      * @return  the {@code Comparator}
  159.      * @since   3.2-ALPHA-6
  160.      **********************************************************************************************************************************************************/
  161.     @Nonnull
  162.     public static Comparator<As> asComparing()
  163.       {
  164.         return AsDisplayableComparator.getInstance();
  165.       }
  166.   }