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.internal; 014 015import static org.apache.juneau.common.internal.ThrowableUtils.*; 016 017import org.apache.juneau.common.utils.*; 018 019/** 020 * A simple settable boolean value. 021 */ 022public final class Flag { 023 024 private boolean value; 025 026 /** 027 * Creates a boolean value initialized to <jk>false</jk>. 028 * 029 * @return A new boolean value. 030 */ 031 public static Flag create() { 032 return of(false); 033 } 034 035 /** 036 * Creates a boolean value with the specified initial state. 037 * 038 * @param value The initial state of the value. 039 * @return A new boolean value. 040 */ 041 public static Flag of(boolean value) { 042 return new Flag(value); 043 } 044 045 private Flag(boolean value) { 046 this.value = value; 047 } 048 049 /** 050 * Runs a snippet of code if the boolean value is <jk>true</jk>. 051 * 052 * <h5 class='section'>Example:</h5> 053 * <p class='bcode'> 054 * BoolValue <jv>flag</jv> = BoolValue.<jsm>create</jsm>(); 055 * ... 056 * <jv>flag</jv>.ifSet(()-><jsm>doSomething</jsm>()); 057 * </p> 058 * 059 * @param snippet The snippet of code to run. 060 * @return This object. 061 */ 062 public Flag ifSet(Snippet snippet) { 063 if (value) 064 runSnippet(snippet); 065 return this; 066 } 067 068 /** 069 * Runs a snippet of code if the boolean value is <jk>false</jk>. 070 * 071 * <h5 class='section'>Example:</h5> 072 * <p class='bcode'> 073 * BoolValue <jv>flag</jv> = BoolValue.<jsm>create</jsm>(); 074 * ... 075 * <jv>flag</jv>.ifNotSet(()-><jsm>doSomething</jsm>()); 076 * </p> 077 * 078 * @param snippet The snippet of code to run. 079 * @return This object. 080 */ 081 public Flag ifNotSet(Snippet snippet) { 082 if (! value) 083 runSnippet(snippet); 084 return this; 085 } 086 087 private void runSnippet(Snippet snippet) { 088 try { 089 snippet.run(); 090 } catch (Error | RuntimeException e) { 091 throw e; 092 } catch (Throwable e) { 093 throw asRuntimeException(e); 094 } 095 } 096 097 /** 098 * Sets the boolean value to <jk>true</jk> and returns the value before it was set. 099 * 100 * @return The previous value. 101 */ 102 public boolean getAndSet() { 103 boolean b = value; 104 value = true; 105 return b; 106 } 107 108 /** 109 * Sets the boolean value to <jk>false</jk> and returns the value before it was set. 110 * 111 * @return The previous value. 112 */ 113 public boolean getAndUnset() { 114 boolean v = value; 115 value = false; 116 return v; 117 } 118 119 /** 120 * Sets the boolean value to <jk>true</jk>. 121 * 122 * @return This object. 123 */ 124 public Flag set() { 125 value = true; 126 return this; 127 } 128 129 /** 130 * Sets the boolean value to <jk>false</jk>. 131 * 132 * @return This object. 133 */ 134 public Flag unset() { 135 value = false; 136 return this; 137 } 138 139 /** 140 * Returns <jk>true</jk> if the boolean value is <jk>true</jk>. 141 * 142 * @return <jk>true</jk> if the boolean value is <jk>true</jk>. 143 */ 144 public boolean isSet() { 145 return value; 146 } 147 148 /** 149 * Returns <jk>true</jk> if the boolean value is <jk>false</jk>. 150 * 151 * @return <jk>true</jk> if the boolean value is <jk>false</jk>. 152 */ 153 public boolean isUnset() { 154 return ! value; 155 } 156 157 /** 158 * Sets the boolean value to <jk>true</jk> if the value is <jk>true</jk>. 159 * 160 * @param value The value to set. 161 * @return This object. 162 */ 163 public Flag setIf(boolean value) { 164 this.value |= value; 165 return this; 166 } 167}