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 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/JuneauRestCommonBasics">juneau-rest-common Basics</a>
031 * </ul>
032 */
033public class BasicLongPart 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 BasicLongPart} object, or <jk>null</jk> if the name or value is <jk>null</jk>.
045    */
046   public static BasicLongPart of(String name, Long value) {
047      if (Utils.isEmpty(name) || value == null)
048         return null;
049      return new BasicLongPart(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 BasicLongPart} object, or <jk>null</jk> if the name or supplier is <jk>null</jk>.
061    */
062   public static BasicLongPart of(String name, Supplier<Long> value) {
063      if (Utils.isEmpty(name) || value == null)
064         return null;
065      return new BasicLongPart(name, value);
066   }
067
068   //-----------------------------------------------------------------------------------------------------------------
069   // Instance
070   //-----------------------------------------------------------------------------------------------------------------
071
072   private final Long value;
073   private final Supplier<Long> 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 BasicLongPart(String name, Long value) {
082      super(name, value);
083      this.value = value;
084      this.supplier = null;
085
086   }
087
088   /**
089    * Constructor.
090    *
091    * @param name The part name.  Must not be <jk>null</jk>.
092    * @param value The part value supplier.  Can be <jk>null</jk> or supply <jk>null</jk>.
093    */
094   public BasicLongPart(String name, Supplier<Long> value) {
095      super(name, value);
096      this.value = null;
097      this.supplier = value;
098   }
099
100   /**
101    * Constructor.
102    *
103    * <p>
104    * <jk>null</jk> and empty values are treated as <jk>null</jk>.
105    * Otherwise parses using {@link Long#valueOf(String)}.
106    *
107    * @param name The part name.  Must not be <jk>null</jk>.
108    * @param value The part value.  Can be <jk>null</jk>.
109    */
110   public BasicLongPart(String name, String value) {
111      super(name, value);
112      this.value = Utils.isEmpty(value) ? null : Long.valueOf(value);
113      this.supplier = null;
114   }
115
116   @Override /* Header */
117   public String getValue() {
118      return Utils.s(value());
119   }
120
121   /**
122    * Returns The part value as a {@link Long} wrapped in an {@link Optional}.
123    *
124    * @return The part value as a {@link Long} wrapped in an {@link Optional}.  Never <jk>null</jk>.
125    */
126   public Optional<Long> asLong() {
127      return Utils.opt(value());
128   }
129
130   /**
131    * Returns The part value as a {@link Long}.
132    *
133    * @return The part value as a {@link Long}, or <jk>null</jk> if the value <jk>null</jk>.
134    */
135   public Long toLong() {
136      return value();
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 FluentLongAssertion<BasicLongPart> assertLong() {
145      return new FluentLongAssertion<>(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>asLong().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 Long orElse(Long other) {
158      Long x = value();
159      return x != null ? x : other;
160   }
161
162   private Long value() {
163      if (supplier != null)
164         return supplier.get();
165      return value;
166   }
167}