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 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);
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);
public Option flatMap(Function> f) {
return bind(f);
}
public abstract boolean isSome();
public boolean isNone() {
return !isSome();
}
/** If this is none return none
else this. */
public Option orElse(Option none) {
return isSome() ? this : none;
}
/** Lazy version of {@link #orElse(Option)}. */
public Option orElse(Function0