Skip to content

Package: Optional

Optional

nameinstructionbranchcomplexitylinemethod
Optional()
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
Optional(Object)
M: 5 C: 8
62%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 4
80%
M: 0 C: 1
100%
empty()
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
fromJavaOptional(Optional)
M: 0 C: 9
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
get()
M: 5 C: 6
55%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 1 C: 2
67%
M: 0 C: 1
100%
isPresent()
M: 0 C: 7
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
of(Object)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
ofNullable(Object)
M: 0 C: 7
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
orNull()
M: 8 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Eugen Neufeld - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.common;
15:
16: import java.util.NoSuchElementException;
17:
18: /**
19: * A container object that may or may not contain a value.
20: *
21: * @param <T> Type of the value
22: * @author Eugen Neufeld
23: * @since 1.8
24: */
25: public final class Optional<T> {
26:
27:         private static final Optional<?> EMPTY = new Optional<Object>();
28:
29:         /**
30:          * Returns an empty Optional instance. No value is present for this Optional.
31:          *
32:          * @param <T> Type of the non-existent value
33:          * @return an empty Optional
34:          */
35:         public static <T> Optional<T> empty() {
36:                 @SuppressWarnings("unchecked")
37:                 final Optional<T> t = (Optional<T>) EMPTY;
38:                 return t;
39:         }
40:
41:         /**
42:          * Returns an Optional with the specified present non-null value.
43:          *
44:          * @param <T> the class of the value
45:          * @param value the value to be present, which must be non-null
46:          * @return an Optional with the value present
47:          */
48:         public static <T> Optional<T> of(T value) {
49:                 return new Optional<T>(value);
50:         }
51:
52:         /**
53:          * Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional.
54:          *
55:          * @param <T> the class of the value
56:          * @param value the possibly-null value to describe
57:          * @return an Optional with a present value if the specified value is non-null, otherwise an empty Optional
58:          */
59:         public static <T> Optional<T> ofNullable(T value) {
60:•                if (value == null) {
61:                         return empty();
62:                 }
63:                 return of(value);
64:         }
65:
66:         /**
67:          * Creates a new Optional from the given java.util.Optional. If the java Optional is empty, an empty Optional is
68:          * returned. Otherwise, the value is re-wrapped in this Optional.
69:          *
70:          * @param <T> the class of the value
71:          * @param javaOptional The java.util.Optional to convert to an EMF Forms Optional
72:          * @return An EMF Forms Optional equivalent to the given Java Optional
73:          * @since 1.20
74:          */
75:         public static <T> Optional<T> fromJavaOptional(java.util.Optional<T> javaOptional) {
76:•                if (javaOptional.isPresent()) {
77:                         return of(javaOptional.get());
78:                 }
79:                 return empty();
80:         }
81:
82:         private final T value;
83:
84:         private Optional() {
85:                 value = null;
86:         }
87:
88:         private Optional(T value) {
89:•                if (value == null) {
90:                         throw new NullPointerException("Value must not be null!"); //$NON-NLS-1$
91:                 }
92:                 this.value = value;
93:         }
94:
95:         /**
96:          * Return true if there is a value present, otherwise false.
97:          *
98:          * @return true if there is a value present, otherwise false
99:          */
100:         public boolean isPresent() {
101:•                return value != null;
102:         }
103:
104:         /**
105:          * If a value is present in this Optional, returns the value, otherwise throws NoSuchElementException.
106:          *
107:          * @return the non-null value held by this Optional
108:          * @see #isPresent()
109:          */
110:         public T get() {
111:•                if (value == null) {
112:                         throw new NoSuchElementException("No value present!"); //$NON-NLS-1$
113:                 }
114:                 return value;
115:         }
116:
117:         /**
118:          * Returns the wrapped value, if present, otherwise {@code null}.
119:          *
120:          * @return the wrapped value, if present, otherwise {@code null}
121:          *
122:          * @since 1.13
123:          */
124:         public Object orNull() {
125:•                if (isPresent()) {
126:                         return get();
127:                 }
128:                 return null;
129:         }
130: }