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