]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/java/src/org/apache/thrift/Option.java
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
20 package org
.apache
.thrift
;
23 * Implementation of the Option type pattern
25 public abstract class Option
<T
> {
27 @SuppressWarnings("rawtypes")
28 private static final Option NONE
= new None();
31 * Whether the Option is defined or not
33 * true if the Option is defined (of type Some)
34 * false if the Option is not defined (of type None)
36 public abstract boolean isDefined();
39 * Get the value of the Option (if it is defined)
41 * @throws IllegalStateException if called on a None
43 public abstract T
get();
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
50 public T
or(T other
) {
58 * The None type, representing an absent value (instead of "null")
60 public static class None
<T
> extends Option
<T
> {
61 public boolean isDefined() {
66 throw new IllegalStateException("Cannot call get() on None");
69 public String
toString() {
75 * The Some type, representing an existence of some value
76 * @param <T> The type of value
78 public static class Some
<T
> extends Option
<T
> {
79 private final T value
;
80 public Some(T value
) {
84 public boolean isDefined() {
92 public String
toString() {
93 return "Some(" + value
+ ")";
98 * Wraps value in an Option type, depending on whether or not value is null
100 * @param <T> type of value
101 * @return Some(value) if value is not null, None if value is null
103 public static <T
> Option
<T
> fromNullable(T value
) {
112 * Wrap value in a Some type (NB! value must not be null!)
114 * @param <T> type of value
115 * @return a new Some(value)
117 public static <T
> Some
<T
> some(T value
) {
118 return new Some
<T
>(value
);
121 @SuppressWarnings("unchecked")
122 public static <T
> None
<T
> none() {
123 return (None
<T
>) NONE
;