001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.http.part; 014 015import static org.apache.juneau.common.internal.StringUtils.*; 016import static org.apache.juneau.internal.CollectionUtils.*; 017 018import java.util.*; 019import java.util.function.*; 020 021import org.apache.http.*; 022import org.apache.juneau.assertions.*; 023 024/** 025 * A {@link NameValuePair} that consists of a single string value. 026 * 027 * <h5 class='section'>See Also:</h5><ul> 028 * <li class='link'><a class="doclink" href="../../../../../index.html#juneau-rest-common">juneau-rest-common</a> 029 * </ul> 030 */ 031public class BasicStringPart extends BasicPart { 032 033 //----------------------------------------------------------------------------------------------------------------- 034 // Static 035 //----------------------------------------------------------------------------------------------------------------- 036 037 /** 038 * Static creator. 039 * 040 * @param name The part name. 041 * @param value The part value. 042 * @return A new {@link BasicStringPart} object, or <jk>null</jk> if the name or value is <jk>null</jk>. 043 */ 044 public static BasicStringPart of(String name, String value) { 045 if (isEmpty(name) || value == null) 046 return null; 047 return new BasicStringPart(name, value); 048 } 049 050 /** 051 * Static creator with delayed value. 052 * 053 * <p> 054 * Part value is re-evaluated on each call to {@link NameValuePair#getValue()}. 055 * 056 * @param name The part name. 057 * @param value The part value supplier. 058 * @return A new {@link BasicStringPart} object, or <jk>null</jk> if the name or supplier is <jk>null</jk>. 059 */ 060 public static BasicStringPart of(String name, Supplier<String> value) { 061 if (isEmpty(name) || value == null) 062 return null; 063 return new BasicStringPart(name, value); 064 } 065 066 //----------------------------------------------------------------------------------------------------------------- 067 // Instance 068 //----------------------------------------------------------------------------------------------------------------- 069 070 private final String value; 071 private final Supplier<String> supplier; 072 073 /** 074 * Constructor. 075 * 076 * @param name The part name. Must not be <jk>null</jk>. 077 * @param value The part value. Can be <jk>null</jk>. 078 */ 079 public BasicStringPart(String name, String value) { 080 super(name, value); 081 this.value = value; 082 this.supplier = null; 083 } 084 085 /** 086 * Constructor. 087 * 088 * @param name The part name. Must not be <jk>null</jk>. 089 * @param value The part value supplier. Can be <jk>null</jk> or supply <jk>null</jk>. 090 */ 091 public BasicStringPart(String name, Supplier<String> value) { 092 super(name, value); 093 this.value = null; 094 this.supplier = value; 095 } 096 097 /** 098 * Provides the ability to perform fluent-style assertions on this part. 099 * 100 * @return A new fluent assertion object. 101 * @throws AssertionError If assertion failed. 102 */ 103 public FluentStringAssertion<BasicStringPart> assertString() { 104 return new FluentStringAssertion<>(value(), this); 105 } 106 107 @Override /* Header */ 108 public String getValue() { 109 return value(); 110 } 111 112 /** 113 * Returns The part value as a {@link String} wrapped in an {@link Optional}. 114 * 115 * @return The part value as a {@link String} wrapped in an {@link Optional}. Never <jk>null</jk>. 116 */ 117 public Optional<String> asString() { 118 return optional(value()); 119 } 120 121 /** 122 * Return the value if present, otherwise return <c>other</c>. 123 * 124 * <p> 125 * This is a shortened form for calling <c>asString().orElse(<jv>other</jv>)</c>. 126 * 127 * @param other The value to be returned if there is no value present, can be <jk>null</jk>. 128 * @return The value, if present, otherwise <c>other</c>. 129 */ 130 public String orElse(String other) { 131 String x = value(); 132 return x != null ? x : other; 133 } 134 135 private String value() { 136 if (supplier != null) 137 return supplier.get(); 138 return value; 139 } 140}