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 java.util.Optional;
12 import javax.annotation.Nullable;
15 * Utility class for storing an optional boolean in a single byte value. This cuts down the memory requirement quite
16 * at very small computational cost.
19 * Note: fields do not have to be explicitly initialized, as default initialization value for 'byte', 0, is used to
20 * represent 'absent' condition.
22 * @author Robert Varga
25 public final class OptionalBoolean {
26 private static final byte ABSENT = 0;
27 private static final byte FALSE = 1;
28 private static final byte TRUE = 2;
30 private OptionalBoolean() {
31 throw new UnsupportedOperationException();
35 * Check if a field value has been set, just like {@link Optional#isPresent()}.
37 * @param value field value
38 * @return True if the value is set.
39 * @throws IllegalArgumentException if value is invalid
41 public static boolean isPresent(final byte value) {
49 throw invalidValue(value);
54 * Decode boolean from a field value, just like {@link Optional#get()}.
56 * @param value Field value
57 * @return Decoded boolean.
58 * @throws IllegalArgumentException if value is invalid
59 * @throws IllegalStateException if value has not been set
61 public static boolean get(final byte value) {
64 throw new IllegalStateException("Field has not been initialized");
70 throw invalidValue(value);
75 * Encode a boolean to a field value, just like {@link Optional#of(Object)}.
77 * @param bool Boolean value.
78 * @return Field value.
80 public static byte of(final boolean bool) {
81 return bool ? TRUE : FALSE;
85 * Convert a nullable {@link Boolean} into a field value, just like {@link Optional#ofNullable(Object)}.
87 * @param bool Boolean value.
88 * @return Field value.
90 public static byte ofNullable(final @Nullable Boolean bool) {
91 return bool == null ? ABSENT : of(bool.booleanValue());
95 * Convert a field value to a nullable {@link Boolean}. Similar to {@code Optional.orElse(null)}.
97 * @param value Fied value.
98 * @return Nullable Boolean.
100 public static @Nullable Boolean toNullable(final byte value) {
105 return Boolean.FALSE;
109 throw invalidValue(value);
114 * Convert a field value into an {@link Optional} {@link Boolean}.
116 * @param value Field value.
117 * @return Optional {@link Boolean}.
118 * @throws IllegalArgumentException if value is invalid.
120 public static Optional<Boolean> toOptional(final byte value) {
123 return Optional.empty();
125 return Optional.of(Boolean.FALSE);
127 return Optional.of(Boolean.TRUE);
129 throw invalidValue(value);
134 * Convert a field value into a String representation.
136 * @param value Field value.
137 * @return Boolean-compatible string, or "absent".
138 * @throws IllegalArgumentException if value is invalid.
140 public static String toString(final byte value) {
145 return Boolean.toString(false);
147 return Boolean.toString(true);
149 throw invalidValue(value);
153 private static IllegalArgumentException invalidValue(final byte value) {
154 throw new IllegalArgumentException("Invalid field value " + value);