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.part;
018import static org.apache.juneau.commons.utils.Shorts.*;
019
020import java.util.*;
021import java.util.function.*;
022
023import org.apache.http.*;
024import org.apache.juneau.bct.assertions.*;
025
026/**
027 * A {@link NameValuePair} that consists of a single integer value.
028 *
029 * <h5 class='section'>See Also:</h5><ul>
030 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommon">juneau-rest-common</a>
031 * </ul>
032 */
033@SuppressWarnings({
034   "java:S2160" // equals() inherited from BasicPart compares name+value; typed Integer field is accessed via getValue()
035})
036public class BasicIntegerPart extends BasicPart {
037
038   /**
039    * Static creator.
040    *
041    * @param name The part name.
042    * @param value The part value.
043    * @return A new {@link BasicIntegerPart} object, or <jk>null</jk> if the name or value is <jk>null</jk>.
044    */
045   public static BasicIntegerPart of(String name, Integer value) {
046      if (ie(name) || value == null)
047         return null;
048      return new BasicIntegerPart(name, value);
049   }
050
051   /**
052    * Static creator with delayed value.
053    *
054    * <p>
055    * Part value is re-evaluated on each call to {@link NameValuePair#getValue()}.
056    *
057    * @param name The part name.
058    * @param value The part value supplier.
059    * @return A new {@link BasicIntegerPart} object, or <jk>null</jk> if the name or supplier is <jk>null</jk>.
060    */
061   public static BasicIntegerPart of(String name, Supplier<Integer> value) {
062      if (ie(name) || value == null)
063         return null;
064      return new BasicIntegerPart(name, value);
065   }
066
067   private final Integer value;
068   private final Supplier<Integer> supplier;
069
070   /**
071    * Constructor.
072    *
073    * @param name The part name.  Must not be <jk>null</jk>.
074    * @param value The part value.  Can be <jk>null</jk>.
075    */
076   public BasicIntegerPart(String name, Integer value) {
077      super(name, value);
078      this.value = value;
079      this.supplier = null;
080   }
081
082   /**
083    * Constructor.
084    *
085    * <p>
086    * <jk>null</jk> and empty values are treated as <jk>null</jk>.
087    * Otherwise parses using {@link Integer#valueOf(String)}.
088    *
089    * @param name The part name.  Must not be <jk>null</jk>.
090    * @param value The part value.  Can be <jk>null</jk>.
091    */
092   public BasicIntegerPart(String name, String value) {
093      super(name, value);
094      this.value = ie(value) ? null : Integer.valueOf(value);
095      this.supplier = null;
096   }
097
098   /**
099    * Constructor.
100    *
101    * @param name The part name.  Must not be <jk>null</jk>.
102    * @param value The part value supplier.  Can be <jk>null</jk> or supply <jk>null</jk>.
103    */
104   public BasicIntegerPart(String name, Supplier<Integer> value) {
105      super(name, value);
106      this.value = null;
107      supplier = value;
108   }
109
110   /**
111    * Returns The part value as an {@link Integer} wrapped in an {@link Optional}.
112    *
113    * @return The part value as an {@link Integer} wrapped in an {@link Optional}.  Never <jk>null</jk>.
114    */
115   public Optional<Integer> asInteger() {
116      return o(toInteger());
117   }
118
119   /**
120    * Provides the ability to perform fluent-style assertions on this part.
121    *
122    * @return A new fluent assertion object.
123    * @throws AssertionError If assertion failed.
124    */
125   public FluentIntegerAssertion<BasicIntegerPart> assertInteger() {
126      return new FluentIntegerAssertion<>(value(), this);
127   }
128
129   @Override /* Overridden from Header */
130   public String getValue() { return s(value()); }
131
132   /**
133    * Return the value if present, otherwise return <c>other</c>.
134    *
135    * <p>
136    * This is a shortened form for calling <c>asInteger().orElse(<jv>other</jv>)</c>.
137    *
138    * @param other The value to be returned if there is no value present, can be <jk>null</jk>.
139    * @return The value, if present, otherwise <c>other</c>.
140    */
141   public Integer orElse(Integer other) {
142      Integer x = value();
143      return nn(x) ? x : other;
144   }
145
146   /**
147    * Returns The part value as an {@link Integer}.
148    *
149    * @return The part value as an {@link Integer}, or <jk>null</jk> if the value <jk>null</jk>.
150    */
151   public Integer toInteger() {
152      return value();
153   }
154
155   private Integer value() {
156      if (nn(supplier))
157         return supplier.get();
158      return value;
159   }
160}