Skip to content

Commit d68d7dd

Browse files
Treiblesschorlectrueden
authored andcommitted
Cleanup
1 parent cc44052 commit d68d7dd

5 files changed

Lines changed: 106 additions & 164 deletions

File tree

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

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,10 @@
5858
import org.scijava.ops.types.Nil;
5959

6060
/**
61-
* Service that manages ops.
61+
* Service to provide a list of available ops structured in a prefix tree and to
62+
* search for ops matching specified types.
6263
*
63-
* @author Curtis Rueden
64+
* @author David Kolb
6465
*/
6566
@Plugin(type = Service.class)
6667
public class OpService extends AbstractService implements SciJavaService, OpEnvironment {
@@ -74,6 +75,9 @@ public class OpService extends AbstractService implements SciJavaService, OpEnvi
7475
@Parameter
7576
private LogService log;
7677

78+
/**
79+
* Prefix tree to cache and quickly find {@link OpInfo}s.
80+
*/
7781
private PrefixTree<OpInfo> opCache;
7882

7983
/**
@@ -144,14 +148,14 @@ public <T> StructInstance<T> findOpInstance(final String opName, final Nil<T> sp
144148
}
145149
return opInst;
146150
}
147-
148-
public <T> T findOp(final Nil<T> specialType, final Nil<?>[] inTypes,
149-
final Nil<?>[] outTypes, final Object... secondaryArgs) {
151+
152+
public <T> T findOp(final Nil<T> specialType, final Nil<?>[] inTypes, final Nil<?>[] outTypes,
153+
final Object... secondaryArgs) {
150154
return findOpInstance(null, specialType, inTypes, outTypes, secondaryArgs).object();
151155
}
152-
153-
public <T> T findOp(final Nil<T> specialType, final Nil<?>[] inTypes,
154-
final Nil<?> outType, final Object... secondaryArgs) {
156+
157+
public <T> T findOp(final Nil<T> specialType, final Nil<?>[] inTypes, final Nil<?> outType,
158+
final Object... secondaryArgs) {
155159
return findOpInstance(null, specialType, inTypes, new Nil[] { outType }, secondaryArgs).object();
156160
}
157161

@@ -173,15 +177,6 @@ public OpCandidate findTypeMatch(final OpRef ref) {
173177
return findTypeMatches(ref).singleMatch();
174178
}
175179

176-
private Type[] merge(Type in1, Type... ins) {
177-
Type[] merged = new Type[ins.length + 1];
178-
merged[0] = in1;
179-
for (int i = 0; i < ins.length; i++) {
180-
merged[i + 1] = ins[i];
181-
}
182-
return merged;
183-
}
184-
185180
private Type[] toTypes(Nil<?>... nils) {
186181
return Arrays.stream(nils).filter(n -> n != null).map(n -> n.getType()).toArray(Type[]::new);
187182
}
@@ -206,19 +201,25 @@ private void addAliases(String[] opNames) {
206201
}
207202
}
208203

209-
private static String getIndent(int num) {
204+
/**
205+
* Constructs a string with the specified number of tabs '\t'.
206+
*
207+
* @param numOfTabs
208+
* @return
209+
*/
210+
private static String getIndent(int numOfTabs) {
210211
String str = "";
211-
for (int i = 0; i < num; i++) {
212+
for (int i = 0; i < numOfTabs; i++) {
212213
str += "\t";
213214
}
214215
return str;
215216
}
216217

217218
/**
218219
* Class to represent a query for a {@link PrefixTree}. Prefixes must be
219-
* separated by dots ('.'). E.g. 'math.add'.
220-
*
221-
* @author David Kolb
220+
* separated by dots ('.'). E.g. 'math.add'. These queries are used in
221+
* order to specify the level where elements should be inserted into or
222+
* retrieved from the tree.
222223
*/
223224
private static class PrefixQuery {
224225
String cachedToString;
@@ -287,20 +288,22 @@ public String toString() {
287288
*
288289
* <pre>
289290
* Prefix: Elem:
290-
* math.add obj1
291-
* math.add obj2
292-
* math.sqrt obj3
293-
* math obj4
294-
* obj5
291+
* "math.add" obj1
292+
* "math.add" obj2
293+
* "math.sqrt" obj3
294+
* "math" obj4
295+
* "" obj5
295296
* </pre>
296297
*
297298
* Will result in the following tree:
298299
*
299300
* <pre>
300301
* root [obj5]
301302
* |
303+
* |
302304
* math [obj4]
303305
* / \
306+
* / \
304307
* add [obj1, obj2] sqrt [obj3]
305308
* </pre>
306309
*
@@ -343,7 +346,8 @@ private void add(PrefixQuery query, PrefixNode<T> node, T data) {
343346
* this class would return all elements contained in the tree except for
344347
* 'obj5'. 'math.add' would only return 'obj1' and 'obj2'. This method
345348
* returns an iterable over these elements in O(# number of all nodes
346-
* below query).
349+
* below query). The number of nodes is the number of distinct prefixes
350+
* below the specified query.
347351
*
348352
* @param query
349353
* @return

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

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@
4646
import org.scijava.struct.StructInstance;
4747

4848
/**
49-
* Utility methods for working with ops. In particular, this class contains
50-
* handy methods for generating human-readable strings describing ops and match
51-
* requests against them.
49+
* Utility methods for working with ops.
5250
*
5351
* @author Curtis Rueden
52+
* @author David Kolb
5453
*/
5554
public final class OpUtils {
5655

@@ -59,23 +58,11 @@ private OpUtils() {
5958
}
6059

6160
// -- Utility methods --
62-
61+
6362
public static String[] parseOpNames(String names) {
6463
return Arrays.stream(names.split(",")).map(s -> s.trim()).toArray(String[]::new);
6564
}
6665

67-
public static Object[] args(final Object[] latter, final Object... former) {
68-
final Object[] result = new Object[former.length + latter.length];
69-
int i = 0;
70-
for (final Object o : former) {
71-
result[i++] = o;
72-
}
73-
for (final Object o : latter) {
74-
result[i++] = o;
75-
}
76-
return result;
77-
}
78-
7966
public static List<MemberInstance<?>> inputs(StructInstance<?> op) {
8067
return op.members().stream() //
8168
.filter(memberInstance -> memberInstance.member().isInput()) //
@@ -278,15 +265,16 @@ public static String opString(final OpInfo info, final Member<?> special) {
278265
}
279266

280267
/**
281-
* Helper method of {@link #opString(OpInfo, Member)} which parses a
282-
* set of items with a default delimiter of ","
268+
* Helper method of {@link #opString(OpInfo, Member)} which parses a set of
269+
* items with a default delimiter of ","
283270
*/
284271
private static String paramString(final Iterable<Member<?>> items, final Member<?> special) {
285272
return paramString(items, special, ",");
286273
}
287274

288275
/**
289-
* As {@link #paramString(Iterable, Member, String)} with an optional delimiter.
276+
* As {@link #paramString(Iterable, Member, String)} with an optional
277+
* delimiter.
290278
*/
291279
private static String paramString(final Iterable<Member<?>> items, final Member<?> special, final String delim) {
292280
return paramString(items, special, delim, false);

0 commit comments

Comments
 (0)