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.classic.part; 018 019import static org.apache.juneau.commons.utils.Shorts.*; 020 021import java.util.*; 022import java.util.function.*; 023 024import org.apache.http.*; 025import org.apache.juneau.bct.assertions.*; 026 027/** 028 * A {@link NameValuePair} that consists of a single string value. 029 * 030 * <h5 class='section'>See Also:</h5><ul> 031 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/JuneauRestCommon">juneau-rest-common</a> 032 * </ul> 033 */ 034@SuppressWarnings({ 035 "java:S2160" // equals() inherited from BasicPart compares name+value; typed String field is accessed via getValue() 036}) 037public class BasicStringPart extends BasicPart { 038 039 /** 040 * Static creator. 041 * 042 * @param name The part name. 043 * @param value The part value. 044 * @return A new {@link BasicStringPart} object, or <jk>null</jk> if the name or value is <jk>null</jk>. 045 */ 046 public static BasicStringPart of(String name, String value) { 047 if (ie(name) || value == null) 048 return null; 049 return new BasicStringPart(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 BasicStringPart} object, or <jk>null</jk> if the name or supplier is <jk>null</jk>. 061 */ 062 public static BasicStringPart of(String name, Supplier<String> value) { 063 if (ie(name) || value == null) 064 return null; 065 return new BasicStringPart(name, value); 066 } 067 068 private final String value; 069 private final Supplier<String> supplier; 070 071 /** 072 * Constructor. 073 * 074 * @param name The part name. Must not be <jk>null</jk>. 075 * @param value The part value. Can be <jk>null</jk>. 076 */ 077 public BasicStringPart(String name, String value) { 078 super(name, value); 079 this.value = value; 080 this.supplier = null; 081 } 082 083 /** 084 * Constructor. 085 * 086 * @param name The part name. Must not be <jk>null</jk>. 087 * @param value The part value supplier. Can be <jk>null</jk> or supply <jk>null</jk>. 088 */ 089 public BasicStringPart(String name, Supplier<String> value) { 090 super(name, value); 091 this.value = null; 092 supplier = value; 093 } 094 095 /** 096 * Provides the ability to perform fluent-style assertions on this part. 097 * 098 * @return A new fluent assertion object. 099 * @throws AssertionError If assertion failed. 100 */ 101 public FluentStringAssertion<BasicStringPart> assertString() { 102 return new FluentStringAssertion<>(value(), this); 103 } 104 105 /** 106 * Returns The part value as a {@link String} wrapped in an {@link Optional}. 107 * 108 * @return The part value as a {@link String} wrapped in an {@link Optional}. Never <jk>null</jk>. 109 */ 110 public Optional<String> asString() { 111 return o(value()); 112 } 113 114 @Override /* Overridden from Header */ 115 public String getValue() { return value(); } 116 117 /** 118 * Return the value if present, otherwise return <c>other</c>. 119 * 120 * <p> 121 * This is a shortened form for calling <c>asString().orElse(<jv>other</jv>)</c>. 122 * 123 * @param other The value to be returned if there is no value present, can be <jk>null</jk>. 124 * @return The value, if present, otherwise <c>other</c>. 125 */ 126 public String orElse(String other) { 127 String x = value(); 128 return nn(x) ? x : other; 129 } 130 131 private String value() { 132 if (nn(supplier)) 133 return supplier.get(); 134 return value; 135 } 136}