Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions src/main/java/org/scijava/prefs/DefaultPrefService.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,28 @@ public List<String> getList(final Class<?> prefClass) {
return getList(preferences);
}

@Override
public Iterable<String> getIterable(final String key) {
return getIterable((Class<?>) null, key);
}

@Override
public Iterable<String> getIterable(final Class<?> prefClass, final String key) {
final Preferences preferences = prefs(prefClass);
return getIterable(preferences.node(key));
}

@Override
public void putIterable(final Iterable<String> iterable, final String key) {
putIterable((Class<?>) null, iterable, key);
}

@Override
public void putIterable(final Class<?> prefClass, final Iterable<String> iterable, final String key) {
final Preferences preferences = prefs(prefClass);
putIterable(preferences.node(key), iterable);
}

// -- Helper methods --

private void clear(final Preferences preferences, final String key) {
Expand Down Expand Up @@ -460,6 +482,60 @@ private List<String> getList(final Preferences preferences) {
return list;
}

private void putIterable(final Preferences preferences,
final Iterable<String> iterable)
{
if (preferences == null) {
throw new IllegalArgumentException("Preferences not set.");
}
int index = 0;
for (final String value : iterable) {
preferences.put("" + index++, value == null ? null : value.toString());
}
}

private Iterable<String> getIterable(final Preferences preferences)
{
if (preferences == null) {
throw new IllegalArgumentException("Preferences not set.");
}
return new Iterable<String>() {
@Override
public Iterator<String> iterator() {
return new Iterator<String>() {
private String value;
private int index;
{
findNext();
}

@Override
public String next() {
final String result = value;
findNext();
return result;
}

@Override
public boolean hasNext() {
return value != null;
}

@Override
public void remove() {
throw new UnsupportedOperationException();
}

private void findNext() {
if (index < 0) return;
value = preferences.get("" + index, null);
index = value == null ? -1 : index + 1;
}
};
}
};
}

private Preferences prefs(final Class<?> c) {
return Preferences.userNodeForPackage(c == null ? PrefService.class : c);
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/scijava/prefs/PrefService.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,24 @@ public interface PrefService extends SciJavaService {
* prefs.
*/
List<String> getList(Class<?> prefClass);

/**
* Puts an iterable into the preferences.
*/
void putIterable(Iterable<String> iterable, String key);

/**
* Puts an iterable into the preferences.
*/
void putIterable(Class<?> prefClass, Iterable<String> iterable, String key);

/**
* Gets an iterable from the preferences.
*/
Iterable<String> getIterable(String key);

/**
* Gets an iterable from the preferences.
*/
Iterable<String> getIterable(Class<?> prefClass, String key);
}
102 changes: 102 additions & 0 deletions src/main/java/org/scijava/script/DefaultScriptInterpreter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2014 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 javax.script.ScriptEngine;
import javax.script.ScriptException;

import org.scijava.prefs.PrefService;

/**
* The default implementation of a {@link ScriptInterpreter}.
*
* @author Johannes Schindelin
*/
public class DefaultScriptInterpreter implements ScriptInterpreter {

private final ScriptLanguage language;
private final ScriptEngine engine;
private final History history;

/**
* Constructs a new {@link DefaultScriptInterpreter}.
*
* @param scriptService the script service
* @param language the script language
*/
public DefaultScriptInterpreter(final PrefService prefs,
final ScriptService scriptService, final ScriptLanguage language)
{
this.language = language;
engine = language.getScriptEngine();
history = new History(prefs, engine.getClass().getName());
readHistory();
}

@Override
public synchronized void readHistory() {
if (history == null) return;
history.read();
}

@Override
public synchronized void writeHistory() {
if (history == null) return;
history.write();
}

@Override
public synchronized String walkHistory(final String currentCommand,
final boolean forward)
{
if (history == null) return currentCommand;
history.replace(currentCommand);
return forward ? history.next() : history.previous();
}

@Override
public void eval(final String command) throws ScriptException {
if (history != null) history.add(command);
if (engine == null) throw new java.lang.IllegalArgumentException();
engine.eval(command);
}

@Override
public ScriptLanguage getLanguage() {
return language;
}

@Override
public ScriptEngine getEngine() {
return engine;
}

}
145 changes: 145 additions & 0 deletions src/main/java/org/scijava/script/History.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2014 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<String>(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 {@link Prefs}
*/
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 currentCommand) {
if (position < 0) {
this.currentCommand = currentCommand;
return false;
}
return entries.replace(position, currentCommand);
}

/**
* 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 position = -1;
for (;;) {
position = entries.previous(position);
if (position < 0) break;
if (builder.length() > 0) builder.append(" -> ");
if (this.position == position) builder.append("[");
builder.append(entries.get(position));
if (this.position == position) builder.append("]");
}
return builder.toString();
}
}
Loading