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 static org.apache.juneau.commons.utils.Shorts.*;
020import static org.apache.juneau.commons.utils.StringUtils.*;
021
022import java.util.*;
023import java.util.function.*;
024
025/**
026 * An {@link org.apache.juneau.http.HttpPart} whose value is a long integer.
027 *
028 * <p>
029 * Mirrors the semantics of {@code BasicLongPart} from {@code juneau-rest-common-classic} without
030 * the Apache HttpCore dependency.
031 *
032 * @since 10.0.0
033 */
034@SuppressWarnings({
035   "java:S2160" // Equality is fully determined by name + getValue() in HttpPartBean; the typed fields are derived from the wire value and add no state to compare
036})
037public class HttpLongPart extends HttpPartBean {
038
039   /**
040    * Creates an {@link HttpLongPart} with the given name and long value.
041    *
042    * @param name The part name. Must not be <jk>null</jk>.
043    * @param value The long value. May be <jk>null</jk>.
044    * @return A new instance. Never <jk>null</jk>.
045    */
046   public static HttpLongPart of(String name, Long value) {
047      return new HttpLongPart(name, value);
048   }
049
050   /**
051    * Creates an {@link HttpLongPart} by parsing the given wire value.
052    *
053    * @param name The part name. Must not be <jk>null</jk>.
054    * @param value The wire value. May be <jk>null</jk> or empty.
055    * @return A new instance. Never <jk>null</jk>.
056    */
057   public static HttpLongPart ofString(String name, String value) {
058      return new HttpLongPart(name, value);
059   }
060
061   /**
062    * Creates an {@link HttpLongPart} with a lazy long supplier.
063    *
064    * @param name The part name. Must not be <jk>null</jk>.
065    * @param supplier Supplier for the long value. Must not be <jk>null</jk>.
066    * @return A new instance. Never <jk>null</jk>.
067    */
068   public static HttpLongPart ofLazy(String name, Supplier<Long> supplier) {
069      return new HttpLongPart(name, supplier);
070   }
071
072   private final Long typedValue;
073   private final Supplier<Long> typedSupplier;
074
075   /**
076    * Constructor accepting a wire-format string value.
077    *
078    * @param name The part name. Must not be <jk>null</jk>.
079    * @param value The wire value. May be <jk>null</jk> or empty.
080    */
081   public HttpLongPart(String name, String value) {
082      super(name, value);
083      this.typedValue = toLong(value);
084      this.typedSupplier = null;
085   }
086
087   /**
088    * Constructor accepting a {@link Long} value.
089    *
090    * @param name The part name. Must not be <jk>null</jk>.
091    * @param value The long value. May be <jk>null</jk>.
092    */
093   public HttpLongPart(String name, Long value) {
094      super(name, s(value));
095      this.typedValue = value;
096      this.typedSupplier = null;
097   }
098
099   /**
100    * Constructor accepting a lazy {@link Long} supplier.
101    *
102    * @param name The part name. Must not be <jk>null</jk>.
103    * @param supplier Supplier for the long value. Must not be <jk>null</jk>.
104    */
105   public HttpLongPart(String name, Supplier<Long> supplier) {
106      super(name, () -> s(supplier.get()));
107      this.typedValue = null;
108      this.typedSupplier = supplier;
109   }
110
111   /**
112    * Returns the value as an {@link Optional}{@code <Long>}.
113    *
114    * @return The long value, wrapped in an {@link Optional}. Never <jk>null</jk>.
115    */
116   public Optional<Long> asLong() {
117      return o(toLong());
118   }
119
120   /**
121    * Returns the value if present, otherwise returns {@code other}.
122    *
123    * @param other The default value.
124    * @return The value or {@code other}.
125    */
126   public Long orElse(Long other) {
127      return asLong().orElse(other);
128   }
129
130   /**
131    * Returns the value as a {@link Long}, or {@code null} if not set.
132    *
133    * @return The long value, possibly {@code null}.
134    */
135   public Long toLong() {
136      if (typedSupplier != null)
137         return typedSupplier.get();
138      return typedValue;
139   }
140
141   private static Long toLong(String value) {
142      if (ie(value))
143         return null;
144      return parseLong(value, () -> iaex("Value '%s' could not be parsed as a long.", value));
145   }
146}