-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathHistory.java
More file actions
145 lines (131 loc) · 4.19 KB
/
Copy pathHistory.java
File metadata and controls
145 lines (131 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2016 Board of Regents of the University of
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
* Institute of Molecular Cell Biology and Genetics.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/
package org.scijava.script;
import org.scijava.prefs.PrefService;
import org.scijava.util.LastRecentlyUsed;
/**
* Container for a script language's interpreter history.
*
* @author Johannes Schindelin
*/
class History {
protected static final long serialVersionUID = 1L;
private static final String PREFIX = "History.";
private final int MAX_ENTRIES = 1000;
private final PrefService prefs;
private final String name;
private final LastRecentlyUsed<String> entries = new LastRecentlyUsed<>(MAX_ENTRIES);
private String currentCommand = "";
private int position = -1;
/**
* Constructs a history object for a given scripting language.
*
* @param name the name of the scripting language
*/
public History(final PrefService prefs, final String name) {
this.prefs = prefs;
this.name = name;
}
/**
* Read back a persisted history.
*/
public void read() {
entries.clear();
for (final String item : prefs.getIterable(getClass(), PREFIX + name)) {
entries.addToEnd(item);
}
}
/**
* Persist the history.
*
* @see PrefService
*/
public void write() {
prefs.putIterable(getClass(), entries, PREFIX + name);
}
/**
* Adds the most recently issued command.
*
* @param command the most recent command to add to the history
*/
public void add(final String command) {
entries.add(command);
position = -1;
currentCommand = "";
}
public boolean replace(final String command) {
if (position < 0) {
currentCommand = command;
return false;
}
return entries.replace(position, command);
}
/**
* Navigates to the next (more recent) command.
* <p>
* This method wraps around, i.e. it returns {@code null} when there is no
* more-recent command in the history.
* </p>
*
* @return the next command
*/
public String next() {
position = entries.next(position);
return position < 0 ? currentCommand : entries.get(position);
}
/**
* Navigates to the previous (i.e less recent) command.
* <p>
* This method wraps around, i.e. it returns {@code null} when there is no
* less-recent command in the history.
* </p>
*
* @return the previous command
*/
public String previous() {
position = entries.previous(position);
return position < 0 ? currentCommand : entries.get(position);
}
@Override
public String toString() {
final StringBuilder builder = new StringBuilder();
int pos = -1;
for (;;) {
pos = entries.previous(pos);
if (pos < 0) break;
if (builder.length() > 0) builder.append(" -> ");
if (this.position == pos) builder.append("[");
builder.append(entries.get(pos));
if (this.position == pos) builder.append("]");
}
return builder.toString();
}
}