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