Class Mappings


  • public final class Mappings
    extends Object
    Collection of various methods dealing with Mappings.
    • Method Detail

      • nullMapping

        public static <D,​R> Mapping<D,​R> nullMapping()
        Retrieves the null mapping, which maps each domain value to null.
        Type Parameters:
        D - domain class.
        R - range class.
        Returns:
        the null mapping.
      • identity

        public static <T> Mapping<T,​T> identity()
        Retrieves the identity mapping, which maps each domain value to itself.
        Type Parameters:
        T - domain/range class.
        Returns:
        the identity mapping.
      • toStringMapping

        public static <D> Mapping<D,​String> toStringMapping()
        Returns a mapping that maps objects to their String representation, as obtained by String.valueOf(Object).
        Returns:
        the "toString()" mapping
      • upcast

        public static <S,​T extends S> Mapping<T,​S> upcast()
        Returns a mapping that maps objects to a supertype representation.
        Returns:
        the "upcast" mapping
      • compose

        public static <D,​I,​R> Mapping<D,​R> compose​(Mapping<D,​? extends I> first,
                                                                     Mapping<? super I,​R> second)
        Retrieves the composition of two mappings, i.e., that mapping that results from applying the Mapping.get(Object) method consecutively.
        Type Parameters:
        D - domain class of the first (and resulting) mapping.
        I - intermediate object class, range class of the first and domain class of the second mapping.
        R - range class of the second (and resulting) mapping.
        Parameters:
        first - first mapping.
        second - second mapping.
        Returns:
        the composed mapping.
      • apply

        public static <D,​R> Collection<R> apply​(Mapping<? super D,​R> mapping,
                                                      Collection<? extends D> coll)
        Applies a mapping to a collection, resulting in a collection containing the result of applying the specified mapping to each element in the collection.

        Note that more specific properties of the specified collection won't be preserved: If the given collection is e.g. a set, and the provided mapping is not bijective, then the resulting collections may contain some values multiple times.

        Type Parameters:
        D - domain class.
        R - range class.
        Parameters:
        mapping - the mapping to apply.
        coll - the collection.
        Returns:
        the mapped collection.
      • apply

        public static <D,​R> Iterator<R> apply​(Mapping<? super D,​R> mapping,
                                                    Iterator<? extends D> baseIt)
        Applies a mapping to an iterator. For the behavior, see apply(Mapping, Iterable). The resulting iterator supports each operation which the underlying supports.
        Type Parameters:
        D - domain class.
        R - range class.
        Parameters:
        mapping - the mapping to apply.
        baseIt - the underlying iterator.
        Returns:
        the mapped iterator.
      • apply

        public static <D,​R> List<R> apply​(Mapping<? super D,​R> mapping,
                                                List<? extends D> list)
        Applies a mapping to a list, resulting in a list containing the result of applying the specified mapping to each element in the list.
        Parameters:
        mapping - the mapping to apply.
        list - the list.
        Returns:
        the mapped list.
      • apply

        public static <D,​R> Iterable<R> apply​(Mapping<? super D,​R> mapping,
                                                    Iterable<? extends D> it)
        Applies a mapping to an iterable. The result is an iterable whose iterator returns the results of applying the specified mapping to each of the elements returned by the original iterable.
        Type Parameters:
        D - domain class.
        R - range clas.
        Parameters:
        mapping - the mapping to apply.
        it - the underlying iterable.
        Returns:
        the mapped iterable.
      • idGet

        public static <D> D idGet​(Mapping<D,​D> mapping,
                                  D key)
      • safeGet

        public static <D,​R> R safeGet​(@Nullable Mapping<? super D,​? extends R> mapping,
                                            D key,
                                            R fallback)
        Safely retrieves a value from a mapping. If the mapping is null or returns a null value, the given fallback value is returned.
        Parameters:
        mapping - the mapping.
        key - the key.
        fallback - the fallback value to return if either the mapping or the originally returned value are null.
        Returns:
        the value returned by the specified mapping, or the fallback value.
      • fromMap

        public static <D,​R> Mapping<D,​R> fromMap​(Map<D,​R> map)