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