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.Nullable;
16 * Utility class for storing an optional boolean in a single byte value. This cuts down the memory requirement quite
17 * at very small computational cost.
20 * Note: fields do not have to be explicitly initialized, as default initialization value for 'byte', 0, is used to
21 * represent 'absent' condition.
23 * @author Robert Varga
26 public final class OptionalBoolean {
27 private static final byte ABSENT = 0;
28 private static final byte FALSE = 1;
29 private static final byte TRUE = 2;
31 private OptionalBoolean() {
32 throw new UnsupportedOperationException();
36 * Check if a field value has been set, just like {@link Optional#isPresent()}.
38 * @param value field value
39 * @return True if the value is set.
40 * @throws IllegalArgumentException if value is invalid
42 public static boolean isPresent(final byte value) {
50 throw invalidValue(value);
55 * Decode boolean from a field value, just like {@link Optional#get()}.
57 * @param value Field value
58 * @return Decoded boolean.
59 * @throws IllegalArgumentException if value is invalid
60 * @throws IllegalStateException if value has not been set
62 public static boolean get(final byte value) {
65 throw new IllegalStateException("Field has not been initialized");
71 throw invalidValue(value);
76 * Encode a boolean to a field value, just like {@link Optional#of(Object)}.
78 * @param bool Boolean value.
79 * @return Field value.
81 public static byte of(final boolean bool) {
82 return bool ? TRUE : FALSE;
86 * Convert a nullable {@link Boolean} into a field value, just like {@link Optional#ofNullable(Object)}.
88 * @param bool Boolean value.
89 * @return Field value.
91 public static byte ofNullable(final @Nullable Boolean bool) {
92 return bool == null ? ABSENT : of(bool.booleanValue());
96 * Convert a field value to a nullable {@link Boolean}. Similar to {@code Optional.orElse(null)}.
98 * @param value Field value.
99 * @return Nullable Boolean.
101 @SuppressFBWarnings("NP_BOOLEAN_RETURN_NULL")
102 public static @Nullable Boolean toNullable(final byte value) {
107 return Boolean.FALSE;
111 throw invalidValue(value);
116 * Convert a field value into an {@link Optional} {@link Boolean}.
118 * @param value Field value.
119 * @return Optional {@link Boolean}.
120 * @throws IllegalArgumentException if value is invalid.
122 public static Optional<Boolean> toOptional(final byte value) {
125 return Optional.empty();
127 return Optional.of(Boolean.FALSE);
129 return Optional.of(Boolean.TRUE);
131 throw invalidValue(value);
136 * Convert a field value into a String representation.
138 * @param value Field value.
139 * @return Boolean-compatible string, or "absent".
140 * @throws IllegalArgumentException if value is invalid.
142 public static String toString(final byte value) {
147 return Boolean.toString(false);
149 return Boolean.toString(true);
151 throw invalidValue(value);
155 private static IllegalArgumentException invalidValue(final byte value) {
156 throw new IllegalArgumentException("Invalid field value " + value);