Skip to content

Commit b4e8a32

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 44029b6 commit b4e8a32

10 files changed

+112
-33
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
@@ -42,6 +42,7 @@
4242
import java.util.concurrent.Future;
4343

4444
import org.scijava.Identifiable;
45+
import org.scijava.ItemPersistence;
4546
import org.scijava.MenuPath;
4647
import org.scijava.Priority;
4748
import org.scijava.convert.ConvertService;
@@ -289,12 +290,16 @@ public ModuleItem<?> getSingleOutput(Module module, Collection<Class<?>> types)
289290

290291
@Override
291292
public <T> void save(final ModuleItem<T> item, final T value) {
292-
if (!item.isPersisted()) return;
293+
ItemPersistence persistence = item.getPersistence();
294+
295+
if (persistence == ItemPersistence.NO) return;
293296

294297
// NB: Do not persist values which are computed via an initializer.
295-
if (item.getInitializer() != null && !item.getInitializer().isEmpty()) return;
298+
if (item.getInitializer() != null && !item.getInitializer().isEmpty() &&
299+
persistence == ItemPersistence.DEFAULT) return;
296300

297-
if (MiscUtils.equal(item.getDefaultValue(), value)) {
301+
if (MiscUtils.equal(item.getDefaultValue(), value) &&
302+
persistence == ItemPersistence.DEFAULT) {
298303
// NB: Do not persist the value if it is the default.
299304
// This is nice if the default value might change later,
300305
// such as when iteratively developing a script.
@@ -318,7 +323,7 @@ public <T> void save(final ModuleItem<T> item, final T value) {
318323
@Override
319324
public <T> T load(final ModuleItem<T> item) {
320325
// if there is nothing to load from persistence return nothing
321-
if (!item.isPersisted()) return null;
326+
if (item.getPersistence() == ItemPersistence.NO) return null;
322327

323328
final String sValue;
324329
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
@@ -49,6 +49,7 @@
4949
import org.scijava.Context;
5050
import org.scijava.Contextual;
5151
import org.scijava.ItemIO;
52+
import org.scijava.ItemPersistence;
5253
import org.scijava.ItemVisibility;
5354
import org.scijava.NullContextException;
5455
import org.scijava.command.Command;
@@ -427,7 +428,7 @@ private <T> void assignAttribute(final DefaultMutableModuleItem<T> item,
427428
else if (is(k, "max")) item.setMaximumValue(as(v, item.getType()));
428429
else if (is(k, "min")) item.setMinimumValue(as(v, item.getType()));
429430
else if (is(k, "name")) item.setName(as(v, String.class));
430-
else if (is(k, "persist")) item.setPersisted(as(v, boolean.class));
431+
else if (is(k, "persist")) item.setPersistence(as(v, ItemPersistence.class));
431432
else if (is(k, "persistKey")) item.setPersistKey(as(v, String.class));
432433
else if (is(k, "required")) item.setRequired(as(v, boolean.class));
433434
else if (is(k, "softMax")) item.setSoftMaximum(as(v, item.getType()));

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

+17-11
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import org.junit.Test;
5757
import org.scijava.Context;
5858
import org.scijava.ItemIO;
59+
import org.scijava.ItemPersistence;
5960
import org.scijava.log.LogService;
6061
import org.scijava.module.ModuleItem;
6162
import org.scijava.plugin.Plugin;
@@ -151,27 +152,32 @@ public void testParameters() {
151152
final List<?> noChoices = Collections.emptyList();
152153

153154
final ModuleItem<?> log = info.getInput("log");
154-
assertItem("log", LogService.class, null, ItemIO.INPUT, false, true, null,
155-
null, null, null, null, null, null, null, noChoices, log);
155+
assertItem("log", LogService.class, null, ItemIO.INPUT, false,
156+
ItemPersistence.DEFAULT, null, null, null, null, null, null, null, null,
157+
noChoices, log);
156158

157159
final ModuleItem<?> sliderValue = info.getInput("sliderValue");
158160
assertItem("sliderValue", int.class, "Slider Value", ItemIO.INPUT, true,
159-
true, null, "slider", 11, null, null, 5, 15, 3.0, noChoices, sliderValue);
161+
ItemPersistence.DEFAULT, null, "slider", 11, null, null, 5, 15, 3.0,
162+
noChoices, sliderValue);
160163

161164
final ModuleItem<?> animal = info.getInput("animal");
162165
final List<String> animalChoices = //
163166
Arrays.asList("quick brown fox", "lazy dog");
164-
assertItem("animal", String.class, null, ItemIO.INPUT, true, false,
165-
null, null, null, null, null, null, null, null, animalChoices, animal);
167+
assertItem("animal", String.class, null, ItemIO.INPUT, true,
168+
ItemPersistence.DEFAULT, null, null, null, null, null, null, null, null,
169+
animalChoices, animal);
166170
assertEquals(animal.get("family"), "Carnivora"); // test custom attribute
167171

168172
final ModuleItem<?> buffer = info.getOutput("buffer");
169-
assertItem("buffer", StringBuilder.class, null, ItemIO.BOTH, true, true,
170-
null, null, null, null, null, null, null, null, noChoices, buffer);
173+
assertItem("buffer", StringBuilder.class, null, ItemIO.BOTH, true,
174+
ItemPersistence.DEFAULT, null, null, null, null, null, null, null, null,
175+
noChoices, buffer);
171176

172177
final ModuleItem<?> result = info.getOutput("result");
173-
assertItem("result", Object.class, null, ItemIO.OUTPUT, true, true, null,
174-
null, null, null, null, null, null, null, noChoices, result);
178+
assertItem("result", Object.class, null, ItemIO.OUTPUT, true,
179+
ItemPersistence.DEFAULT, null, null, null, null, null, null, null, null,
180+
noChoices, result);
175181

176182
int inputCount = 0;
177183
final ModuleItem<?>[] inputs = { log, sliderValue, animal, buffer };
@@ -188,7 +194,7 @@ public void testParameters() {
188194

189195
private void assertItem(final String name, final Class<?> type,
190196
final String label, final ItemIO ioType, final boolean required,
191-
final boolean persist, final String persistKey, final String style,
197+
final ItemPersistence persist, final String persistKey, final String style,
192198
final Object value, final Object min, final Object max,
193199
final Object softMin, final Object softMax, final Number stepSize,
194200
final List<?> choices, final ModuleItem<?> item)
@@ -198,7 +204,7 @@ private void assertItem(final String name, final Class<?> type,
198204
assertEquals(label, item.getLabel());
199205
assertSame(ioType, item.getIOType());
200206
assertEquals(required, item.isRequired());
201-
assertEquals(persist, item.isPersisted());
207+
assertEquals(persist, item.getPersistence());
202208
assertEquals(persistKey, item.getPersistKey());
203209
assertEquals(style, item.getWidgetStyle());
204210
assertEquals(value, item.getDefaultValue());

0 commit comments

Comments
 (0)