All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.role.ui.Displayable Maven / Gradle / Ivy

Go to download

Roles are a powerful way for designing complex behaviours while keeping good practices such as Single Responsibility, Dependency Inversion and Interface Segregation.

The newest version!
/*
 * *********************************************************************************************************************
 *
 * TheseFoolishThings: Miscellaneous utilities
 * http://tidalwave.it/projects/thesefoolishthings
 *
 * Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
 *
 * *********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * *********************************************************************************************************************
 *
 * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
 * git clone https://github.com/tidalwave-it/thesefoolishthings-src
 *
 * *********************************************************************************************************************
 */
package it.tidalwave.role.ui;

import javax.annotation.Nonnull;
import java.util.Comparator;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import it.tidalwave.util.As;
import it.tidalwave.role.ui.impl.AsDisplayableComparator;
import it.tidalwave.role.ui.impl.DefaultDisplayable;
import it.tidalwave.role.ui.impl.DisplayableComparator;
import static it.tidalwave.util.BundleUtilities.getMessage;

/***********************************************************************************************************************
 *
 * The role of an object which can provide its own display name.
 *
 * @stereotype Role
 *
 * @author  Fabrizio Giudici
 * @it.tidalwave.javadoc.stable
 *
 **********************************************************************************************************************/
@FunctionalInterface
public interface Displayable
  {
    public static final Class _Displayable_ = Displayable.class;

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

    /*******************************************************************************************************************
     *
     * Returns the display name in the current {@link java.util.Locale}.
     *
     * @return  the display name
     *
     ******************************************************************************************************************/
    @Nonnull
    public String getDisplayName();

    /*******************************************************************************************************************
     *
     * Sends the display name in the current {@link java.util.Locale} to a given customer.
     *
     * @param     consumer    the {@code Consumer}
     * @since     3.2-ALPHA-15
     *
     ******************************************************************************************************************/
    @SuppressWarnings("BoundedWildcard")
    public default void display (@Nonnull final Consumer consumer)
      {
        consumer.accept(getDisplayName());
      }

    /*******************************************************************************************************************
     *
     * Creates an instance with a given display name.
     *
     * @param  displayName    the display name
     * @return                the new instance
     * @since                 3.2-ALPHA-1 (was {@code DefaultDisplayable}
     *
     ******************************************************************************************************************/
    @Nonnull
    public static Displayable of (@Nonnull final String displayName)
      {
        return of(displayName, "???");
      }

    /*******************************************************************************************************************
     *
     * Creates an instance with a given display name iand an explicit label for  {@code toString()}.
     *
     * @param  displayName    the display name
     * @param  toStringName   the name to be rendered when {@code toString()} is called
     * @return                the new instance
     * @since                 3.2-ALPHA-1 (was {@code DefaultDisplayable}
     *
     ******************************************************************************************************************/
    @Nonnull
    public static Displayable of (@Nonnull final String displayName, @Nonnull final String toStringName)
      {
        return new DefaultDisplayable(displayName, toStringName);
      }

    /*******************************************************************************************************************
     *
     * Creates an instance from a {@link Supplier}{@code }. The supplier is invoked each time
     * {@link #getDisplayName()} is called.
     *
     * @param   supplier      the {@code Supplier}
     * @return                the new instance
     * @since                 3.2-ALPHA-3
     * @it.tidalwave.javadoc.experimental
     *
     ******************************************************************************************************************/
    @Nonnull
    public static Displayable of (@Nonnull final Supplier supplier)
      {
        return supplier::get;
      }

    /*******************************************************************************************************************
     *
     * Creates an instance from a {@link Function}{@code } and a generic object that the function is applied
     * to. The function is invoked each time {@link #getDisplayName()} is called.
     *
     * @param              the type of the object
     * @param   function      the {@code Function}
     * @param   object        the object
     * @return                the new instance
     * @since                 3.2-ALPHA-3
     * @it.tidalwave.javadoc.experimental
     *
     ******************************************************************************************************************/
    @Nonnull
    public static  Displayable of (@Nonnull final Function function, @Nonnull final T object)
      {
        return () -> function.apply(object);
      }

    /*******************************************************************************************************************
     *
     * Creates a {@link LocalizedDisplayable} from a resource bundle. The bundle resource file is named
     * {@code Bundle.properties} and it should be placed in the same package as the owner class.
     *
     * @param   ownerClass    the class that owns the bundle
     * @param   key           the resource key
     * @return                the new instance
     * @since                 3.2-ALPHA-1 (was previously in {@code Displayable8}
     *
     ******************************************************************************************************************/
    @Nonnull
    public static LocalizedDisplayable fromBundle (@Nonnull final Class ownerClass, @Nonnull final String key)
      {
        return new DefaultDisplayable(getMessage(ownerClass, key));
      }

    /*******************************************************************************************************************
     *
     * Returns a {@link Comparator} for comparing two instances of {@code Displayable}.
     *
     * @return  the {@code Comparator}
     * @since   3.2-ALPHA-6
     *
     ******************************************************************************************************************/
    @Nonnull
    public static Comparator comparing()
      {
        return DisplayableComparator.getInstance();
      }

    /*******************************************************************************************************************
     *
     * Returns a {@link Comparator} for comparing two instances of objects implementing {@code As} that contain the
     * {@code Displayable} role.
     *
     * @return  the {@code Comparator}
     * @since   3.2-ALPHA-6
     *
     ******************************************************************************************************************/
    @Nonnull
    public static Comparator asComparing()
      {
        return AsDisplayableComparator.getInstance();
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy