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; 018 019import static org.apache.juneau.commons.utils.Shorts.*; 020import static org.apache.juneau.commons.utils.StringUtils.*; 021 022import java.util.*; 023import java.util.function.*; 024 025import org.apache.juneau.commons.*; 026import org.apache.juneau.http.*; 027import org.apache.juneau.bct.assertions.*; 028 029/** 030 * Category of headers that consist of a single integer value. 031 * 032 * <h2 class='figure'>Example</h2> 033 * <p class='bcode'> 034 * Age: 300 035 * </p> 036 * 037 * <h2 class='section'>See Also:</h2><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 = "integer", format = "int32") 046@SuppressWarnings({ 047 "java:S2160" // equals() inherited from BasicHeader compares name+value; typed Integer field is accessed via getValue() 048}) 049public class BasicIntegerHeader 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 BasicIntegerHeader of(String name, Integer value) { 063 return value == null ? null : new BasicIntegerHeader(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 using {@link Integer#parseInt(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 BasicIntegerHeader of(String name, String value) { 078 return value == null ? null : new BasicIntegerHeader(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 BasicIntegerHeader of(String name, Supplier<Integer> value) { 095 return value == null ? null : new BasicIntegerHeader(name, value); 096 } 097 098 private final Integer value; 099 private final transient Supplier<Integer> 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 BasicIntegerHeader(String name, Integer 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 using {@link Integer#parseInt(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 BasicIntegerHeader(String name, String value) { 127 super(name, value); 128 this.value = parse(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 BasicIntegerHeader(String name, Supplier<Integer> value) { 145 super(name, null); 146 this.value = null; 147 supplier = value; 148 } 149 150 /** 151 * Returns the header value as an {@link Integer} wrapped in an {@link Optional}. 152 * 153 * @return The header value as an {@link Integer} wrapped in an {@link Optional}. Never <jk>null</jk>. 154 */ 155 public Optional<Integer> asInteger() { 156 return o(value()); 157 } 158 159 /** 160 * Provides the ability to perform fluent-style assertions on this header. 161 * 162 * <h4 class='section'>Examples:</h4> 163 * <p class='bjava'> 164 * <jc>// Validates the response content is older than 1.</jc> 165 * <jv>client</jv> 166 * .get(<jsf>URL</jsf>) 167 * .run() 168 * .getHeader(<js>"Age"</js>).asIntegerHeader().assertInteger().isGreaterThan(1); 169 * </p> 170 * 171 * @return A new fluent assertion object. 172 * @throws AssertionError If assertion failed. 173 */ 174 public FluentIntegerAssertion<BasicIntegerHeader> assertInteger() { 175 return new FluentIntegerAssertion<>(value(), this); 176 } 177 178 @Override /* Overridden from Header */ 179 public String getValue() { return s(value()); } 180 181 /** 182 * Return the value if present, otherwise return <c>other</c>. 183 * 184 * <p> 185 * This is a shortened form for calling <c>asInteger().orElse(<jv>other</jv>)</c>. 186 * 187 * @param other The value to be returned if there is no value present, can be <jk>null</jk>. 188 * @return The value, if present, otherwise <c>other</c>. 189 */ 190 public Integer orElse(Integer other) { 191 Integer x = value(); 192 return nn(x) ? x : other; 193 } 194 195 /** 196 * Returns the header value as an {@link Integer}. 197 * 198 * @return The header value as an {@link Integer}, or <jk>null</jk> if the header has no value. 199 */ 200 public Integer toInteger() { 201 return value(); 202 } 203 204 private static Integer parse(String value) { 205 if (value == null) 206 return null; 207 return parseInt(value, () -> iaex("Value '%s' could not be parsed as an integer.", value)); 208 } 209 210 private Integer value() { 211 if (nn(supplier)) 212 return supplier.get(); 213 return value; 214 } 215}