Autopsy  3.1
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.Objects;
22 import javafx.beans.property.Property;
23 import javafx.beans.property.ReadOnlyBooleanProperty;
24 import javafx.beans.property.ReadOnlyBooleanWrapper;
25 import javafx.beans.property.ReadOnlyObjectProperty;
26 import javafx.beans.property.ReadOnlyObjectWrapper;
27 import javafx.beans.property.SimpleListProperty;
28 import javafx.collections.FXCollections;
29 import javax.annotation.concurrent.GuardedBy;
30 import javax.annotation.concurrent.ThreadSafe;
31 
42 @ThreadSafe
43 public class History<T> {
44 
45  @GuardedBy("this")
46  private final ObservableStack<T> historyStack = new ObservableStack<>();
47 
48  @GuardedBy("this")
49  private final ObservableStack<T> forwardStack = new ObservableStack<>();
50 
51  @GuardedBy("this")
52  private final ReadOnlyObjectWrapper<T> currentState = new ReadOnlyObjectWrapper<>();
53 
54  @GuardedBy("this")
55  private final ReadOnlyBooleanWrapper canAdvance = new ReadOnlyBooleanWrapper();
56 
57  @GuardedBy("this")
58  private final ReadOnlyBooleanWrapper canRetreat = new ReadOnlyBooleanWrapper();
59 
60  synchronized public T getCurrentState() {
61  return currentState.get();
62  }
63 
64  synchronized public boolean canAdvance() {
65  return canAdvance.get();
66  }
67 
68  synchronized public boolean canRetreat() {
69  return canRetreat.get();
70  }
71 
72  synchronized public ReadOnlyObjectProperty<T> currentState() {
73  return currentState.getReadOnlyProperty();
74  }
75 
76  synchronized public ReadOnlyBooleanProperty getCanAdvance() {
77  return canAdvance.getReadOnlyProperty();
78  }
79 
80  synchronized public ReadOnlyBooleanProperty getCanRetreat() {
81  return canRetreat.getReadOnlyProperty();
82  }
83 
84  public History(T initialState) {
85  this();
86  currentState.set(initialState);
87  }
88 
89  public History() {
90  canAdvance.bind(forwardStack.emptyProperty().not());
91  canRetreat.bind(historyStack.emptyProperty().not());
92  }
93 
100  synchronized public T advance() {
101  final T peek = forwardStack.peek();
102 
103  if (peek != null && peek.equals(currentState.get()) == false) {
104  historyStack.push(currentState.get());
105  currentState.set(peek);
106  forwardStack.pop();
107  }
108  return peek;
109  }
110 
117  synchronized public T retreat() {
118  final T pop = historyStack.pop();
119 
120  if (pop != null && pop.equals(currentState.get()) == false) {
121  forwardStack.push(currentState.get());
122  currentState.set(pop);
123  return pop;
124  } else if (pop != null && pop.equals(currentState.get())) {
125  return retreat();
126  }
127  return pop;
128  }
129 
139  synchronized public void advance(T newState) throws IllegalArgumentException {
140 
141  if (newState != null && Objects.equals(currentState.get(), newState) == false) {
142  if (currentState.get() != null) {
143  historyStack.push(currentState.get());
144  }
145  currentState.set(newState);
146  if (newState.equals(forwardStack.peek())) {
147  forwardStack.pop();
148  } else {
149  forwardStack.clear();
150  }
151  }
152  }
153 
154  synchronized public void clear() {
155  historyStack.clear();
156  forwardStack.clear();
157  currentState.set(null);
158  }
159 
167  private static class ObservableStack<T> extends SimpleListProperty<T> {
168 
169  public ObservableStack() {
170  super(FXCollections.<T>synchronizedObservableList(FXCollections.<T>observableArrayList()));
171  }
172 
173  public void push(T item) {
174  synchronized (this) {
175  add(0, item);
176  }
177  }
178 
179  public T pop() {
180  synchronized (this) {
181  if (isEmpty()) {
182  return null;
183  } else {
184  return remove(0);
185  }
186  }
187  }
188 
189  public T peek() {
190  synchronized (this) {
191  if (isEmpty()) {
192  return null;
193  } else {
194  return get(0);
195  }
196  }
197  }
198  }
199 }
final ObservableStack< T > forwardStack
Definition: History.java:49
final ObservableStack< T > historyStack
Definition: History.java:46
synchronized boolean canRetreat()
Definition: History.java:68
synchronized boolean canAdvance()
Definition: History.java:64
synchronized ReadOnlyObjectProperty< T > currentState()
Definition: History.java:72
synchronized ReadOnlyBooleanProperty getCanRetreat()
Definition: History.java:80
synchronized void advance(T newState)
Definition: History.java:139
synchronized ReadOnlyBooleanProperty getCanAdvance()
Definition: History.java:76

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.