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

  27. import javax.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.role.ui.impl.AsDisplayableComparator;
  34. import it.tidalwave.role.ui.impl.DefaultDisplayable;
  35. import it.tidalwave.role.ui.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.  * @author  Fabrizio Giudici
  44.  * @it.tidalwave.javadoc.stable
  45.  *
  46.  **************************************************************************************************************************************************************/
  47. @FunctionalInterface
  48. public interface Displayable
  49.   {
  50.     public static final Class<Displayable> _Displayable_ = Displayable.class;

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

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

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

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

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

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

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

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

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

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