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 an integer. 027 * 028 * <p> 029 * Mirrors the semantics of {@code BasicIntegerPart} 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 HttpIntegerPart extends HttpPartBean { 038 039 /** 040 * Creates an {@link HttpIntegerPart} with the given name and integer value. 041 * 042 * @param name The part name. Must not be <jk>null</jk>. 043 * @param value The integer value. May be <jk>null</jk>. 044 * @return A new instance. Never <jk>null</jk>. 045 */ 046 public static HttpIntegerPart of(String name, Integer value) { 047 return new HttpIntegerPart(name, value); 048 } 049 050 /** 051 * Creates an {@link HttpIntegerPart} 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 HttpIntegerPart ofString(String name, String value) { 058 return new HttpIntegerPart(name, value); 059 } 060 061 /** 062 * Creates an {@link HttpIntegerPart} with a lazy integer supplier. 063 * 064 * @param name The part name. Must not be <jk>null</jk>. 065 * @param supplier Supplier for the integer value. Must not be <jk>null</jk>. 066 * @return A new instance. Never <jk>null</jk>. 067 */ 068 public static HttpIntegerPart ofLazy(String name, Supplier<Integer> supplier) { 069 return new HttpIntegerPart(name, supplier); 070 } 071 072 private final Integer typedValue; 073 private final Supplier<Integer> 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 HttpIntegerPart(String name, String value) { 082 super(name, value); 083 this.typedValue = toInteger(value); 084 this.typedSupplier = null; 085 } 086 087 /** 088 * Constructor accepting an {@link Integer} value. 089 * 090 * @param name The part name. Must not be <jk>null</jk>. 091 * @param value The integer value. May be <jk>null</jk>. 092 */ 093 public HttpIntegerPart(String name, Integer value) { 094 super(name, s(value)); 095 this.typedValue = value; 096 this.typedSupplier = null; 097 } 098 099 /** 100 * Constructor accepting a lazy {@link Integer} supplier. 101 * 102 * @param name The part name. Must not be <jk>null</jk>. 103 * @param supplier Supplier for the integer value. Must not be <jk>null</jk>. 104 */ 105 public HttpIntegerPart(String name, Supplier<Integer> 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 <Integer>}. 113 * 114 * @return The integer value, wrapped in an {@link Optional}. Never <jk>null</jk>. 115 */ 116 public Optional<Integer> asInteger() { 117 return o(toInteger()); 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 Integer orElse(Integer other) { 127 return asInteger().orElse(other); 128 } 129 130 /** 131 * Returns the value as an {@link Integer}, or {@code null} if not set. 132 * 133 * @return The integer value, possibly {@code null}. 134 */ 135 public Integer toInteger() { 136 if (typedSupplier != null) 137 return typedSupplier.get(); 138 return typedValue; 139 } 140 141 private static Integer toInteger(String value) { 142 if (ie(value)) 143 return null; 144 return parseInt(value, () -> iaex("Value '%s' could not be parsed as an integer.", value)); 145 } 146}