001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.util; 020 021import net.jcip.annotations.Immutable; 022import net.jcip.annotations.ThreadSafe; 023 024import java.io.Serializable; 025 026/** 027 * Represents an optional value. Loosely based on 028 * <a href="http://download.java.net/jdk8/docs/api/java/util/Optional.html">The Java 8 version</a>, but filtered for 029 * Java 7 compatibility. 030 */ 031@Immutable 032@ThreadSafe 033public final class Optional<T> implements Serializable { 034 035 private static final long serialVersionUID = 42L; 036 037 private final T value; 038 private static final Optional<?> EMPTY = new Optional<>(); 039 private static final String EMPTY_VALUE_STR = "Optional.empty"; 040 041 private Optional(final T value) { 042 this.value = value; 043 } 044 045 private Optional() { 046 this.value = null; 047 } 048 049 /** 050 * @return An Optional with the given value. 051 * @throws IllegalArgumentException if the value is null 052 */ 053 public static <T> Optional<T> of(final T value) throws IllegalArgumentException { 054 if (null == value) { 055 throw new IllegalArgumentException("Passed a null value. Use ofNullable() instead"); 056 } 057 return new Optional<>(value); 058 } 059 060 /** 061 * @return an Optional with no value. 062 */ 063 public static <T> Optional<T> empty() { 064 return (Optional<T>) EMPTY; 065 } 066 067 /** 068 * @return An optional representing the given value, or an empty Optional. 069 */ 070 public static <T> Optional<T> ofNullable(final T value) { 071 if (null == value) { 072 return Optional.empty(); 073 } else { 074 return Optional.of(value); 075 } 076 } 077 078 /** 079 * @return the value represented or null, if isPresent() is false. 080 */ 081 public T get() { 082 return this.value; 083 } 084 085 /** 086 * @param other 087 * @return the value of this Optional or other, if no value exists. 088 */ 089 public T orElse(final T other) { 090 if (isPresent()) { 091 return this.get(); 092 } else { 093 return other; 094 } 095 } 096 097 /** 098 * @return true if there is a value, false otherwise. 099 */ 100 public boolean isPresent() { 101 return null != this.value; 102 } 103 104 @Override 105 public boolean equals(final Object obj) { 106 107 if (this == obj) { 108 return true; 109 } 110 111 if (obj == null || getClass() != obj.getClass()) { 112 return false; 113 } 114 115 final Optional that = (Optional) obj; 116 return this.value == that.value || this.value != null && this.value.equals(that.value); 117 } 118 119 @Override 120 public int hashCode() { 121 return this.value == null ? 0 : this.value.hashCode(); 122 } 123 124 @Override 125 public String toString() { 126 return this.value == null ? EMPTY_VALUE_STR : "Optional:{" + this.value + "}"; 127 } 128}