2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.util;
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;
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.
21 * Note: fields do not have to be explicitly initialized, as default initialization value for 'byte', 0, is used to
22 * represent 'absent' condition.
24 * @author Robert Varga
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;
32 private OptionalBoolean() {
33 throw new UnsupportedOperationException();
37 * Check if a field value has been set, just like {@link Optional#isPresent()}.
39 * @param value field value
40 * @return True if the value is set.
41 * @throws IllegalArgumentException if value is invalid
43 public static boolean isPresent(final byte value) {
51 throw invalidValue(value);
56 * Decode boolean from a field value, just like {@link Optional#get()}.
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
63 public static boolean get(final byte value) {
66 throw new IllegalStateException("Field has not been initialized");
72 throw invalidValue(value);
77 * Encode a boolean to a field value, just like {@link Optional#of(Object)}.
79 * @param bool Boolean value.
80 * @return Field value.
82 public static byte of(final boolean bool) {
83 return bool ? TRUE : FALSE;
87 * Convert a nullable {@link Boolean} into a field value, just like {@link Optional#ofNullable(Object)}.
89 * @param bool Boolean value.
90 * @return Field value.
92 public static byte ofNullable(final @Nullable Boolean bool) {
93 return bool == null ? ABSENT : of(bool.booleanValue());
97 * Convert a field value to a nullable {@link Boolean}. Similar to {@code Optional.orElse(null)}.
99 * @param value Field value.
100 * @return Nullable Boolean.
102 @SuppressFBWarnings("NP_BOOLEAN_RETURN_NULL")
103 public static @Nullable Boolean toNullable(final byte value) {
108 return Boolean.FALSE;
112 throw invalidValue(value);
117 * Convert a field value into an {@link Optional} {@link Boolean}.
119 * @param value Field value.
120 * @return Optional {@link Boolean}.
121 * @throws IllegalArgumentException if value is invalid.
123 public static @NonNull Optional<Boolean> toOptional(final byte value) {
126 return Optional.empty();
128 return Optional.of(Boolean.FALSE);
130 return Optional.of(Boolean.TRUE);
132 throw invalidValue(value);
137 * Convert a field value into a String representation.
139 * @param value Field value.
140 * @return Boolean-compatible string, or "absent".
141 * @throws IllegalArgumentException if value is invalid.
143 public static @NonNull String toString(final byte value) {
148 return Boolean.toString(false);
150 return Boolean.toString(true);
152 throw invalidValue(value);
156 private static IllegalArgumentException invalidValue(final byte value) {
157 throw new IllegalArgumentException("Invalid field value " + value);