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