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 final String valueStr; 039 private final int valueHash; 040 041 private Optional(final T value) { 042 this.value = value; 043 this.valueStr = "Optional:{" + value + '}'; 044 this.valueHash = value.hashCode(); 045 } 046 047 private Optional() { 048 this.value = null; 049 this.valueStr = "OptionalvNothing"; 050 this.valueHash = 0; 051 } 052 053 /** 054 * @return An Optional with the given value. 055 * @throws IllegalArgumentException if the value is null 056 */ 057 public static <T> Optional<T> of(final T value) throws IllegalArgumentException { 058 if (null == value) { 059 throw new IllegalArgumentException("Passed a null value. Use ofNullable() instead"); 060 } 061 return new Optional<>(value); 062 } 063 064 /** 065 * @return an Optional with no value. 066 */ 067 public static <T> Optional<T> empty() { 068 return new Optional<>(); 069 } 070 071 /** 072 * @return An optional representing the given value, or an empty Optional. 073 */ 074 public static <T> Optional<T> ofNullable(final T value) { 075 if (null == value) { 076 return Optional.empty(); 077 } else { 078 return Optional.of(value); 079 } 080 } 081 082 /** 083 * @return the value represented or null, if isPresent() is false. 084 */ 085 public T get() { 086 return this.value; 087 } 088 089 /** 090 * @param other 091 * @return the value of this Optional or other, if no value exists. 092 */ 093 public T orElse(final T other) { 094 if (isPresent()) { 095 return this.get(); 096 } else { 097 return other; 098 } 099 } 100 101 /** 102 * @return true if there is a value, false otherwise. 103 */ 104 public boolean isPresent() { 105 return null != this.value; 106 } 107 108 @Override 109 public boolean equals(final Object obj) { 110 111 if (this == obj) { 112 return true; 113 } 114 115 if (obj == null || getClass() != obj.getClass()) { 116 return false; 117 } 118 119 final Optional that = (Optional) obj; 120 return this.value == that.value || this.value != null && this.value.equals(that.value); 121 } 122 123 @Override 124 public int hashCode() { 125 return this.valueHash; 126 } 127 128 @Override 129 public String toString() { 130 return this.valueStr; 131 } 132}