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.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/JuneauRestCommonBasics">juneau-rest-common Basics</a>
032 * </ul>
033 */
034public class BasicUriPart extends BasicPart {
035   /**
036    * Static creator with delayed value.
037    *
038    * <p>
039    * Part value is re-evaluated on each call to {@link NameValuePair#getValue()}.
040    *
041    * @param name The part name.
042    * @param value The part value supplier.
043    * @return A new {@link BasicUriPart} object, or <jk>null</jk> if the name or supplier is <jk>null</jk>.
044    */
045   public static BasicUriPart of(String name, Supplier<URI> value) {
046      if (e(name) || value == null)
047         return null;
048      return new BasicUriPart(name, value);
049   }
050
051   /**
052    * Static creator.
053    *
054    * @param name The part name.
055    * @param value The part value.
056    * @return A new {@link BasicUriPart} object, or <jk>null</jk> if the name or value is <jk>null</jk>.
057    */
058   public static BasicUriPart of(String name, URI value) {
059      if (e(name) || value == null)
060         return null;
061      return new BasicUriPart(name, value);
062   }
063
064   private final URI value;
065   private final Supplier<URI> supplier;
066
067   /**
068    * Constructor.
069    *
070    * <p>
071    * <jk>null</jk> and empty values are treated as <jk>null</jk>.
072    * Otherwise parses using {@link URI#create(String)}.
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 BasicUriPart(String name, String value) {
078      super(name, value);
079      this.value = e(value) ? null : URI.create(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 BasicUriPart(String name, Supplier<URI> value) {
090      super(name, value);
091      this.value = null;
092      supplier = value;
093   }
094
095   /**
096    * Constructor.
097    *
098    * @param name The part name.  Must not be <jk>null</jk>.
099    * @param value The part value.  Can be <jk>null</jk>.
100    */
101   public BasicUriPart(String name, URI value) {
102      super(name, value);
103      this.value = value;
104      this.supplier = null;
105   }
106
107   /**
108    * Returns The part value as a {@link URI} wrapped in an {@link Optional}.
109    *
110    * @return The part value as a {@link URI} wrapped in an {@link Optional}.  Never <jk>null</jk>.
111    */
112   public Optional<URI> asUri() {
113      return opt(value());
114   }
115
116   /**
117    * Return the value if present, otherwise return <c>other</c>.
118    *
119    * <p>
120    * This is a shortened form for calling <c>asString().orElse(<jv>other</jv>)</c>.
121    *
122    * @param other The value to be returned if there is no value present, can be <jk>null</jk>.
123    * @return The value, if present, otherwise <c>other</c>.
124    */
125   public URI orElse(URI other) {
126      URI x = value();
127      return nn(x) ? x : other;
128   }
129
130   /**
131    * Returns The part value as a {@link URI}.
132    *
133    * @return The part value as a {@link URI}, or <jk>null</jk> if the value <jk>null</jk>.
134    */
135   public URI toUri() {
136      return value();
137   }
138
139   private URI value() {
140      if (nn(supplier))
141         return supplier.get();
142      return value;
143   }
144}