Autopsy  4.7.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
History.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2014 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.coreutils;
20 
21 import java.util.Deque;
22 import java.util.Objects;
23 import javafx.beans.property.Property;
24 import javafx.beans.property.ReadOnlyBooleanProperty;
25 import javafx.beans.property.ReadOnlyBooleanWrapper;
26 import javafx.beans.property.ReadOnlyObjectProperty;
27 import javafx.beans.property.ReadOnlyObjectWrapper;
28 import javafx.beans.property.SimpleListProperty;
29 import javafx.collections.FXCollections;
30 import javax.annotation.concurrent.GuardedBy;
31 import javax.annotation.concurrent.ThreadSafe;
32 
43 @ThreadSafe
44 public class History<T> {
45 
46  @GuardedBy("this")
47  private final ObservableStack<T> historyStack = new ObservableStack<>();
48 
49  @GuardedBy("this")
50  private final ObservableStack<T> forwardStack = new ObservableStack<>();
51 
52  @GuardedBy("this")
53  private final ReadOnlyObjectWrapper<T> currentState = new ReadOnlyObjectWrapper<>();
54 
55  @GuardedBy("this")
56  private final ReadOnlyBooleanWrapper canAdvance = new ReadOnlyBooleanWrapper();
57 
58  @GuardedBy("this")
59  private final ReadOnlyBooleanWrapper canRetreat = new ReadOnlyBooleanWrapper();
60 
61  synchronized public T getCurrentState() {
62  return currentState.get();
63  }
64 
65  synchronized public boolean canAdvance() {
66  return canAdvance.get();
67  }
68 
69  synchronized public boolean canRetreat() {
70  return canRetreat.get();
71  }
72 
73  synchronized public ReadOnlyObjectProperty<T> currentState() {
74  return currentState.getReadOnlyProperty();
75  }
76 
77  synchronized public ReadOnlyBooleanProperty getCanAdvance() {
78  return canAdvance.getReadOnlyProperty();
79  }
80 
81  synchronized public ReadOnlyBooleanProperty getCanRetreat() {
82  return canRetreat.getReadOnlyProperty();
83  }
84 
85  public History(T initialState) {
86  this();
87  currentState.set(initialState);
88  }
89 
90  public History() {
91  canAdvance.bind(forwardStack.emptyProperty().not());
92  canRetreat.bind(historyStack.emptyProperty().not());
93  }
94 
95  synchronized public void reset(T newState) {
96  forwardStack.clear();
97  historyStack.clear();
98  currentState.set(newState);
99  }
100 
107  synchronized public T advance() {
108  final T peek = forwardStack.peek();
109 
110  if (peek != null && peek.equals(currentState.get()) == false) {
111  historyStack.push(currentState.get());
112  currentState.set(peek);
113  forwardStack.pop();
114  }
115  return peek;
116  }
117 
124  synchronized public T retreat() {
125  final T pop = historyStack.pop();
126 
127  if (pop != null && pop.equals(currentState.get()) == false) {
128  forwardStack.push(currentState.get());
129  currentState.set(pop);
130  return pop;
131  } else if (pop != null && pop.equals(currentState.get())) {
132  return retreat();
133  }
134  return pop;
135  }
136 
146  synchronized public void advance(T newState) throws IllegalArgumentException {
147  if (newState != null && Objects.equals(currentState.get(), newState) == false) {
148  if (currentState.get() != null) {
149  historyStack.push(currentState.get());
150  }
151  currentState.set(newState);
152  if (newState.equals(forwardStack.peek())) {
153  forwardStack.pop();
154  } else {
155  forwardStack.clear();
156  }
157  }
158  }
159 
160  synchronized public void clear() {
161  historyStack.clear();
162  forwardStack.clear();
163  currentState.set(null);
164  }
165 
173  private static class ObservableStack<T> extends SimpleListProperty<T> {
174 
175  public ObservableStack() {
176  super(FXCollections.<T>synchronizedObservableList(FXCollections.<T>observableArrayList()));
177  }
178 
179  public void push(T item) {
180  synchronized (this) {
181  add(0, item);
182  }
183  }
184 
185  public T pop() {
186  synchronized (this) {
187  if (isEmpty()) {
188  return null;
189  } else {
190  return remove(0);
191  }
192  }
193  }
194 
195  public T peek() {
196  synchronized (this) {
197  if (isEmpty()) {
198  return null;
199  } else {
200  return get(0);
201  }
202  }
203  }
204  }
205 }
final ObservableStack< T > forwardStack
Definition: History.java:50
final ObservableStack< T > historyStack
Definition: History.java:47
synchronized boolean canRetreat()
Definition: History.java:69
synchronized void reset(T newState)
Definition: History.java:95
synchronized boolean canAdvance()
Definition: History.java:65
synchronized ReadOnlyObjectProperty< T > currentState()
Definition: History.java:73
synchronized ReadOnlyBooleanProperty getCanRetreat()
Definition: History.java:81
synchronized void advance(T newState)
Definition: History.java:146
synchronized ReadOnlyBooleanProperty getCanAdvance()
Definition: History.java:77

Copyright © 2012-2016 Basis Technology. Generated on: Mon Jun 18 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.