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

japicmp.util.Optional Maven / Gradle / Ivy

Go to download

japicmp is a library that computes the differences between two versions of a jar file/artifact in order to ease the API documentation for clients/customers.

The newest version!
package japicmp.util;

public abstract class Optional {
	public abstract boolean isPresent();

	public abstract T get();

	public abstract Optional or(Optional secondChoice);

	public abstract T or(T secondChoice);

	public abstract int hashCode();

	public abstract boolean equals(Object object);

	private static final class Present extends Optional {
		private final T reference;

		Present(T reference) {
			this.reference = reference;
		}

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

		@Override
		public T get() {
			return this.reference;
		}

		@Override
		public Optional or(Optional secondChoice) {
			return this;
		}

		@Override
		public T or(T secondChoice) {
			return this.reference;
		}

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

		@Override
		public boolean equals(Object object) {
			return object instanceof Present && this.reference.equals(((Present) object).reference);
		}

		@Override
		public String toString() {
			return this.reference.toString();
		}
	}

	private static final class Absent extends Optional {
		@SuppressWarnings("rawtypes")
		private static final Absent INSTANCE = new Absent();

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

		@Override
		public T get() {
			throw new IllegalStateException("value is absent.");
		}

		@Override
		@SuppressWarnings("unchecked")
		public Optional or(Optional secondChoice) {
			return (Optional) secondChoice;
		}

		@Override
		public T or(T secondChoice) {
			return secondChoice;
		}

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

		@Override
		public boolean equals(Object object) {
			return object == this;
		}

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

	public static  Optional of(T reference) {
		if (reference == null) {
			throw new IllegalArgumentException("reference should not be null.");
		}
		return new Present<>(reference);
	}

	public static  Optional fromNullable(T reference) {
		if (reference == null) {
			return new Absent<>();
		}
		return new Present<>(reference);
	}

	@SuppressWarnings("unchecked")
	public static  Optional absent() {
		return (Optional) Absent.INSTANCE;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy