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 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/JuneauRestCommonBasics">juneau-rest-common Basics</a> 032 * </ul> 033 */ 034public class BasicStringPart extends BasicPart { 035 /** 036 * Static creator. 037 * 038 * @param name The part name. 039 * @param value The part value. 040 * @return A new {@link BasicStringPart} object, or <jk>null</jk> if the name or value is <jk>null</jk>. 041 */ 042 public static BasicStringPart of(String name, String value) { 043 if (e(name) || value == null) 044 return null; 045 return new BasicStringPart(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 BasicStringPart} object, or <jk>null</jk> if the name or supplier is <jk>null</jk>. 057 */ 058 public static BasicStringPart of(String name, Supplier<String> value) { 059 if (e(name) || value == null) 060 return null; 061 return new BasicStringPart(name, value); 062 } 063 064 private final String value; 065 private final Supplier<String> 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 BasicStringPart(String name, String value) { 074 super(name, value); 075 this.value = value; 076 this.supplier = null; 077 } 078 079 /** 080 * Constructor. 081 * 082 * @param name The part name. Must not be <jk>null</jk>. 083 * @param value The part value supplier. Can be <jk>null</jk> or supply <jk>null</jk>. 084 */ 085 public BasicStringPart(String name, Supplier<String> value) { 086 super(name, value); 087 this.value = null; 088 supplier = value; 089 } 090 091 /** 092 * Provides the ability to perform fluent-style assertions on this part. 093 * 094 * @return A new fluent assertion object. 095 * @throws AssertionError If assertion failed. 096 */ 097 public FluentStringAssertion<BasicStringPart> assertString() { 098 return new FluentStringAssertion<>(value(), this); 099 } 100 101 /** 102 * Returns The part value as a {@link String} wrapped in an {@link Optional}. 103 * 104 * @return The part value as a {@link String} wrapped in an {@link Optional}. Never <jk>null</jk>. 105 */ 106 public Optional<String> asString() { 107 return opt(value()); 108 } 109 110 @Override /* Overridden from Header */ 111 public String getValue() { return value(); } 112 113 /** 114 * Return the value if present, otherwise return <c>other</c>. 115 * 116 * <p> 117 * This is a shortened form for calling <c>asString().orElse(<jv>other</jv>)</c>. 118 * 119 * @param other The value to be returned if there is no value present, can be <jk>null</jk>. 120 * @return The value, if present, otherwise <c>other</c>. 121 */ 122 public String orElse(String other) { 123 String x = value(); 124 return nn(x) ? x : other; 125 } 126 127 private String value() { 128 if (nn(supplier)) 129 return supplier.get(); 130 return value; 131 } 132}