Class Narrow

java.lang.Object
com.atlassian.bamboo.util.Narrow

public class Narrow extends Object
Utility for narrowing object instances to Class types See to(Object, Class)
Since:
2.7
  • Method Summary

    Modifier and Type
    Method
    Description
    static <X, T extends X>
    T
    downTo(X o, @NotNull Class<T> aClass)
    Should be used for casting down the hierarchy of classes.
    static <X, T extends X>
    Iterable<T>
    iterableDownTo(@NotNull Iterable<X> iterable, @NotNull Class<T> aClass)
    Filters given Iterable returning only these objects which could be narrowed to given type.
    static <T> Iterable<T>
    iterableTo(@NotNull Iterable<?> iterable, @NotNull Class<T> aClass)
    Filters given Iterable returning only these objects which could be narrowed to given type
    static <R> Function<Object,R>
    reinterpret(@NotNull Class<R> aClass)
    Returns a Function that narrows supplied arguments.
    static <T> T
    reinterpret(@Nullable Object o, @NotNull Class<T> aClass)
    Narrows the given Object to the type of the Class.
    static <X, T extends X>
    Stream<T>
    streamDownTo(@NotNull Stream<X> stream, @NotNull Class<T> aClass)
    Filters given Stream returning only these objects which could be narrowed to given type.
    static <X, T> Stream<T>
    streamTo(@NotNull Stream<X> stream, @NotNull Class<T> aClass)
    Filters given Stream returning only these objects which could be narrowed to given type.
    static <T> T
    to(@Nullable Object o, @NotNull Class<T> aClass)
    Narrows the given Object to the type of the Class.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • reinterpret

      @Nullable public static <T> T reinterpret(@Nullable @Nullable Object o, @NotNull @NotNull Class<T> aClass)
      Narrows the given Object to the type of the Class. Using downTo(Object, Class) is always preferred over using this method. If object cannot be narrowed to the given type this method returns null This is a non-throwing form of Class.cast(Object)
      Type Parameters:
      T - the type to be narrowed to
      Parameters:
      o - the object to be narrowed. This object can be a null reference
      aClass - the class to narrow to
      Returns:
      narrowed object
    • reinterpret

      @NotNull public static <R> Function<Object,R> reinterpret(@NotNull @NotNull Class<R> aClass)
      Returns a Function that narrows supplied arguments.
    • to

      @Nullable public static <T> T to(@Nullable @Nullable Object o, @NotNull @NotNull Class<T> aClass)
      Narrows the given Object to the type of the Class. Using downTo(Object, Class) is always preferred over using this method. If object cannot be narrowed to the given type this method returns null This is a non-throwing form of Class.cast(Object)
      Type Parameters:
      T - the type to be narrowed to
      Parameters:
      o - the object to be narrowed. This object can be a null reference
      aClass - the class to narrow to
      Returns:
      narrowed object
    • downTo

      @Nullable public static <X, T extends X> T downTo(@Nullable X o, @NotNull @NotNull Class<T> aClass)
      Should be used for casting down the hierarchy of classes. See to(Object, Class).
    • iterableTo

      public static <T> Iterable<T> iterableTo(@NotNull @NotNull Iterable<?> iterable, @NotNull @NotNull Class<T> aClass)
      Filters given Iterable returning only these objects which could be narrowed to given type
      Type Parameters:
      T -
      Parameters:
      iterable - input iterable containing elements to filter
      aClass - class used to narrow elements of iterable
      Returns:
      filtered iterable with non-null elements narrowed to aClass
    • iterableDownTo

      public static <X, T extends X> Iterable<T> iterableDownTo(@NotNull @NotNull Iterable<X> iterable, @NotNull @NotNull Class<T> aClass)
      Filters given Iterable returning only these objects which could be narrowed to given type. This version is more typesafe that iterableTo(Iterable, Class) and should be used when casting down the class structure. (e.g. List<ImmutablePlan> -> Iterable<ImmutableChain>)
      Parameters:
      iterable - input iterable containing elements to filter
      aClass - class used to narrow elements of iterable
      Returns:
      filtered iterable with non-null elements narrowed to aClass
    • streamTo

      public static <X, T> Stream<T> streamTo(@NotNull @NotNull Stream<X> stream, @NotNull @NotNull Class<T> aClass)
      Filters given Stream returning only these objects which could be narrowed to given type.
      Parameters:
      stream - input iterable containing elements to filter
      aClass - class used to narrow elements of iterable
      Returns:
      filtered iterable with non-null elements narrowed to aClass
    • streamDownTo

      public static <X, T extends X> Stream<T> streamDownTo(@NotNull @NotNull Stream<X> stream, @NotNull @NotNull Class<T> aClass)
      Filters given Stream returning only these objects which could be narrowed to given type. This version is more typesafe that streamTo(Stream, Class) and should be used when casting down the class structure. (e.g. List<ImmutablePlan> -> Iterable<ImmutableChain>)
      Parameters:
      stream - input iterable containing elements to filter
      aClass - class used to narrow elements of iterable
      Returns:
      filtered iterable with non-null elements narrowed to aClass