]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/java/src/org/apache/thrift/Option.java
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / java / src / org / apache / thrift / Option.java
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 @SuppressWarnings("rawtypes")
28 private static final Option NONE = new None();
29
30 /**
31 * Whether the Option is defined or not
32 * @return
33 * true if the Option is defined (of type Some)
34 * false if the Option is not defined (of type None)
35 */
36 public abstract boolean isDefined();
37
38 /**
39 * Get the value of the Option (if it is defined)
40 * @return the value
41 * @throws IllegalStateException if called on a None
42 */
43 public abstract T get();
44
45 /**
46 * Get the contained value (if defined) or else return a default value
47 * @param other what to return if the value is not defined (a None)
48 * @return either the value, or other if the value is not defined
49 */
50 public T or(T other) {
51 if (isDefined()) {
52 return get();
53 } else {
54 return other;
55 }
56 }
57 /**
58 * The None type, representing an absent value (instead of "null")
59 */
60 public static class None<T> extends Option<T> {
61 public boolean isDefined() {
62 return false;
63 }
64
65 public T get() {
66 throw new IllegalStateException("Cannot call get() on None");
67 }
68
69 public String toString() {
70 return "None";
71 }
72 }
73
74 /**
75 * The Some type, representing an existence of some value
76 * @param <T> The type of value
77 */
78 public static class Some<T> extends Option<T> {
79 private final T value;
80 public Some(T value) {
81 this.value = value;
82 }
83
84 public boolean isDefined() {
85 return true;
86 }
87
88 public T get() {
89 return value;
90 }
91
92 public String toString() {
93 return "Some(" + value + ")";
94 }
95 }
96
97 /**
98 * Wraps value in an Option type, depending on whether or not value is null
99 * @param value
100 * @param <T> type of value
101 * @return Some(value) if value is not null, None if value is null
102 */
103 public static <T> Option<T> fromNullable(T value) {
104 if (value != null) {
105 return some(value);
106 } else {
107 return none();
108 }
109 }
110
111 /**
112 * Wrap value in a Some type (NB! value must not be null!)
113 * @param value
114 * @param <T> type of value
115 * @return a new Some(value)
116 */
117 public static <T> Some<T> some(T value) {
118 return new Some<T>(value);
119 }
120
121 @SuppressWarnings("unchecked")
122 public static <T> None<T> none() {
123 return (None<T>) NONE;
124 }
125 }