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.Locale;
  30. import java.util.Map;
  31. import java.util.SortedSet;
  32. import java.util.TreeSet;
  33. import java.util.function.Consumer;
  34. import java.util.function.Function;
  35. import java.util.function.Supplier;
  36. import it.tidalwave.util.As;
  37. import it.tidalwave.ui.core.role.impl.AsDisplayableComparator;
  38. import it.tidalwave.ui.core.role.impl.DefaultDisplayable;
  39. import it.tidalwave.ui.core.role.impl.DisplayableComparator;
  40. import static it.tidalwave.util.BundleUtilities.getMessage;

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

  56.     /***********************************************************************************************************************************************************
  57.      * A default {@code Displayable} with an empty display name.
  58.      **********************************************************************************************************************************************************/
  59.     public static final Displayable DEFAULT = new DefaultDisplayable("", "DEFAULT");

  60.     /***********************************************************************************************************************************************************
  61.      * Returns the display name in the current {@link java.util.Locale}.
  62.      *
  63.      * @return  the display name
  64.      **********************************************************************************************************************************************************/
  65.     @Nonnull
  66.     public String getDisplayName();

  67.     /***********************************************************************************************************************************************************
  68.      * Returns the display name in the given {@link Locale}.
  69.      * @param    locale       the {@code Locale}
  70.      * @since                 2.0-ALPHA-2
  71.      * @return                the display name
  72.      **********************************************************************************************************************************************************/
  73.     @Nonnull
  74.     public default String getDisplayName (@Nonnull final Locale locale)
  75.       {
  76.         return getDisplayName();
  77.       }

  78.     /***********************************************************************************************************************************************************
  79.      * Returns all the display names in {@link Map} where they are indexed by {@code Locale}.
  80.      * @since                 2.0-ALPHA-2
  81.      * @return                the display names
  82.      **********************************************************************************************************************************************************/
  83.     @Nonnull
  84.     public default Map<Locale, String> getDisplayNames()
  85.       {
  86.         return Map.of();
  87.       }

  88.     /***********************************************************************************************************************************************************
  89.      * Returns the supported {@code Locale}s.
  90.      * @since                 2.0-ALPHA-2
  91.      * @return                the available {@code Locale}s
  92.      **********************************************************************************************************************************************************/
  93.     @Nonnull
  94.     public default SortedSet<Locale> getLocales()
  95.       {
  96.         return new TreeSet<>();
  97.       }

  98.     /***********************************************************************************************************************************************************
  99.      * Sends the display name in the current {@link java.util.Locale} to a given customer.
  100.      *
  101.      * @param     consumer    the {@code Consumer}
  102.      * @since     3.2-ALPHA-15
  103.      **********************************************************************************************************************************************************/
  104.     @SuppressWarnings("BoundedWildcard")
  105.     public default void display (@Nonnull final Consumer<String> consumer)
  106.       {
  107.         consumer.accept(getDisplayName());
  108.       }

  109.     /***********************************************************************************************************************************************************
  110.      * Creates an instance with a given display name.
  111.      *
  112.      * @param  displayName    the display name
  113.      * @return                the new instance
  114.      * @since                 3.2-ALPHA-1 (was {@code DefaultDisplayable}
  115.      **********************************************************************************************************************************************************/
  116.     @Nonnull
  117.     public static Displayable of (@Nonnull final String displayName)
  118.       {
  119.         return of(displayName, "???");
  120.       }

  121.     /***********************************************************************************************************************************************************
  122.      * Creates an instance with a given display name iand an explicit label for  {@code toString()}.
  123.      *
  124.      * @param  displayName    the display name
  125.      * @param  toStringName   the name to be rendered when {@code toString()} is called
  126.      * @return                the new instance
  127.      * @since                 3.2-ALPHA-1 (was {@code DefaultDisplayable}
  128.      **********************************************************************************************************************************************************/
  129.     @Nonnull
  130.     public static Displayable of (@Nonnull final String displayName, @Nonnull final String toStringName)
  131.       {
  132.         return new DefaultDisplayable(displayName, toStringName);
  133.       }

  134.     /***********************************************************************************************************************************************************
  135.      * Creates an instance from a {@link Supplier}{@code <String>}. The supplier is invoked each time
  136.      * {@link #getDisplayName()} is called.
  137.      *
  138.      * @param   supplier      the {@code Supplier}
  139.      * @return                the new instance
  140.      * @since                 3.2-ALPHA-3
  141.      * @it.tidalwave.javadoc.experimental
  142.      **********************************************************************************************************************************************************/
  143.     @Nonnull
  144.     public static Displayable of (@Nonnull final Supplier<String> supplier)
  145.       {
  146.         return supplier::get;
  147.       }

  148.     /***********************************************************************************************************************************************************
  149.      * Creates an instance from a {@link Function}{@code <T, String>} and a generic object that the function is applied
  150.      * to. The function is invoked each time {@link #getDisplayName()} is called.
  151.      *
  152.      * @param   <T>           the type of the object
  153.      * @param   function      the {@code Function}
  154.      * @param   object        the object
  155.      * @return                the new instance
  156.      * @since                 3.2-ALPHA-3
  157.      * @it.tidalwave.javadoc.experimental
  158.      **********************************************************************************************************************************************************/
  159.     @Nonnull
  160.     public static <T> Displayable of (@Nonnull final Function<T, String> function, @Nonnull final T object)
  161.       {
  162.         return () -> function.apply(object);
  163.       }

  164.     /***********************************************************************************************************************************************************
  165.      * Creates a {@link Displayable} from a resource bundle. The bundle resource file is named {@code Bundle.properties} and it should be placed in the same
  166.      * package as the owner class.
  167.      * @param   ownerClass    the class that owns the bundle
  168.      * @param   key           the resource key
  169.      * @since                 2.0-ALPHA-2
  170.      * @return                the new instance
  171.      **********************************************************************************************************************************************************/
  172.     @Nonnull
  173.     public static Displayable fromBundle (@Nonnull final Class<?> ownerClass, @Nonnull final String key)
  174.       {
  175.         return new DefaultDisplayable(getMessage(ownerClass, key));
  176.       }

  177.     /***********************************************************************************************************************************************************
  178.      * Returns a {@link Comparator} for comparing two instances of {@code Displayable}.
  179.      *
  180.      * @return  the {@code Comparator}
  181.      * @since   3.2-ALPHA-6
  182.      **********************************************************************************************************************************************************/
  183.     @Nonnull
  184.     public static Comparator<Displayable> comparing()
  185.       {
  186.         return DisplayableComparator.getInstance();
  187.       }

  188.     /***********************************************************************************************************************************************************
  189.      * Returns a {@link Comparator} for comparing two instances of objects implementing {@code As} that contain the
  190.      * {@code Displayable} role.
  191.      *
  192.      * @return  the {@code Comparator}
  193.      * @since   3.2-ALPHA-6
  194.      **********************************************************************************************************************************************************/
  195.     @Nonnull
  196.     public static Comparator<As> asComparing()
  197.       {
  198.         return AsDisplayableComparator.getInstance();
  199.       }

  200.     /***********************************************************************************************************************************************************
  201.      * Renders the attached object into a {@link String}. The method accepts optional parameters that can be used to control the format of the rendering; they
  202.      * are usually specific of the object attached to this role.
  203.      * @param  args  optional rendering parameters
  204.      * @return       the string
  205.      **********************************************************************************************************************************************************/
  206.     @Nonnull
  207.     public default String render (@Nonnull final Object ... args)
  208.       {
  209.         return getDisplayName();
  210.       }

  211.     /***********************************************************************************************************************************************************
  212.      * Renders the attached object providing the string tu a {@link Consumer}. The method accepts optional parameters that can be used to control the format of
  213.      * the rendering; they are usually specific of the object attached to this role.
  214.      * @param  consumer        the {@code Consumer} to append to
  215.      * @param  args            optional rendering parameters
  216.      **********************************************************************************************************************************************************/
  217.     public default void renderTo (@Nonnull final Consumer<? super String> consumer, @Nonnull final Object ... args)
  218.       {
  219.         consumer.accept(render(args));
  220.       }
  221.   }