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 boolean 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 BasicBooleanPart 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 BasicBooleanPart} object, or <jk>null</jk> if the name or value is <jk>null</jk>.
043    */
044   public static BasicBooleanPart of(String name, Boolean value) {
045      if (isEmpty(name) || value == null)
046         return null;
047      return new BasicBooleanPart(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 BasicBooleanPart} object, or <jk>null</jk> if the name or supplier is <jk>null</jk>.
059    */
060   public static BasicBooleanPart of(String name, Supplier<Boolean> value) {
061      if (isEmpty(name) || value == null)
062         return null;
063      return new BasicBooleanPart(name, value);
064   }
065
066   //-----------------------------------------------------------------------------------------------------------------
067   // Instance
068   //-----------------------------------------------------------------------------------------------------------------
069
070   private final Boolean value;
071   private final Supplier<Boolean> 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 BasicBooleanPart(String name, Boolean 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 BasicBooleanPart(String name, Supplier<Boolean> 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 Boolean#valueOf(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 BasicBooleanPart(String name, String value) {
108      super(name, value);
109      this.value = isEmpty(value) ? null : Boolean.valueOf(value);
110      this.supplier = null;
111   }
112
113   @Override /* NameValuePair */
114   public String getValue() {
115      return stringify(value());
116   }
117
118   /**
119    * Returns The part value as a {@link Boolean} wrapped in an {@link Optional}.
120    *
121    * @return The part value as a {@link Boolean} wrapped in an {@link Optional}.  Never <jk>null</jk>.
122    */
123   public Optional<Boolean> asBoolean() {
124      return optional(toBoolean());
125   }
126
127   /**
128    * Returns The part value as a {@link Boolean}.
129    *
130    * @return The part value as a {@link Boolean}, or <jk>null</jk> if the value <jk>null</jk>.
131    */
132   public Boolean toBoolean() {
133      return value();
134   }
135
136   /**
137    * Provides the ability to perform fluent-style assertions on this part.
138    *
139    * @return A new fluent assertion object.
140    * @throws AssertionError If assertion failed.
141    */
142   public FluentBooleanAssertion<BasicBooleanPart> assertBoolean() {
143      return new FluentBooleanAssertion<>(value(), this);
144   }
145
146   /**
147    * Return the value if present, otherwise return <c>other</c>.
148    *
149    * <p>
150    * This is a shortened form for calling <c>asBoolean().orElse(<jv>other</jv>)</c>.
151    *
152    * @param other The value to be returned if there is no value present, can be <jk>null</jk>.
153    * @return The value, if present, otherwise <c>other</c>.
154    */
155   public Boolean orElse(Boolean other) {
156      Boolean x = value();
157      return x != null ? x : other;
158   }
159
160   private Boolean value() {
161      if (supplier != null)
162         return supplier.get();
163      return value;
164   }
165}