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.net.*;
019import java.util.*;
020import java.util.function.*;
021
022import org.apache.http.*;
023
024/**
025 * A {@link NameValuePair} that consists of a single URL 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 BasicUriPart 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 BasicUriPart} object, or <jk>null</jk> if the name or value is <jk>null</jk>.
043    */
044   public static BasicUriPart of(String name, URI value) {
045      if (isEmpty(name) || value == null)
046         return null;
047      return new BasicUriPart(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 BasicUriPart} object, or <jk>null</jk> if the name or supplier is <jk>null</jk>.
059    */
060   public static BasicUriPart of(String name, Supplier<URI> value) {
061      if (isEmpty(name) || value == null)
062         return null;
063      return new BasicUriPart(name, value);
064   }
065
066   //-----------------------------------------------------------------------------------------------------------------
067   // Instance
068   //-----------------------------------------------------------------------------------------------------------------
069
070   private final URI value;
071   private final Supplier<URI> 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 BasicUriPart(String name, URI 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 BasicUriPart(String name, Supplier<URI> value) {
092      super(name, value);
093      this.value = null;
094      this.supplier = value;
095   }
096
097   /**
098    * Constructor.
099    *
100    * <p>
101    * <jk>null</jk> and empty values are treated as <jk>null</jk>.
102    * Otherwise parses using {@link URI#create(String)}.
103    *
104    * @param name The part name.  Must not be <jk>null</jk>.
105    * @param value The part value.  Can be <jk>null</jk>.
106    */
107   public BasicUriPart(String name, String value) {
108      super(name, value);
109      this.value = isEmpty(value) ? null : URI.create(value);
110      this.supplier = null;
111   }
112
113   /**
114    * Returns The part value as a {@link URI} wrapped in an {@link Optional}.
115    *
116    * @return The part value as a {@link URI} wrapped in an {@link Optional}.  Never <jk>null</jk>.
117    */
118   public Optional<URI> asUri() {
119      return optional(value());
120   }
121
122   /**
123    * Returns The part value as a {@link URI}.
124    *
125    * @return The part value as a {@link URI}, or <jk>null</jk> if the value <jk>null</jk>.
126    */
127   public URI toUri() {
128      return value();
129   }
130
131   /**
132    * Return the value if present, otherwise return <c>other</c>.
133    *
134    * <p>
135    * This is a shortened form for calling <c>asString().orElse(<jv>other</jv>)</c>.
136    *
137    * @param other The value to be returned if there is no value present, can be <jk>null</jk>.
138    * @return The value, if present, otherwise <c>other</c>.
139    */
140   public URI orElse(URI other) {
141      URI x = value();
142      return x != null ? x : other;
143   }
144
145   private URI value() {
146      if (supplier != null)
147         return supplier.get();
148      return value;
149   }
150}