Skip to content

Commit 2c225ea

Browse files
gselzerctrueden
authored andcommitted
Consolidate OpInfo printout methods
There were two different methods that wrote printouts for OpInfos, using rather different syntaxes. We decided that one syntax was better, and added member highlight support (which was only available using the other syntax).
1 parent 8995919 commit 2c225ea

2 files changed

Lines changed: 23 additions & 80 deletions

File tree

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

Lines changed: 15 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -344,86 +344,27 @@ public static String matchInfo(final MatchingResult res) {
344344
return sb.toString();
345345
}
346346

347-
/**
348-
* Gets a string describing the given op, highlighting the specific
349-
* parameter.
350-
*
351-
* @param info
352-
* The {@link OpInfo} metadata which describes the op.
353-
* @param special
354-
* A parameter of particular interest when describing the op.
355-
* @return A string describing the op.
356-
*/
357-
public static String opString(final OpInfo info, final Member<?> special) {
358-
final StringBuilder sb = new StringBuilder();
359-
final String outputString = paramString(outputs(info.struct()), null).trim();
360-
if (!outputString.isEmpty())
361-
sb.append("(" + outputString + ") =\n\t");
362-
sb.append(info.implementationName());
363-
sb.append("(" + paramString(inputs(info.struct()), special) + ")");
364-
return sb.toString();
365-
}
366-
367-
/**
368-
* Helper method of {@link #opString(OpInfo, Member)} which parses a set of
369-
* items with a default delimiter of ","
370-
*/
371-
private static String paramString(final Iterable<Member<?>> items, final Member<?> special) {
372-
return paramString(items, special, ",");
373-
}
374-
375-
/**
376-
* As {@link #paramString(Iterable, Member, String)} with an optional
377-
* delimiter.
378-
*/
379-
private static String paramString(final Iterable<Member<?>> items, final Member<?> special, final String delim) {
380-
return paramString(items, special, delim, false);
381-
}
382-
383-
/**
384-
* As {@link #paramString(Iterable, Member, String)} with a toggle to
385-
* control if inputs are types only or include the names/descriptions.
386-
*/
387-
private static String paramString(final Iterable<Member<?>> items, final Member<?> special, final String delim,
388-
final boolean typeOnly) {
389-
final StringBuilder sb = new StringBuilder();
390-
boolean first = true;
391-
for (final Member<?> item : items) {
392-
if (first)
393-
first = false;
394-
else
395-
sb.append(delim);
396-
sb.append("\n");
397-
if (item == special)
398-
sb.append("==>"); // highlight special item
399-
sb.append("\t\t");
400-
sb.append(item.getType().getTypeName());
401-
402-
if (!typeOnly) {
403-
sb.append(" " + item.getKey());
404-
sb.append(" -> " + item.getDescription());
405-
}
406-
}
407-
return sb.toString();
347+
public static String opString(final OpInfo info) {
348+
return opString(info, null);
408349
}
409350

410-
public static String opString(final OpInfo info) {
351+
public static String opString(final OpInfo info, final Member<?> special) {
411352
final StringBuilder sb = new StringBuilder();
412353
sb.append(info.implementationName() + "(\n\t Inputs:\n");
413354
for (final Member<?> arg : info.inputs()) {
414-
sb.append("\t\t");
415-
sb.append(arg.getType().getTypeName());
416-
sb.append(" ");
417-
sb.append(arg.getKey());
418-
if (!arg.getDescription().isEmpty()) {
419-
sb.append(" -> ");
420-
sb.append(arg.getDescription());
421-
}
422-
sb.append("\n");
355+
appendParam(sb, arg, special);
423356
}
424357
sb.append("\t Outputs:\n");
425-
final Member<?> arg = info.output();
426-
sb.append("\t\t");
358+
appendParam(sb, info.output(), special);
359+
sb.append(")\n");
360+
return sb.toString();
361+
}
362+
363+
private static void appendParam(final StringBuilder sb, final Member<?> arg,
364+
final Member<?> special)
365+
{
366+
if (arg == special) sb.append("==> \t"); // highlight special item
367+
else sb.append("\t\t");
427368
sb.append(arg.getType().getTypeName());
428369
sb.append(" ");
429370
sb.append(arg.getKey());
@@ -432,8 +373,7 @@ public static String opString(final OpInfo info) {
432373
sb.append(arg.getDescription());
433374
}
434375
sb.append("\n");
435-
sb.append(")\n");
436-
return sb.toString();
376+
return;
437377
}
438378

439379
public static Class<?> findFirstImplementedFunctionalInterface(final OpRef opRef) {

scijava/scijava-ops/src/test/java/org/scijava/param/JavadocParameterTest.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,14 @@ public void opStringRegressionTest() {
163163
Assert.assertEquals(expected, actual);
164164

165165
// test special op string
166-
expected = "(java.util.List<java.lang.Long> output -> foo + bar) =\n" +
167-
" public static java.util.List<java.lang.Long> org.scijava.param.JavadocParameterTest." +
168-
"OpMethodFoo(java.util.List<java.lang.String>,java.util.List<java.lang.String>)(\n" +
169-
" java.util.List<java.lang.String> foo -> the first input,\n" +
170-
"==> java.util.List<java.lang.String> bar -> the second input)";
166+
expected =
167+
"public static java.util.List<java.lang.Long> org.scijava.param.JavadocParameterTest." +
168+
"OpMethodFoo(java.util.List<java.lang.String>,java.util.List<java.lang.String>)(\n" +
169+
" Inputs:\n" +
170+
" java.util.List<java.lang.String> foo -> the first input\n" +
171+
"==> java.util.List<java.lang.String> bar -> the second input\n" +
172+
" Outputs:\n" +
173+
" java.util.List<java.lang.Long> output -> foo + bar\n" + ")\n";
171174
actual = OpUtils.opString(info, info.inputs().get(1));
172175
Assert.assertEquals(expected, actual);
173176
}

0 commit comments

Comments
 (0)