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.classic.header;
018import static org.apache.juneau.commons.utils.Shorts.*;
019
020import java.util.*;
021import java.util.function.*;
022
023import org.apache.juneau.commons.*;
024import org.apache.juneau.commons.utils.*;
025import org.apache.juneau.http.*;
026import org.apache.juneau.bct.assertions.*;
027
028/**
029 * Category of headers that consist of a single boolean value.
030 *
031 * <h2 class='figure'>Example</h2>
032 * <p class='bcode'>
033 *    Foo: true
034 * </p>
035 *
036 * <h2 class='section'>See Also:</h2><ul>
037 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommon">juneau-rest-common</a>
038 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
039 * </ul>
040 *
041 * @serial exclude
042 */
043@Header
044@Schema(type = "boolean")
045@SuppressWarnings({
046   "java:S2160" // equals() inherited from BasicHeader compares name+value; typed boolean field is accessed via getValue()
047})
048public class BasicBooleanHeader extends BasicHeader {
049   private static final long serialVersionUID = 1L;
050
051   /**
052    * Static creator.
053    *
054    * @param name The header name.
055    * @param value
056    *    The header value.
057    *    <br>Can be <jk>null</jk>.
058    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
059    * @throws IllegalArgumentException If name is <jk>null</jk> or empty.
060    */
061   public static BasicBooleanHeader of(String name, Boolean value) {
062      return value == null ? null : new BasicBooleanHeader(name, value);
063   }
064
065   /**
066    * Static creator.
067    *
068    * @param name The header name.
069    * @param value
070    *    The header value.
071    *    <br>Must be parsable by {@link Boolean#parseBoolean(String)}.
072    *    <br>Can be <jk>null</jk>.
073    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
074    * @throws IllegalArgumentException If name is <jk>null</jk> or empty.
075    */
076   public static BasicBooleanHeader of(String name, String value) {
077      return value == null ? null : new BasicBooleanHeader(name, value);
078   }
079
080   /**
081    * Static creator with delayed value.
082    *
083    * <p>
084    * Header value is re-evaluated on each call to {@link #getValue()}.
085    *
086    * @param name The header name.
087    * @param value
088    *    The supplier of the header value.
089    *    <br>Can be <jk>null</jk>.
090    * @return A new header bean, or <jk>null</jk> if the value is <jk>null</jk>.
091    * @throws IllegalArgumentException If name is <jk>null</jk> or empty.
092    */
093   public static BasicBooleanHeader of(String name, Supplier<Boolean> value) {
094      return value == null ? null : new BasicBooleanHeader(name, value);
095   }
096
097   private final Boolean value;
098   private final transient Supplier<Boolean> supplier;
099
100   /**
101    * Constructor.
102    *
103    * @param name The header name.
104    * @param value
105    *    The header value.
106    *    <br>Can be <jk>null</jk> to create a header with no value.
107    * @throws IllegalArgumentException If name is <jk>null</jk> or empty.
108    */
109   public BasicBooleanHeader(String name, Boolean value) {
110      super(name, value);
111      this.value = value;
112      this.supplier = null;
113   }
114
115   /**
116    * Constructor.
117    *
118    * @param name The header name.
119    * @param value
120    *    The header value.
121    *    <br>Must be parsable by {@link Boolean#parseBoolean(String)}.
122    *    <br>Can be <jk>null</jk> to create a header with no value.
123    * @throws IllegalArgumentException If name is <jk>null</jk> or empty.
124    */
125   public BasicBooleanHeader(String name, String value) {
126      super(name, value);
127      this.value = ie(value) ? null : b(value);
128      this.supplier = null;
129   }
130
131   /**
132    * Constructor with delayed value.
133    *
134    * <p>
135    * Header value is re-evaluated on each call to {@link #getValue()}.
136    *
137    * @param name The header name.
138    * @param value
139    *    The supplier of the header value.
140    *    <br>Can be <jk>null</jk> to create a header with no value.
141    * @throws IllegalArgumentException If name is <jk>null</jk> or empty.
142    */
143   public BasicBooleanHeader(String name, Supplier<Boolean> value) {
144      super(name, null);
145      this.value = null;
146      supplier = value;
147   }
148
149   /**
150    * Returns the header value as a {@link Boolean} wrapped in an {@link Optional}.
151    *
152    * @return The header value as a {@link Boolean} wrapped in an {@link Optional}.  Never <jk>null</jk>.
153    */
154   public Optional<Boolean> asBoolean() {
155      return o(value());
156   }
157
158   /**
159    * Provides the ability to perform fluent-style assertions on this header.
160    *
161    * <h4 class='section'>Examples:</h4>
162    * <p class='bjava'>
163    *    <jc>// Validates the response header Foo is true.</jc>
164    *    <jv>client</jv>
165    *       .get(<jsf>URL</jsf>)
166    *       .run()
167    *       .getHeader(<js>"Foo"</js>).asBooleanHeader().assertBoolean().isTrue();
168    * </p>
169    *
170    * @return A new fluent assertion object.
171    * @throws AssertionError If assertion failed.
172    */
173   public FluentBooleanAssertion<BasicBooleanHeader> assertBoolean() {
174      return new FluentBooleanAssertion<>(value(), this);
175   }
176
177   @Override /* Overridden from Header */
178   public String getValue() { return s(value()); }
179
180   /**
181    * Returns <jk>true</jk> if the header value is <jk>true</jk>.
182    *
183    * @return <jk>true</jk> if the header value is <jk>true</jk>.
184    */
185   public boolean isTrue() {
186      return ObjectUtils.isTrue(value());
187   }
188
189   /**
190    * Return the value if present, otherwise return <c>other</c>.
191    *
192    * <p>
193    * This is a shortened form for calling <c>asBoolean().orElse(<jv>other</jv>)</c>.
194    *
195    * @param other The value to be returned if there is no value present, can be <jk>null</jk>.
196    * @return The value, if present, otherwise <c>other</c>.
197    */
198   public Boolean orElse(Boolean other) {
199      Boolean x = value();
200      return nn(x) ? x : other;
201   }
202
203   /**
204    * Returns the header value as a {@link Boolean}.
205    *
206    * @return The header value as a {@link Boolean}, or <jk>null</jk> if the header has no value.
207    */
208   public Boolean toBoolean() {
209      return value();
210   }
211
212   private Boolean value() {
213      if (nn(supplier))
214         return supplier.get();
215      return value;
216   }
217}