Package io.atlassian.fugue
Interface Maybe<A>
-
- Type Parameters:
A- the contained type
- All Superinterfaces:
Effect.Applicant<A>,java.lang.Iterable<A>
- All Known Subinterfaces:
Either.Projection<A,B,L,R>
- All Known Implementing Classes:
Either.AbstractProjection,Either.LeftProjection,Either.RightProjection,Option,Option.None,Option.Some
public interface Maybe<A> extends java.lang.Iterable<A>, Effect.Applicant<A>
Implemented by things that may or may not contain a value.Note there are some methods suggested by this interface that cannot be expressed here as the return type cannot be expressed in Java's type system (due to the lack of higher-kinded types). These are for instance (where M is the implementing Maybe sub-type):
- <B> Maybe<B> map(Function<? super A, B>)
- <B> Maybe<B> flatMap(Function<? super A, Maybe<B>>
- Maybe<A> filter(Predicate<? super A>)
- Since:
- 1.0
-
-
Method Summary
All Methods Instance Methods Abstract Methods Deprecated Methods Modifier and Type Method Description booleancontains(A elem)Tests whether the option contains a given value as an element.booleanexists(java.util.function.Predicate<? super A> p)Whether this isis definedand applying the predicate to the contained value returns true.booleanforall(java.util.function.Predicate<? super A> p)Returnstrueif emptyor the result of the application of the given function to the value.Aget()Get the value if defined.AgetOr(java.util.function.Supplier<? extends A> supplier)Get the valueif definedor call the supplier and return its value if not.<B extends A>
AgetOrElse(B other)Get the value if defined, otherwise returnsother.AgetOrElse(java.util.function.Supplier<? extends A> supplier)Deprecated.since 3.0getOrElse(Supplier)is being replaced withgetOr(Supplier).AgetOrError(java.util.function.Supplier<java.lang.String> msg)Get the value or throws an error with the supplied message if not defined.AgetOrNull()Get the value if defined or null if not.<X extends java.lang.Throwable>
AgetOrThrow(java.util.function.Supplier<X> ifUndefined)Get the value or throws the supplied throwable if not defined.booleanisDefined()If the type contains a value return true.booleanisEmpty()If the type does not contain a value return true.java.util.Iterator<A>iterator()Return an iterator for this type.-
Methods inherited from interface io.atlassian.fugue.Effect.Applicant
foreach
-
-
-
-
Method Detail
-
get
A get()
Get the value if defined. Throw an exception otherwise.- Returns:
- the wrapped value
- Throws:
java.util.NoSuchElementException- if this is a none
-
getOrElse
<B extends A> A getOrElse(B other)
Get the value if defined, otherwise returnsother.- Type Parameters:
B- default value type- Parameters:
other- value to return if thisis empty- Returns:
- wrapped value if this
is defined, otherwise returnsother
-
getOr
A getOr(java.util.function.Supplier<? extends A> supplier)
Get the valueif definedor call the supplier and return its value if not. ReplacesgetOrElse(Supplier). Get the valueif definedor call the supplier and return its value if not.- Parameters:
supplier- called if thisis empty- Returns:
- the wrapped value or the value from the
Supplier
-
getOrElse
@Deprecated A getOrElse(java.util.function.Supplier<? extends A> supplier)
Deprecated.since 3.0getOrElse(Supplier)is being replaced withgetOr(Supplier). In Java 8 type inference cannot disambiguate between an overloaded method taking a generic A and the same method taking a Supplier<A>.Get the valueif definedor call the supplier and return its value if not.- Parameters:
supplier- called if thisis empty- Returns:
- the wrapped value or the value from the
Supplier
-
getOrNull
A getOrNull()
Get the value if defined or null if not.Although the use of null is discouraged, code written to use these must often interface with code that expects and returns nulls.
- Returns:
- the value or null if not defined
-
getOrError
A getOrError(java.util.function.Supplier<java.lang.String> msg)
Get the value or throws an error with the supplied message if not defined.Used when absolutely sure this
is defined.- Parameters:
msg- the message for the error.- Returns:
- the contained value.
-
getOrThrow
<X extends java.lang.Throwable> A getOrThrow(java.util.function.Supplier<X> ifUndefined) throws X extends java.lang.Throwable
Get the value or throws the supplied throwable if not defined.Used when absolutely sure this
is defined.- Type Parameters:
X- exception type- Parameters:
ifUndefined- the supplier of the throwable.- Returns:
- the contained value.
- Throws:
X- the throwable the supplier creates if there is no value.X extends java.lang.Throwable- Since:
- 2.0
-
isDefined
boolean isDefined()
If the type contains a value return true.- Returns:
trueif this holds a value,falseotherwise.
-
isEmpty
boolean isEmpty()
If the type does not contain a value return true.- Returns:
trueif this does not hold a value,falseotherwise.
-
exists
boolean exists(java.util.function.Predicate<? super A> p)
Whether this isis definedand applying the predicate to the contained value returns true.- Parameters:
p- the predicate to test, must not be null- Returns:
trueif defined and the predicate returns true for the contained value,falseotherwise.
-
iterator
java.util.Iterator<A> iterator()
Return an iterator for this type. In most cases this takes the form of an iterator returning zero or one values.- Specified by:
iteratorin interfacejava.lang.Iterable<A>- Returns:
- an iterator over the contained value
if defined, or an empty one otherwise.
-
forall
boolean forall(java.util.function.Predicate<? super A> p)
Returnstrueif emptyor the result of the application of the given function to the value.- Parameters:
p- The predicate function to test on the contained value, must not be null- Returns:
trueif no value or returns the result of the application of the given function to the value.
-
contains
boolean contains(A elem)
Tests whether the option contains a given value as an element.- Parameters:
elem- the element to test- Returns:
trueif the option has an element that is equal to elem,falseotherwise- Since:
- 6.1
-
-