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