2 * Copyright (C) 2017 EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 package org
.lttng
.scope
.tmf2
.views
.core
;
12 import java
.util
.concurrent
.atomic
.AtomicInteger
;
14 import javafx
.beans
.property
.BooleanProperty
;
15 import javafx
.beans
.property
.ReadOnlyBooleanProperty
;
16 import javafx
.beans
.property
.SimpleBooleanProperty
;
17 import javafx
.beans
.value
.ChangeListener
;
20 * Utility class that serves as a wrapper around a single boolean flag that can
21 * be enabled/disabled. It counts the number of times {@link #disable()} is
22 * called, and only really re-enables the inner value when {@link #enable()} is
23 * called that many times.
25 * It is meant to be useful in multi-thread scenarios, where concurrent
26 * "critical sections" may want to disable something like a listener, and not
27 * have it really be re-enabled until all critical sections are finished. Thus
30 * The inner value is exposed through the {@link #enabledProperty()} method, which
31 * returns a {@link ReadOnlyBooleanProperty}. You can attach
32 * {@link ChangeListener}s to that property to get notified of inner value
35 * It is "enabled" at creation time.
37 * @author Alexandre Montplaisir
39 public class NestingBoolean
{
41 private final AtomicInteger fDisabledCount
= new AtomicInteger(0);
42 private final BooleanProperty fBoolean
= new SimpleBooleanProperty(true);
45 * Decrease the "disabled" count by 1. If it reaches (or already was at) 0
46 * then the value is truly enabled.
48 public synchronized void enable() {
49 /* Decrement the count but only if it is currently above 0 */
50 int ret
= fDisabledCount
.updateAndGet(value
-> value
> 0 ? value
- 1 : 0);
57 * Increase the "disabled" count by 1. The inner value will necessarily be
58 * disabled after this call.
60 public synchronized void disable() {
61 fDisabledCount
.incrementAndGet();
66 * Property representing the inner boolean value.
68 * @return The inner value
70 public ReadOnlyBooleanProperty
enabledProperty() {