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.net.*; 022import java.util.*; 023import java.util.function.*; 024 025import org.apache.http.*; 026 027/** 028 * A {@link NameValuePair} that consists of a single URL 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 URI field is accessed via getValue() 036}) 037public class BasicUriPart extends BasicPart { 038 039 /** 040 * Static creator with delayed value. 041 * 042 * <p> 043 * Part value is re-evaluated on each call to {@link NameValuePair#getValue()}. 044 * 045 * @param name The part name. 046 * @param value The part value supplier. 047 * @return A new {@link BasicUriPart} object, or <jk>null</jk> if the name or supplier is <jk>null</jk>. 048 */ 049 public static BasicUriPart of(String name, Supplier<URI> value) { 050 if (ie(name) || value == null) 051 return null; 052 return new BasicUriPart(name, value); 053 } 054 055 /** 056 * Static creator. 057 * 058 * @param name The part name. 059 * @param value The part value. 060 * @return A new {@link BasicUriPart} object, or <jk>null</jk> if the name or value is <jk>null</jk>. 061 */ 062 public static BasicUriPart of(String name, URI value) { 063 if (ie(name) || value == null) 064 return null; 065 return new BasicUriPart(name, value); 066 } 067 068 private final URI value; 069 private final Supplier<URI> supplier; 070 071 /** 072 * Constructor. 073 * 074 * <p> 075 * <jk>null</jk> and empty values are treated as <jk>null</jk>. 076 * Otherwise parses using {@link URI#create(String)}. 077 * 078 * @param name The part name. Must not be <jk>null</jk>. 079 * @param value The part value. Can be <jk>null</jk>. 080 */ 081 public BasicUriPart(String name, String value) { 082 super(name, value); 083 this.value = ie(value) ? null : URI.create(value); 084 this.supplier = null; 085 } 086 087 /** 088 * Constructor. 089 * 090 * @param name The part name. Must not be <jk>null</jk>. 091 * @param value The part value supplier. Can be <jk>null</jk> or supply <jk>null</jk>. 092 */ 093 public BasicUriPart(String name, Supplier<URI> value) { 094 super(name, value); 095 this.value = null; 096 supplier = value; 097 } 098 099 /** 100 * Constructor. 101 * 102 * @param name The part name. Must not be <jk>null</jk>. 103 * @param value The part value. Can be <jk>null</jk>. 104 */ 105 public BasicUriPart(String name, URI value) { 106 super(name, value); 107 this.value = value; 108 this.supplier = null; 109 } 110 111 /** 112 * Returns The part value as a {@link URI} wrapped in an {@link Optional}. 113 * 114 * @return The part value as a {@link URI} wrapped in an {@link Optional}. Never <jk>null</jk>. 115 */ 116 public Optional<URI> asUri() { 117 return o(value()); 118 } 119 120 /** 121 * Return the value if present, otherwise return <c>other</c>. 122 * 123 * <p> 124 * This is a shortened form for calling <c>asString().orElse(<jv>other</jv>)</c>. 125 * 126 * @param other The value to be returned if there is no value present, can be <jk>null</jk>. 127 * @return The value, if present, otherwise <c>other</c>. 128 */ 129 public URI orElse(URI other) { 130 URI x = value(); 131 return nn(x) ? x : other; 132 } 133 134 /** 135 * Returns The part value as a {@link URI}. 136 * 137 * @return The part value as a {@link URI}, or <jk>null</jk> if the value <jk>null</jk>. 138 */ 139 public URI toUri() { 140 return value(); 141 } 142 143 private URI value() { 144 if (nn(supplier)) 145 return supplier.get(); 146 return value; 147 } 148}