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 java.util.function.*;
016
017import org.apache.juneau.http.annotation.*;
018
019/**
020 * Represents a parsed <l>Age</l> HTTP response header.
021 *
022 * <p>
023 * The age the object has been in a proxy cache in seconds.
024 *
025 * <h5 class='figure'>Example</h5>
026 * <p class='bcode w800'>
027 *    Age: 12
028 * </p>
029 *
030 * <h5 class='topic'>RFC2616 Specification</h5>
031 *
032 * The Age response-header field conveys the sender's estimate of the amount of time since the response (or its
033 * revalidation) was generated at the origin server.
034 * A cached response is "fresh" if its age does not exceed its freshness lifetime.
035 * Age values are calculated as specified in section 13.2.3.
036 *
037 * <p class='bcode w800'>
038 *    Age = "Age" ":" age-value
039 *    age-value = delta-seconds
040 * </p>
041 *
042 * <p>
043 * Age values are non-negative decimal integers, representing time in seconds.
044 *
045 * <p>
046 * If a cache receives a value larger than the largest positive integer it can represent, or if any of its age
047 * calculations overflows, it MUST transmit an Age header with a value of 2147483648 (2^31).
048 *
049 * <p>
050 * An HTTP/1.1 server that includes a cache MUST include an Age header field in every response generated from its own
051 * cache.
052 *
053 * <p>
054 * Caches SHOULD use an arithmetic type of at least 31 bits of range.
055 *
056 * <ul class='seealso'>
057 *    <li class='extlink'>{@doc ExtRFC2616}
058 * </ul>
059 */
060@Header("Age")
061public class Age extends BasicIntegerHeader {
062
063   private static final long serialVersionUID = 1L;
064
065   /**
066    * Convenience creator.
067    *
068    * @param value
069    *    The header value.
070    *    <br>Can be any of the following:
071    *    <ul>
072    *       <li>{@link Number} - Converted to an integer using {@link Number#intValue()}.
073    *       <li>{@link String} - Parsed using {@link Integer#parseInt(String)}.
074    *       <li>Anything else - Converted to <c>String</c>.
075    *    </ul>
076    * @return A new {@link BasicIntegerHeader} object.
077    */
078   public static Age of(Object value) {
079      if (value == null)
080         return null;
081      return new Age(value);
082   }
083
084   /**
085    * Convenience creator using supplier.
086    *
087    * <p>
088    * Header value is re-evaluated on each call to {@link #getValue()}.
089    *
090    * @param value
091    *    The header value supplier.
092    *    <br>Can be any of the following:
093    *    <ul>
094    *       <li>{@link Number} - Converted to an integer using {@link Number#intValue()}.
095    *       <li>{@link String} - Parsed using {@link Integer#parseInt(String)}.
096    *       <li>Anything else - Converted to <c>String</c>.
097    *    </ul>
098    * @return A new {@link BasicIntegerHeader} object.
099    */
100   public static Age of(Supplier<?> value) {
101      if (value == null)
102         return null;
103      return new Age(value);
104   }
105
106   /**
107    * Constructor.
108    *
109    * @param value
110    *    The header value.
111    *    <br>Can be any of the following:
112    *    <ul>
113    *       <li>{@link Number} - Converted to an integer using {@link Number#intValue()}.
114    *       <li>{@link String} - Parsed using {@link Integer#parseInt(String)}.
115    *       <li>Anything else - Converted to <c>String</c>.
116    *       <li>A {@link Supplier} of anything on this list.
117    *    </ul>
118    */
119   public Age(Object value) {
120      super("Age", value);
121   }
122
123   /**
124    * Constructor
125    *
126    * @param value
127    *    The header value.
128    */
129   public Age(String value) {
130      this((Object)value);
131   }
132}