Skip to content

Commit c9992de

Browse files
committed
Add ItemPersistence to denote persistence behavior
Introduces the ItemPersistence enum (YES, NO, DEFAULT). YES and NO overwrite additional checks (i.e. precedence of an initializer, value is default value). In case of DEFAULT, the value is persisted only if it is not the default value and no initializer is set.
1 parent 5b20d97 commit c9992de

10 files changed

+102
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2016 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava;
33+
34+
import org.scijava.plugin.Parameter;
35+
36+
/**
37+
* Defines the persistence setting of a parameter.
38+
*
39+
* @author Stefan Helfrich
40+
*/
41+
public enum ItemPersistence {
42+
43+
/**
44+
* Item is persisted in any case.
45+
*/
46+
YES,
47+
48+
/**
49+
* Item is never persisted.
50+
*/
51+
NO,
52+
53+
/**
54+
* Item is persisted unless an additional {@link Parameter#initializer()
55+
* initializer()} method is defined or the value to persist is the defined
56+
* default value.
57+
*/
58+
DEFAULT
59+
60+
}

src/main/java/org/scijava/command/CommandModuleItem.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.List;
3838

3939
import org.scijava.ItemIO;
40+
import org.scijava.ItemPersistence;
4041
import org.scijava.ItemVisibility;
4142
import org.scijava.Optional;
4243
import org.scijava.module.AbstractModuleItem;
@@ -109,7 +110,7 @@ public boolean isRequired() {
109110
}
110111

111112
@Override
112-
public boolean isPersisted() {
113+
public ItemPersistence getPersistence() {
113114
return getParameter().persist();
114115
}
115116

src/main/java/org/scijava/module/AbstractModuleItem.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
import org.scijava.AbstractBasicDetails;
3939
import org.scijava.ItemIO;
40+
import org.scijava.ItemPersistence;
4041
import org.scijava.ItemVisibility;
4142
import org.scijava.util.ClassUtils;
4243
import org.scijava.util.ConversionUtils;
@@ -71,7 +72,7 @@ public String toString() {
7172
sm.append("description", getDescription());
7273
sm.append("visibility", getVisibility(), ItemVisibility.NORMAL);
7374
sm.append("required", isRequired());
74-
sm.append("persisted", isPersisted());
75+
sm.append("persisted", getPersistence());
7576
sm.append("persistKey", getPersistKey());
7677
sm.append("callback", getCallback());
7778
sm.append("widgetStyle", getWidgetStyle());
@@ -131,8 +132,8 @@ public boolean isRequired() {
131132
}
132133

133134
@Override
134-
public boolean isPersisted() {
135-
return true;
135+
public ItemPersistence getPersistence() {
136+
return ItemPersistence.DEFAULT;
136137
}
137138

138139
@Override
@@ -149,7 +150,7 @@ public String getPersistKey() {
149150
@Deprecated
150151
public T loadValue() {
151152
// if there is nothing to load from persistence return nothing
152-
if (!isPersisted()) return null;
153+
if (getPersistence() == ItemPersistence.NO) return null;
153154

154155
final String sValue;
155156
final String persistKey = getPersistKey();
@@ -169,7 +170,7 @@ public T loadValue() {
169170
@Override
170171
@Deprecated
171172
public void saveValue(final T value) {
172-
if (!isPersisted()) return;
173+
if (getPersistence() == ItemPersistence.NO) return;
173174

174175
final String sValue = value == null ? "" : value.toString();
175176

src/main/java/org/scijava/module/DefaultModuleService.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import java.util.concurrent.ExecutionException;
4242
import java.util.concurrent.Future;
4343

44+
import org.scijava.ItemPersistence;
4445
import org.scijava.MenuPath;
4546
import org.scijava.Priority;
4647
import org.scijava.convert.ConvertService;
@@ -277,12 +278,16 @@ public ModuleItem<?> getSingleOutput(Module module, Collection<Class<?>> types)
277278

278279
@Override
279280
public <T> void save(final ModuleItem<T> item, final T value) {
280-
if (!item.isPersisted()) return;
281+
ItemPersistence persistence = item.getPersistence();
282+
283+
if (persistence == ItemPersistence.NO) return;
281284

282285
// NB: Do not persist values which are computed via an initializer.
283-
if (item.getInitializer() != null && !item.getInitializer().isEmpty()) return;
286+
if (item.getInitializer() != null && !item.getInitializer().isEmpty() &&
287+
persistence == ItemPersistence.DEFAULT) return;
284288

285-
if (MiscUtils.equal(item.getDefaultValue(), value)) {
289+
if (MiscUtils.equal(item.getDefaultValue(), value) &&
290+
persistence == ItemPersistence.DEFAULT) {
286291
// NB: Do not persist the value if it is the default.
287292
// This is nice if the default value might change later,
288293
// such as when iteratively developing a script.
@@ -306,7 +311,7 @@ public <T> void save(final ModuleItem<T> item, final T value) {
306311
@Override
307312
public <T> T load(final ModuleItem<T> item) {
308313
// if there is nothing to load from persistence return nothing
309-
if (!item.isPersisted()) return null;
314+
if (item.getPersistence() == ItemPersistence.NO) return null;
310315

311316
final String sValue;
312317
final String persistKey = item.getPersistKey();

src/main/java/org/scijava/module/DefaultMutableModuleItem.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.List;
3838

3939
import org.scijava.ItemIO;
40+
import org.scijava.ItemPersistence;
4041
import org.scijava.ItemVisibility;
4142

4243
/**
@@ -54,7 +55,7 @@ public class DefaultMutableModuleItem<T> extends AbstractModuleItem<T>
5455
private ItemIO ioType;
5556
private ItemVisibility visibility;
5657
private boolean required;
57-
private boolean persisted;
58+
private ItemPersistence persistence;
5859
private String persistKey;
5960
private String initializer;
6061
private String callback;
@@ -87,7 +88,7 @@ public DefaultMutableModuleItem(final ModuleInfo info, final String name,
8788
ioType = super.getIOType();
8889
visibility = super.getVisibility();
8990
required = super.isRequired();
90-
persisted = super.isPersisted();
91+
persistence = super.getPersistence();
9192
persistKey = super.getPersistKey();
9293
initializer = super.getInitializer();
9394
callback = super.getCallback();
@@ -113,7 +114,7 @@ public DefaultMutableModuleItem(final ModuleInfo info,
113114
ioType = item.getIOType();
114115
visibility = item.getVisibility();
115116
required = item.isRequired();
116-
persisted = item.isPersisted();
117+
persistence = item.getPersistence();
117118
persistKey = item.getPersistKey();
118119
initializer = item.getInitializer();
119120
callback = item.getCallback();
@@ -148,8 +149,8 @@ public void setRequired(final boolean required) {
148149
}
149150

150151
@Override
151-
public void setPersisted(final boolean persisted) {
152-
this.persisted = persisted;
152+
public void setPersistence(final ItemPersistence persistence) {
153+
this.persistence = persistence;
153154
}
154155

155156
@Override
@@ -241,8 +242,8 @@ public boolean isRequired() {
241242
}
242243

243244
@Override
244-
public boolean isPersisted() {
245-
return persisted;
245+
public ItemPersistence getPersistence() {
246+
return persistence;
246247
}
247248

248249
@Override

src/main/java/org/scijava/module/ModuleItem.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737

3838
import org.scijava.BasicDetails;
3939
import org.scijava.ItemIO;
40+
import org.scijava.ItemPersistence;
4041
import org.scijava.ItemVisibility;
4142

4243
/**
@@ -84,7 +85,7 @@ public interface ModuleItem<T> extends BasicDetails {
8485
boolean isRequired();
8586

8687
/** Gets whether to remember the most recent value of the parameter. */
87-
boolean isPersisted();
88+
ItemPersistence getPersistence();
8889

8990
/** Gets the key to use for saving the value persistently. */
9091
String getPersistKey();

src/main/java/org/scijava/module/MutableModuleItem.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import java.util.List;
3535

3636
import org.scijava.ItemIO;
37+
import org.scijava.ItemPersistence;
3738
import org.scijava.ItemVisibility;
3839

3940
/**
@@ -50,7 +51,7 @@ public interface MutableModuleItem<T> extends ModuleItem<T> {
5051

5152
void setRequired(boolean required);
5253

53-
void setPersisted(boolean persisted);
54+
void setPersistence(ItemPersistence persistence);
5455

5556
void setPersistKey(String persistKey);
5657

src/main/java/org/scijava/plugin/Parameter.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.lang.annotation.Target;
3838

3939
import org.scijava.ItemIO;
40+
import org.scijava.ItemPersistence;
4041
import org.scijava.ItemVisibility;
4142

4243
/**
@@ -111,15 +112,16 @@
111112
boolean required() default true;
112113

113114
/** Defines whether to remember the most recent value of the parameter. */
114-
boolean persist() default true;
115+
ItemPersistence persist() default ItemPersistence.DEFAULT;
115116

116117
/** Defines a key to use for saving the value persistently. */
117118
String persistKey() default "";
118119

119120
/**
120121
* Defines a function that is called to initialize the parameter. If an
121122
* initializer is defined, it takes precedence over {@link #persist()}, i.e.
122-
* the parameter is not persisted even if {@code persist=true} is set.
123+
* the parameter is not persisted even if {@link #persist()} returns
124+
* {@link ItemPersistence#YES}.
123125
*/
124126
String initializer() default "";
125127

src/main/java/org/scijava/script/ScriptInfo.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.scijava.Context;
4848
import org.scijava.Contextual;
4949
import org.scijava.ItemIO;
50+
import org.scijava.ItemPersistence;
5051
import org.scijava.ItemVisibility;
5152
import org.scijava.NullContextException;
5253
import org.scijava.command.Command;
@@ -457,7 +458,7 @@ else if ("name".equalsIgnoreCase(key)) {
457458
item.setName(value);
458459
}
459460
else if ("persist".equalsIgnoreCase(key)) {
460-
item.setPersisted(convertService.convert(value, boolean.class));
461+
item.setPersistence(convertService.convert(value, ItemPersistence.class));
461462
}
462463
else if ("persistKey".equalsIgnoreCase(key)) {
463464
item.setPersistKey(value);

src/test/java/org/scijava/script/ScriptInfoTest.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.junit.Test;
5656
import org.scijava.Context;
5757
import org.scijava.ItemIO;
58+
import org.scijava.ItemPersistence;
5859
import org.scijava.log.LogService;
5960
import org.scijava.module.ModuleItem;
6061
import org.scijava.plugin.Plugin;
@@ -147,19 +148,19 @@ public void testParameters() {
147148
new ScriptInfo(context, "params.bsizes", new StringReader(script));
148149

149150
final ModuleItem<?> log = info.getInput("log");
150-
assertItem("log", LogService.class, null, ItemIO.INPUT, false, true, null,
151+
assertItem("log", LogService.class, null, ItemIO.INPUT, false, ItemPersistence.DEFAULT, null,
151152
null, null, null, null, null, null, null, log);
152153

153154
final ModuleItem<?> sliderValue = info.getInput("sliderValue");
154155
assertItem("sliderValue", int.class, "Slider Value", ItemIO.INPUT, true,
155-
true, null, "slider", 11, null, null, 5, 15, 3.0, sliderValue);
156+
ItemPersistence.DEFAULT, null, "slider", 11, null, null, 5, 15, 3.0, sliderValue);
156157

157158
final ModuleItem<?> buffer = info.getOutput("buffer");
158-
assertItem("buffer", StringBuilder.class, null, ItemIO.BOTH, true, true,
159+
assertItem("buffer", StringBuilder.class, null, ItemIO.BOTH, true, ItemPersistence.DEFAULT,
159160
null, null, null, null, null, null, null, null, buffer);
160161

161162
final ModuleItem<?> result = info.getOutput("result");
162-
assertItem("result", Object.class, null, ItemIO.OUTPUT, true, true, null,
163+
assertItem("result", Object.class, null, ItemIO.OUTPUT, true, ItemPersistence.DEFAULT, null,
163164
null, null, null, null, null, null, null, result);
164165

165166
int inputCount = 0;
@@ -177,7 +178,7 @@ public void testParameters() {
177178

178179
private void assertItem(final String name, final Class<?> type,
179180
final String label, final ItemIO ioType, final boolean required,
180-
final boolean persist, final String persistKey, final String style,
181+
final ItemPersistence persist, final String persistKey, final String style,
181182
final Object value, final Object min, final Object max,
182183
final Object softMin, final Object softMax, final Number stepSize,
183184
final ModuleItem<?> item)
@@ -187,7 +188,7 @@ private void assertItem(final String name, final Class<?> type,
187188
assertEquals(label, item.getLabel());
188189
assertSame(ioType, item.getIOType());
189190
assertEquals(required, item.isRequired());
190-
assertEquals(persist, item.isPersisted());
191+
assertEquals(persist, item.getPersistence());
191192
assertEquals(persistKey, item.getPersistKey());
192193
assertEquals(style, item.getWidgetStyle());
193194
assertEquals(value, item.getDefaultValue());

0 commit comments

Comments
 (0)