Migrate common/util to use JDT annotations
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / OptionalBoolean.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.util;
9
10 import com.google.common.annotations.Beta;
11 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
12 import java.util.Optional;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
15
16 /**
17  * Utility class for storing an optional boolean in a single byte value. This cuts down the memory requirement quite
18  * at very small computational cost.
19  *
20  * <p>
21  * Note: fields do not have to be explicitly initialized, as default initialization value for 'byte', 0, is used to
22  *       represent 'absent' condition.
23  *
24  * @author Robert Varga
25  */
26 @Beta
27 public final class OptionalBoolean {
28     private static final byte ABSENT = 0;
29     private static final byte FALSE = 1;
30     private static final byte TRUE = 2;
31
32     private OptionalBoolean() {
33         throw new UnsupportedOperationException();
34     }
35
36     /**
37      * Check if a field value has been set, just like {@link Optional#isPresent()}.
38      *
39      * @param value field value
40      * @return True if the value is set.
41      * @throws IllegalArgumentException if value is invalid
42      */
43     public static boolean isPresent(final byte value) {
44         switch (value) {
45             case ABSENT:
46                 return false;
47             case FALSE:
48             case TRUE:
49                 return true;
50             default:
51                 throw invalidValue(value);
52         }
53     }
54
55     /**
56      * Decode boolean from a field value, just like {@link Optional#get()}.
57      *
58      * @param value Field value
59      * @return Decoded boolean.
60      * @throws IllegalArgumentException if value is invalid
61      * @throws IllegalStateException if value has not been set
62      */
63     public static boolean get(final byte value) {
64         switch (value) {
65             case ABSENT:
66                 throw new IllegalStateException("Field has not been initialized");
67             case FALSE:
68                 return false;
69             case TRUE:
70                 return true;
71             default:
72                 throw invalidValue(value);
73         }
74     }
75
76     /**
77      * Encode a boolean to a field value, just like {@link Optional#of(Object)}.
78      *
79      * @param bool Boolean value.
80      * @return Field value.
81      */
82     public static byte of(final boolean bool) {
83         return bool ? TRUE : FALSE;
84     }
85
86     /**
87      * Convert a nullable {@link Boolean} into a field value, just like {@link Optional#ofNullable(Object)}.
88      *
89      * @param bool Boolean value.
90      * @return Field value.
91      */
92     public static byte ofNullable(final @Nullable Boolean bool) {
93         return bool == null ? ABSENT : of(bool.booleanValue());
94     }
95
96     /**
97      * Convert a field value to a nullable {@link Boolean}. Similar to {@code Optional.orElse(null)}.
98      *
99      * @param value Field value.
100      * @return Nullable Boolean.
101      */
102     @SuppressFBWarnings("NP_BOOLEAN_RETURN_NULL")
103     public static @Nullable Boolean toNullable(final byte value) {
104         switch (value) {
105             case ABSENT:
106                 return null;
107             case FALSE:
108                 return Boolean.FALSE;
109             case TRUE:
110                 return Boolean.TRUE;
111             default:
112                 throw invalidValue(value);
113         }
114     }
115
116     /**
117      * Convert a field value into an {@link Optional} {@link Boolean}.
118      *
119      * @param value Field value.
120      * @return Optional {@link Boolean}.
121      * @throws IllegalArgumentException if value is invalid.
122      */
123     public static @NonNull Optional<Boolean> toOptional(final byte value) {
124         switch (value) {
125             case ABSENT:
126                 return Optional.empty();
127             case FALSE:
128                 return Optional.of(Boolean.FALSE);
129             case TRUE:
130                 return Optional.of(Boolean.TRUE);
131             default:
132                 throw invalidValue(value);
133         }
134     }
135
136     /**
137      * Convert a field value into a String representation.
138      *
139      * @param value Field value.
140      * @return Boolean-compatible string, or "absent".
141      * @throws IllegalArgumentException if value is invalid.
142      */
143     public static @NonNull String toString(final byte value) {
144         switch (value) {
145             case ABSENT:
146                 return "absent";
147             case FALSE:
148                 return Boolean.toString(false);
149             case TRUE:
150                 return Boolean.toString(true);
151             default:
152                 throw invalidValue(value);
153         }
154     }
155
156     private static IllegalArgumentException invalidValue(final byte value) {
157         throw new IllegalArgumentException("Invalid field value " + value);
158     }
159 }