All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jodd.util.function.Maybe Maven / Gradle / Ivy

There is a newer version: 5.1.0-20190624
Show newest version
// Copyright (c) 2003-present, Jodd Team (http://jodd.org)
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

package jodd.util.function;

import java.util.Collections;
import java.util.Iterator;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * Slightly better Maybe monad. It does not have a {@code get()}, but it has {@code or()} :).
 * Slightly better implementation of Optional, as there are no IF blocks all over the class.
 */
public interface Maybe extends Iterable {

	/**
	 * Static factory for Maybe.
	 */
	static  Maybe of(final T value) {
		if (value == null) {
			return nothing();
		}
		return just(value);
	}

	@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
	static  Maybe of(final Optional optionalValue) {
		return optionalValue.map(Maybe::of).orElseGet(Maybe::nothing);
	}

	/**
	 * Returns {@code true} if value is present.
	 */
	boolean isJust();

	/**
	 * Returns {@code false} if value is not present.
	 */
	boolean isNothing();

	/**
	 * Takes a default value and a function. If the Maybe value is Nothing, the function returns the default value.
	 * Otherwise, it applies the function to the value inside the Just and returns the result.
	 */
	 V maybe(V defaultValue, Function function);

	/**
	 * Consumes value if present.
	 */
	void consumeJust(Consumer consumer);

	/**
	 * Returns empty or single-element stream.
	 */
	Stream stream();

	/**
	 * Use a maybe of given value if this one is NOTHING.
	 */
	Maybe or(T otherValue);

	/**
	 * Use give maybe if this one is NOTHING.
	 */
	Maybe or(Maybe maybe);

	/**
	 * Use supplied value if this one is NOTHING.
	 */
	Maybe or​(Supplier> supplier);

	/**
	 * Map.
	 */
	 Maybe map(Function mapper);

	/**
	 * Flat map.
	 */
	 Maybe flatMap(Function> mapper);

	Maybe filter(Predicate mapping);

	/**
	 * Converts Maybe to the Java Optional.
	 */
	Optional optional();

	/**
	 * Returns a NOTHING.
	 */
	static  Maybe nothing() {
		return new Maybe() {
			@Override
			public boolean isJust() {
				return false;
			}

			@Override
			public boolean isNothing() {
				return true;
			}

			@Override
			public  V maybe(final V defaultValue, final Function function) {
				return defaultValue;
			}

			@Override
			public void consumeJust(final Consumer consumer) {
			}

			@Override
			public Iterator iterator() {
				return Collections.emptyList().iterator();
			}

			@Override
			public Stream stream() {
				return Stream.empty();
			}

			@Override
			public Maybe or(final T otherValue) {
				return Maybe.of(otherValue);
			}

			@Override
			public Maybe or(final Maybe maybeJust) {
				return maybeJust;
			}

			@SuppressWarnings("unchecked")
			@Override
			public Maybe or​(final Supplier> supplier) {
				final Maybe r = (Maybe) supplier.get();
				return Objects.requireNonNull(r);
			}

			@Override
			public  Maybe map(final Function mapper) {
				return nothing();
			}

			@Override
			public  Maybe flatMap(final Function> mapper) {
				return nothing();
			}

			@Override
			public Maybe filter(final Predicate mapping) {
				return nothing();
			}

			@Override
			public String toString() {
				return "nothing";
			}

			@Override
			public boolean equals(final Object obj) {
				return false;
			}

			@Override
			public int hashCode() {
				return 0;
			}

			@Override
			public Optional optional() {
				return Optional.empty();
			}
		};
	}

	/**
	 * Returns a JUST>
	 */
	static  Maybe just(final T theValue) {
		return new Just<>(theValue);
	}

	class Just implements Maybe {
		private final T value;

		private Just(final T value) {
			Objects.requireNonNull(value);
			this.value = value;
		}

		@Override
		public boolean isJust() {
			return true;
		}

		@Override
		public boolean isNothing() {
			return false;
		}

		@Override
		public  V maybe(final V defaultValue, final Function function) {
			return function.apply(value);
		}

		@Override
		public void consumeJust(final Consumer consumer) {
			consumer.accept(value);
		}

		@Override
		public Iterator iterator() {
			return Collections.singleton(value).iterator();
		}

		@Override
		public Stream stream() {
			return Stream.of(value);
		}

		@Override
		public Maybe or(final T otherValue) {
			return this;
		}

		@Override
		public Maybe or(final Maybe maybeDefaultValue) {
			return this;
		}

		@Override
		public Maybe or​(final Supplier> supplier) {
			return this;
		}

		@Override
		public  Maybe map(final Function mapper) {
			return Maybe.of(mapper.apply(value));
		}

		@Override
		public  Maybe flatMap(final Function> mapper) {
			return mapper.apply(value);
		}

		@Override
		public Maybe filter(final Predicate predicate) {
			return predicate.test(value) ? this : nothing();
		}

		@Override
		public String toString() {
			return "just: " + value.toString();
		}

		@Override
		public boolean equals(final Object o) {
			if (this == o) return true;
			if (o == null || getClass() != o.getClass()) return false;

			final Just that = (Just) o;

			return value.equals(that.value);

		}

		@Override
		public int hashCode() {
			return value.hashCode();
		}

		@Override
		public Optional optional() {
			return Optional.of(value);
		}
	}
}