Skip to content

Commit e6ebeea

Browse files
gselzerctrueden
authored andcommitted
Add OpMonitor interface for monitoring progress
Signed-off-by: Curtis Rueden <ctrueden@wisc.edu>
1 parent 5c4de1f commit e6ebeea

5 files changed

Lines changed: 334 additions & 32 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.scijava;
2+
3+
import org.scijava.ops.OpMonitor;
4+
import org.scijava.util.Logger;
5+
6+
/**
7+
* Default implementation of {@link OpMonitor}
8+
*
9+
* @author Gabriel Selzer
10+
* @author Marcel Wiedenmann
11+
*
12+
*/
13+
public class DefaultOpMonitor implements OpMonitor {
14+
15+
private volatile boolean canceled;
16+
17+
private volatile double progress;
18+
19+
private Logger logger;
20+
21+
// private volatile OpMonitor parent;
22+
//
23+
// private List<OpMonitor> children;
24+
25+
public DefaultOpMonitor() {
26+
this(new Logger());
27+
}
28+
29+
public DefaultOpMonitor(Logger log) {
30+
logger = log;
31+
canceled = false;
32+
progress = 0d;
33+
}
34+
35+
@Override
36+
public boolean isCanceled() {
37+
return canceled;
38+
}
39+
40+
@Override
41+
public void cancel() {
42+
canceled = true;
43+
}
44+
45+
@Override
46+
public Logger logger() {
47+
return logger;
48+
}
49+
50+
@Override
51+
public void setProgress(double progress) {
52+
this.progress = progress;
53+
}
54+
55+
@Override
56+
public double getProgress() {
57+
return this.progress;
58+
}
59+
60+
}

src/main/java/org/scijava/ops/OpExecutionException.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
/*
2+
* #%L
3+
* SciJava Operations: a framework for reusable algorithms.
4+
* %%
5+
* Copyright (C) 2018 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
129
package org.scijava.ops;
230

331
/**
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* #%L
3+
* SciJava Operations: a framework for reusable algorithms.
4+
* %%
5+
* Copyright (C) 2018 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
package org.scijava.ops;
30+
31+
import java.util.concurrent.CancellationException;
32+
33+
import org.scijava.util.Logger;
34+
35+
/**
36+
* Used as a bridge between the {@link Op} and the user. Allows the user to
37+
* cancel any Op as well as allowing the Op to notify the user of the Op's
38+
* progress and of any warnings encountered through the course of computation.
39+
*
40+
* @author Gabriel Selzer
41+
* @author Marcel Wiedenmann
42+
*
43+
*/
44+
public interface OpMonitor {
45+
46+
/**
47+
* Checks if the user has canceled computation.
48+
*
49+
* @return true if the user has canceled computation.
50+
*/
51+
boolean isCanceled();
52+
53+
/**
54+
* Throws an {@link CancellationException} if the user has canceled
55+
* computation.
56+
*/
57+
default void checkCanceled() {
58+
if (isCanceled())
59+
throw new CancellationException("The Op was canceled before it was able to complete computation");
60+
}
61+
62+
/**
63+
* If called by the user, computation is canceled.
64+
*/
65+
void cancel();
66+
67+
/**
68+
* Returns a {@link Logger} for use by the {@link Op}.
69+
*
70+
* @return a {@link Logger}.
71+
*/
72+
Logger logger();
73+
74+
/**
75+
* Sets the progress of the computation monitored by this {@link OpMonitor}
76+
*
77+
* @param progress
78+
* - the progress <i>p</i> of the {@link Op}'s computation. 0 &lt;=
79+
* <i>p</i> &lt;= 1
80+
*/
81+
void setProgress(double progress);
82+
83+
/**
84+
* Returns the completion progress of the Op's computation as a decimal.
85+
*
86+
* @return progress - the progress <i>p</i> of the {@link Op}'s computation. 0
87+
* &lt;= <i>p</i> &lt;= 1
88+
*/
89+
double getProgress();
90+
91+
// OpMonitor createSubMonitor();
92+
93+
}

src/main/java/org/scijava/util/Logger.java

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,91 +18,90 @@
1818
*/
1919
public class Logger {
2020

21-
private Logger() {
22-
// Static utility class
21+
public Logger() {
2322
}
2423

25-
public static void debug(final Object msg) {
24+
public void debug(final Object msg) {
2625
log(DEBUG, msg);
2726
}
2827

29-
public static void debug(final Throwable t) {
28+
public void debug(final Throwable t) {
3029
log(DEBUG, t);
3130
}
3231

33-
public static void debug(final Object msg, final Throwable t) {
32+
public void debug(final Object msg, final Throwable t) {
3433
log(DEBUG, msg, t);
3534
}
3635

37-
public static void error(final Object msg) {
36+
public void error(final Object msg) {
3837
log(ERROR, msg);
3938
}
4039

41-
public static void error(final Throwable t) {
40+
public void error(final Throwable t) {
4241
log(ERROR, t);
4342
}
4443

45-
public static void error(final Object msg, final Throwable t) {
44+
public void error(final Object msg, final Throwable t) {
4645
log(ERROR, msg, t);
4746
}
4847

49-
public static void info(final Object msg) {
48+
public void info(final Object msg) {
5049
log(INFO, msg);
5150
}
5251

53-
public static void info(final Throwable t) {
52+
public void info(final Throwable t) {
5453
log(INFO, t);
5554
}
5655

57-
public static void info(final Object msg, final Throwable t) {
56+
public void info(final Object msg, final Throwable t) {
5857
log(INFO, msg, t);
5958
}
6059

61-
public static void trace(final Object msg) {
60+
public void trace(final Object msg) {
6261
log(TRACE, msg);
6362
}
6463

65-
public static void trace(final Throwable t) {
64+
public void trace(final Throwable t) {
6665
log(TRACE, t);
6766
}
6867

69-
public static void trace(final Object msg, final Throwable t) {
68+
public void trace(final Object msg, final Throwable t) {
7069
log(TRACE, msg, t);
7170
}
7271

73-
public static void warn(final Object msg) {
72+
public void warn(final Object msg) {
7473
log(WARN, msg);
7574
}
7675

77-
public static void warn(final Throwable t) {
76+
public void warn(final Throwable t) {
7877
log(WARN, t);
7978
}
8079

81-
public static void warn(final Object msg, final Throwable t) {
80+
public void warn(final Object msg, final Throwable t) {
8281
log(WARN, msg, t);
8382
}
8483

85-
public static boolean isDebug() {
84+
public boolean isDebug() {
8685
return isLevel(DEBUG);
8786
}
8887

89-
public static boolean isError() {
88+
public boolean isError() {
9089
return isLevel(ERROR);
9190
}
9291

93-
public static boolean isInfo() {
92+
public boolean isInfo() {
9493
return isLevel(INFO);
9594
}
9695

97-
public static boolean isTrace() {
96+
public boolean isTrace() {
9897
return isLevel(TRACE);
9998
}
10099

101-
public static boolean isWarn() {
100+
public boolean isWarn() {
102101
return isLevel(WARN);
103102
}
104103

105-
public static boolean isLevel(final int level) {
104+
public boolean isLevel(final int level) {
106105
return getLevel() >= level;
107106
}
108107

@@ -114,7 +113,7 @@ public static boolean isLevel(final int level) {
114113
* is performed.
115114
* @param msg The message to log.
116115
*/
117-
public static void log(final int level, final Object msg) {
116+
public void log(final int level, final Object msg) {
118117
log(level, msg, null);
119118
}
120119

@@ -126,7 +125,7 @@ public static void log(final int level, final Object msg) {
126125
* logging is performed.
127126
* @param t The exception to log.
128127
*/
129-
public static void log(final int level, final Throwable t) {
128+
public void log(final int level, final Throwable t) {
130129
log(level, null, t);
131130
}
132131

@@ -139,7 +138,7 @@ public static void log(final int level, final Throwable t) {
139138
* @param msg The message to log.
140139
* @param t The exception to log.
141140
*/
142-
public static void log(final int level, final Object msg, final Throwable t) {
141+
public void log(final int level, final Object msg, final Throwable t) {
143142
if (isLevel(level)) alwaysLog(level, msg, t);
144143
}
145144

@@ -151,32 +150,32 @@ public static void log(final int level, final Object msg, final Throwable t) {
151150
* @param msg The message to log.
152151
* @param t The exception to log.
153152
*/
154-
public static void alwaysLog(final int level, final Object msg,
153+
public void alwaysLog(final int level, final Object msg,
155154
final Throwable t)
156155
{
157156
throw new UnsupportedOperationException("not yet implemented");
158157
}
159158

160159
/** Returns the name of this logger. */
161-
public static String getName() {
160+
public String getName() {
162161
return getSource().name();
163162
}
164163

165164
/** Returns the {@link LogSource} associated with this logger. */
166-
public static LogSource getSource() {
165+
public LogSource getSource() {
167166
throw new UnsupportedOperationException("not yet implemented");
168167
}
169168

170169
/** Returns the log level of this logger. see {@link LogLevel} */
171-
public static int getLevel() {
170+
public int getLevel() {
172171
throw new UnsupportedOperationException("not yet implemented");
173172
}
174173

175174
/**
176175
* Creates a sub logger, that forwards the message it gets to this logger. The
177176
* sub logger will have the same log level as this logger.
178177
*/
179-
public static Logger subLogger(final String name) {
178+
public Logger subLogger(final String name) {
180179
return subLogger(name, getLevel());
181180
}
182181

@@ -186,7 +185,7 @@ public static Logger subLogger(final String name) {
186185
* @param name The name of the sub logger.
187186
* @param level The log level of the sub logger.
188187
*/
189-
public static Logger subLogger(final String name, final int level) {
188+
public Logger subLogger(final String name, final int level) {
190189
throw new UnsupportedOperationException("not yet implemented");
191190
}
192191

0 commit comments

Comments
 (0)