1+ /*
2+ * Licensed to the Apache Software Foundation (ASF) under one
3+ * or more contributor license agreements. See the NOTICE file
4+ * distributed with this work for additional information
5+ * regarding copyright ownership. The ASF licenses this file
6+ * to you under the Apache License, Version 2.0 (the
7+ * "License"); you may not use this file except in compliance
8+ * with the License. You may obtain a copy of the License at
9+ *
10+ * http://www.apache.org/licenses/LICENSE-2.0
11+ *
12+ * Unless required by applicable law or agreed to in writing,
13+ * software distributed under the License is distributed on an
14+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+ * KIND, either express or implied. See the License for the
16+ * specific language governing permissions and limitations
17+ * under the License.
18+ */
19+
20+ package org .apache .thrift ;
21+
22+ /**
23+ * Implementation of the Option type pattern
24+ */
25+ public abstract class Option <T > {
26+
27+ /**
28+ * Whether the Option is defined or not
29+ * @return
30+ * true if the Option is defined (of type Some)
31+ * false if the Option is not defined (of type None)
32+ */
33+ public abstract boolean isDefined ();
34+
35+ /**
36+ * Get the value of the Option (if it is defined)
37+ * @return the value
38+ * @throws IllegalStateException if called on a None
39+ */
40+ public abstract T get ();
41+
42+ /**
43+ * Get the contained value (if defined) or else return a default value
44+ * @param other what to return if the value is not defined (a None)
45+ * @return either the value, or other if the value is not defined
46+ */
47+ public T or (T other ) {
48+ if (isDefined ()) {
49+ return get ();
50+ } else {
51+ return other ;
52+ }
53+ }
54+ /**
55+ * The None type, representing an absent value (instead of "null")
56+ */
57+ public static class None <T > extends Option <T > {
58+ public boolean isDefined () {
59+ return false ;
60+ }
61+
62+ public T get () {
63+ throw new IllegalStateException ("Cannot call get() on None" );
64+ }
65+
66+ public String toString () {
67+ return "None" ;
68+ }
69+ }
70+
71+ /**
72+ * The Some type, representing an existence of some value
73+ * @param <T> The type of value
74+ */
75+ public static class Some <T > extends Option <T > {
76+ private final T value ;
77+ public Some (T value ) {
78+ this .value = value ;
79+ }
80+
81+ public boolean isDefined () {
82+ return true ;
83+ }
84+
85+ public T get () {
86+ return value ;
87+ }
88+
89+ public String toString () {
90+ return "Some(" +value .toString ()+")" ;
91+ }
92+ }
93+
94+ /**
95+ * Wraps value in an Option type, depending on whether or not value is null
96+ * @param value
97+ * @param <T> type of value
98+ * @return Some(value) if value is not null, None if value is null
99+ */
100+ public static <T > Option <T > fromNullable (T value ) {
101+ if (value != null ) {
102+ return new Some <T >(value );
103+ } else {
104+ return new None <T >();
105+ }
106+ }
107+
108+ /**
109+ * Wrap value in a Some type (NB! value must not be null!)
110+ * @param value
111+ * @param <T> type of value
112+ * @return a new Some(value)
113+ */
114+ public static <T > Some <T > some (T value ) {
115+ return new Some <T >(value );
116+ }
117+
118+ public static <T > None <T > none () {
119+ return new None <T >();
120+ }
121+ }
0 commit comments