001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.http.header;
014
015import static org.apache.juneau.common.internal.StringUtils.*;
016import static org.apache.juneau.internal.CollectionUtils.*;
017
018import java.util.*;
019import java.util.function.*;
020
021import org.apache.juneau.*;
022import org.apache.juneau.annotation.*;
023import org.apache.juneau.assertions.*;
024import org.apache.juneau.http.annotation.*;
025
026/**
027 * Category of headers that consist of a single long value.
028 *
029 * <p>
030 * <h5 class='figure'>Example</h5>
031 * <p class='bcode'>
032 *    Content-Length: 300
033 * </p>
034 *
035 * <h5 class='section'>See Also:</h5><ul>
036 *    <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a>
037 *    <li class='extlink'><a class="doclink" href="https://www.w3.org/Protocols/rfc2616/rfc2616.html">Hypertext Transfer Protocol -- HTTP/1.1</a>
038 * </ul>
039 *
040 * @serial exclude
041 */
042@Header
043@Schema(type="integer",format="int64")
044public class BasicLongHeader extends BasicHeader {
045
046   //-----------------------------------------------------------------------------------------------------------------
047   // Static
048   //-----------------------------------------------------------------------------------------------------------------
049
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>Must be parsable by {@link Long#parseLong(String)}.
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 BasicLongHeader of(String name, String value) {
064      return value == null ? null : new BasicLongHeader(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 by {@link Long#parseLong(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 BasicLongHeader of(String name, Long value) {
079      return value == null ? null : new BasicLongHeader(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 BasicLongHeader of(String name, Supplier<Long> value) {
096      return value == null ? null : new BasicLongHeader(name, value);
097   }
098
099   //-----------------------------------------------------------------------------------------------------------------
100   // Instance
101   //-----------------------------------------------------------------------------------------------------------------
102
103   private final Long value;
104   private final Supplier<Long> 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 Long#parseLong(String)}.
113    *    <br>Can be <jk>null</jk>.
114    * @throws IllegalArgumentException If name is <jk>null</jk> or empty.
115    */
116   public BasicLongHeader(String name, String value) {
117      super(name, value);
118      this.value = parse(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 BasicLongHeader(String name, Long 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 BasicLongHeader(String name, Supplier<Long> value) {
150      super(name, null);
151      this.value = null;
152      this.supplier = value;
153   }
154
155   @Override /* Header */
156   public String getValue() {
157      return stringify(value());
158   }
159
160   /**
161    * Returns the header value as a {@link Long} wrapped in an {@link Optional}.
162    *
163    * @return The header value as a {@link Long} wrapped in an {@link Optional}.  Never <jk>null</jk>.
164    */
165   public Optional<Long> asLong() {
166      return optional(value());
167   }
168
169   /**
170    * Returns the header value as a {@link Long}.
171    *
172    * @return The header value as a {@link Long}.  Can be <jk>null</jk>.
173    */
174   public Long toLong() {
175      return value();
176   }
177
178   /**
179    * Provides the ability to perform fluent-style assertions on this header.
180    *
181    * <h5 class='section'>Examples:</h5>
182    * <p class='bjava'>
183    *    <jc>// Validates the response body is not too large.</jc>
184    *    <jv>client</jv>
185    *       .get(<jsf>URL</jsf>)
186    *       .run()
187    *       .getHeader(<js>"Length"</js>).asLongHeader().assertLong().isLessThan(100000);
188    * </p>
189    *
190    * @return A new fluent assertion object.
191    * @throws AssertionError If assertion failed.
192    */
193   public FluentLongAssertion<BasicLongHeader> assertLong() {
194      return new FluentLongAssertion<>(value(), this);
195   }
196
197   /**
198    * Return the value if present, otherwise return <c>other</c>.
199    *
200    * <p>
201    * This is a shortened form for calling <c>asLong().orElse(<jv>other</jv>)</c>.
202    *
203    * @param other The value to be returned if there is no value present, can be <jk>null</jk>.
204    * @return The value, if present, otherwise <c>other</c>.
205    */
206   public Long orElse(Long other) {
207      Long x = value();
208      return x != null ? x : other;
209   }
210
211   private Long parse(String value) {
212      try {
213         return value == null ? null : Long.parseLong(value);
214      } catch (NumberFormatException e) {
215         throw new BasicRuntimeException("Value ''{0}'' could not be parsed as a long.", value);
216      }
217   }
218
219   private Long value() {
220      if (supplier != null)
221         return supplier.get();
222      return value;
223   }
224}