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