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 long 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 Long field is accessed via getValue()
035})
036public class BasicLongPart 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 BasicLongPart} object, or <jk>null</jk> if the name or value is <jk>null</jk>.
044    */
045   public static BasicLongPart of(String name, Long value) {
046      if (ie(name) || value == null)
047         return null;
048      return new BasicLongPart(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 BasicLongPart} object, or <jk>null</jk> if the name or supplier is <jk>null</jk>.
060    */
061   public static BasicLongPart of(String name, Supplier<Long> value) {
062      if (ie(name) || value == null)
063         return null;
064      return new BasicLongPart(name, value);
065   }
066
067   private final Long value;
068   private final Supplier<Long> 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 BasicLongPart(String name, Long value) {
077      super(name, value);
078      this.value = value;
079      this.supplier = null;
080
081   }
082
083   /**
084    * Constructor.
085    *
086    * <p>
087    * <jk>null</jk> and empty values are treated as <jk>null</jk>.
088    * Otherwise parses using {@link Long#valueOf(String)}.
089    *
090    * @param name The part name.  Must not be <jk>null</jk>.
091    * @param value The part value.  Can be <jk>null</jk>.
092    */
093   public BasicLongPart(String name, String value) {
094      super(name, value);
095      this.value = ie(value) ? null : Long.valueOf(value);
096      this.supplier = null;
097   }
098
099   /**
100    * Constructor.
101    *
102    * @param name The part name.  Must not be <jk>null</jk>.
103    * @param value The part value supplier.  Can be <jk>null</jk> or supply <jk>null</jk>.
104    */
105   public BasicLongPart(String name, Supplier<Long> value) {
106      super(name, value);
107      this.value = null;
108      supplier = value;
109   }
110
111   /**
112    * Returns The part value as a {@link Long} wrapped in an {@link Optional}.
113    *
114    * @return The part value as a {@link Long} wrapped in an {@link Optional}.  Never <jk>null</jk>.
115    */
116   public Optional<Long> asLong() {
117      return o(value());
118   }
119
120   /**
121    * Provides the ability to perform fluent-style assertions on this part.
122    *
123    * @return A new fluent assertion object.
124    * @throws AssertionError If assertion failed.
125    */
126   public FluentLongAssertion<BasicLongPart> assertLong() {
127      return new FluentLongAssertion<>(value(), this);
128   }
129
130   @Override /* Overridden from Header */
131   public String getValue() { return s(value()); }
132
133   /**
134    * Return the value if present, otherwise return <c>other</c>.
135    *
136    * <p>
137    * This is a shortened form for calling <c>asLong().orElse(<jv>other</jv>)</c>.
138    *
139    * @param other The value to be returned if there is no value present, can be <jk>null</jk>.
140    * @return The value, if present, otherwise <c>other</c>.
141    */
142   public Long orElse(Long other) {
143      Long x = value();
144      return nn(x) ? x : other;
145   }
146
147   /**
148    * Returns The part value as a {@link Long}.
149    *
150    * @return The part value as a {@link Long}, or <jk>null</jk> if the value <jk>null</jk>.
151    */
152   public Long toLong() {
153      return value();
154   }
155
156   private Long value() {
157      if (nn(supplier))
158         return supplier.get();
159      return value;
160   }
161}