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 e) { 091 throw e; 092 } catch (RuntimeException e) { 093 throw e; 094 } catch (Throwable e) { 095 throw asRuntimeException(e); 096 } 097 } 098 099 /** 100 * Sets the boolean value to <jk>true</jk> and returns the value before it was set. 101 * 102 * @return The previous value. 103 */ 104 public boolean getAndSet() { 105 boolean b = value; 106 value = true; 107 return b; 108 } 109 110 /** 111 * Sets the boolean value to <jk>false</jk> and returns the value before it was set. 112 * 113 * @return The previous value. 114 */ 115 public boolean getAndUnset() { 116 boolean v = value; 117 value = false; 118 return v; 119 } 120 121 /** 122 * Sets the boolean value to <jk>true</jk>. 123 * 124 * @return This object. 125 */ 126 public Flag set() { 127 value = true; 128 return this; 129 } 130 131 /** 132 * Sets the boolean value to <jk>false</jk>. 133 * 134 * @return This object. 135 */ 136 public Flag unset() { 137 value = false; 138 return this; 139 } 140 141 /** 142 * Returns <jk>true</jk> if the boolean value is <jk>true</jk>. 143 * 144 * @return <jk>true</jk> if the boolean value is <jk>true</jk>. 145 */ 146 public boolean isSet() { 147 return value; 148 } 149 150 /** 151 * Returns <jk>true</jk> if the boolean value is <jk>false</jk>. 152 * 153 * @return <jk>true</jk> if the boolean value is <jk>false</jk>. 154 */ 155 public boolean isUnset() { 156 return ! value; 157 } 158 159 /** 160 * Sets the boolean value to <jk>true</jk> if the value is <jk>true</jk>. 161 * 162 * @param value The value to set. 163 * @return This object. 164 */ 165 public Flag setIf(boolean value) { 166 this.value |= value; 167 return this; 168 } 169}