001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.http.part;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020
021import java.util.*;
022import java.util.function.*;
023
024/**
025 * An {@link org.apache.juneau.http.HttpPart} whose value is a boolean.
026 *
027 * <p>
028 * Mirrors the semantics of {@code BasicBooleanPart} from {@code juneau-rest-common-classic} without
029 * the Apache HttpCore dependency. The wire value uses {@code "true"} / {@code "false"}.
030 *
031 * @since 10.0.0
032 */
033@SuppressWarnings({
034   "java:S2160" // Equality is fully determined by name + getValue() in HttpPartBean; the typed fields are derived from the wire value and add no state to compare
035})
036public class HttpBooleanPart extends HttpPartBean {
037
038   /**
039    * Creates an {@link HttpBooleanPart} with the given name and boolean value.
040    *
041    * @param name The part name. Must not be <jk>null</jk>.
042    * @param value The boolean value. May be <jk>null</jk>.
043    * @return A new instance. Never <jk>null</jk>.
044    */
045   public static HttpBooleanPart of(String name, Boolean value) {
046      return new HttpBooleanPart(name, value);
047   }
048
049   /**
050    * Creates an {@link HttpBooleanPart} by parsing the given string value.
051    *
052    * @param name The part name. Must not be <jk>null</jk>.
053    * @param value The wire value. May be <jk>null</jk> or empty.
054    * @return A new instance. Never <jk>null</jk>.
055    */
056   public static HttpBooleanPart ofString(String name, String value) {
057      return new HttpBooleanPart(name, value);
058   }
059
060   /**
061    * Creates an {@link HttpBooleanPart} with a lazy boolean supplier.
062    *
063    * @param name The part name. Must not be <jk>null</jk>.
064    * @param supplier Supplier for the boolean value. Must not be <jk>null</jk>.
065    * @return A new instance. Never <jk>null</jk>.
066    */
067   public static HttpBooleanPart ofLazy(String name, Supplier<Boolean> supplier) {
068      return new HttpBooleanPart(name, supplier);
069   }
070
071   private final Boolean typedValue;
072   private final Supplier<Boolean> typedSupplier;
073
074   /**
075    * Constructor accepting a wire-format string value.
076    *
077    * @param name The part name. Must not be <jk>null</jk>.
078    * @param value The wire value (e.g. {@code "true"}). May be <jk>null</jk> or empty.
079    */
080   public HttpBooleanPart(String name, String value) {
081      super(name, value);
082      this.typedValue = ie(value) ? null : Boolean.valueOf(b(value));
083      this.typedSupplier = null;
084   }
085
086   /**
087    * Constructor accepting a {@link Boolean} value.
088    *
089    * @param name The part name. Must not be <jk>null</jk>.
090    * @param value The boolean value. May be <jk>null</jk>.
091    */
092   public HttpBooleanPart(String name, Boolean value) {
093      super(name, value == null ? null : value.toString());
094      this.typedValue = value;
095      this.typedSupplier = null;
096   }
097
098   /**
099    * Constructor accepting a lazy {@link Boolean} supplier.
100    *
101    * @param name The part name. Must not be <jk>null</jk>.
102    * @param supplier Supplier for the boolean value. Must not be <jk>null</jk>.
103    */
104   public HttpBooleanPart(String name, Supplier<Boolean> supplier) {
105      super(name, () -> {
106         var v = supplier.get();
107         return v == null ? null : v.toString();
108      });
109      this.typedValue = null;
110      this.typedSupplier = supplier;
111   }
112
113   /**
114    * Returns the value as an {@link Optional}{@code <Boolean>}.
115    *
116    * @return The boolean value, wrapped in an {@link Optional}. Never <jk>null</jk>.
117    */
118   public Optional<Boolean> asBoolean() {
119      return o(toBoolean());
120   }
121
122   /**
123    * Returns the value if present, otherwise returns {@code other}.
124    *
125    * @param other The default value.
126    * @return The value or {@code other}.
127    */
128   public Boolean orElse(Boolean other) {
129      var x = toBoolean();
130      return nn(x) ? x : other;
131   }
132
133   /**
134    * Returns the value as a {@link Boolean}, or {@code null} if not set.
135    *
136    * @return The boolean value, possibly {@code null}.
137    */
138   public Boolean toBoolean() {
139      if (typedSupplier != null)
140         return typedSupplier.get();
141      return typedValue;
142   }
143}