org.opencastproject.util.data.Option Maven / Gradle / Ivy
/**
* Licensed to The Apereo Foundation under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
*
* The Apereo Foundation licenses this file to you under the Educational
* Community 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://opensource.org/licenses/ecl2.txt
*
* 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.
*
*/
package org.opencastproject.util.data;
import static org.opencastproject.util.data.Tuple.tuple;
import static org.opencastproject.util.data.functions.Misc.chuck;
import com.entwinemedia.fn.data.Opt;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* The option type encapsulates on optional value. It contains either some value or is empty. Please make sure to NEVER
* wrap null into a some. Instead use none.
*/
// todo clean up the mix of abstract methods and concrete implementations based on the isSome() decision
public abstract class Option implements Iterable {
private Option() {
}
/** Safe decomposition of the option type. */
public abstract B fold(Match visitor);
/** Safe decomposition of the option type using functions. */
public B fold(Function some, Function0 none) {
return isSome() ? some.apply(get()) : none.apply();
}
public abstract Option foreach(Function super A, Void> f);
public abstract Option fmap(Function super A, ? extends B> f);
public Option map(Function super A, ? extends B> f) {
return fmap(f);
}
/** Monadic bind operation m a -> (a -> m b) -> m b
. */
public abstract Option bind(Function> f);
/** @see org.opencastproject.util.data.functions.Functions#bind(Function) */
public Option flatMap(Function> f) {
return bind(f);
}
public abstract boolean isSome();
public boolean isNone() {
return !isSome();
}
/** If this is some return some
. Like {@link #bind(Function)} but ignores the option's content. */
public Option andThen(Option some) {
return isSome() ? some : Option. none();
}
/** If this is some return some
. Like {@link #map(Function)} but ignores the option's content. */
public Option andThenV(B some) {
return isSome() ? some(some) : Option. none();
}
/** Lazy version of {@link #andThen(Option)}. */
public Option andThen(Function0
© 2015 - 2025 Weber Informatics LLC | Privacy Policy