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