Package com.atlassian.bamboo.util
Class Narrow
- java.lang.Object
-
- com.atlassian.bamboo.util.Narrow
-
public class Narrow extends Object
Utility for narrowing object instances toClass
types Seeto(Object, Class)
- Since:
- 2.7
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <X,T extends X>
TdownTo(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 typestatic <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)
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)
-
-
-
Method Detail
-
reinterpret
@Nullable public static <T> T reinterpret(@Nullable @Nullable Object o, @NotNull @NotNull Class<T> aClass)
Narrows the givenObject
to the type of theClass
. UsingdownTo(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 ofClass.cast(Object)
- Type Parameters:
T
- the type to be narrowed to- Parameters:
o
- the object to be narrowed. This object can be a null referenceaClass
- 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 givenObject
to the type of theClass
. UsingdownTo(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 ofClass.cast(Object)
- Type Parameters:
T
- the type to be narrowed to- Parameters:
o
- the object to be narrowed. This object can be a null referenceaClass
- 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. Seeto(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 filteraClass
- 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 thatiterableTo(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 filteraClass
- 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 filteraClass
- 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 thatstreamTo(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 filteraClass
- class used to narrow elements of iterable- Returns:
- filtered iterable with non-null elements narrowed to aClass
-
-