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