Skip to content

Commit d7fbc08

Browse files
gselzerhinerm
authored andcommitted
First cut at a Hints data structure
1 parent 784eba8 commit d7fbc08

4 files changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.scijava.ops.hints;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.NoSuchElementException;
6+
import java.util.Optional;
7+
8+
import org.scijava.ops.hints.DefaultOpHints.Adaptable;
9+
import org.scijava.ops.hints.DefaultOpHints.Simplifiable;
10+
11+
/**
12+
* Default Implementation of
13+
* @author G
14+
*
15+
*/
16+
public class DefaultHints implements Hints {
17+
18+
private final Map<String, String> hints;
19+
20+
public DefaultHints() {
21+
this(new String[] {Simplifiable.YES, Adaptable.YES});
22+
}
23+
24+
public DefaultHints(String... startingHints) {
25+
hints = new HashMap<>();
26+
for(String hint : startingHints)
27+
setHint(hint);
28+
}
29+
30+
@Override
31+
public String setHint(String hint) {
32+
String[] hintParts = getHintParts(hint);
33+
validateHintParts(hint, hintParts);
34+
return hints.put(hintParts[0], hintParts[1]);
35+
}
36+
37+
@Override
38+
public boolean containsHintType(String hintType) {
39+
String[] hintParts = getHintParts(hintType);
40+
return hints.containsKey(hintParts[1]);
41+
}
42+
43+
@Override
44+
public boolean isActive(String hint) {
45+
return hints.containsKey(hint);
46+
}
47+
48+
@Override
49+
public String getHint(String hintType) {
50+
String[] hintParts = getHintParts(hintType);
51+
if (!hints.containsKey(hintParts[1])) throw new NoSuchElementException(
52+
"No hint of type " + hintType + " is contained!");
53+
return buildHint(hintParts[0], hintParts[1], hints.get(hintParts[1]));
54+
55+
}
56+
57+
private String buildHint(String base, String prefix, String suffix) {
58+
return String.join(".", base, prefix, suffix);
59+
}
60+
61+
private String[] getHintParts(String hint) {
62+
return hint.split(".");
63+
}
64+
65+
private void validateHintParts(String hint, String[] hintParts) {
66+
if (hintParts.length != 3) throw new IllegalArgumentException("Hint " +
67+
hint +
68+
" does not conform to hint structure <Hints.BASE>.<hint_type>.<hint>!");
69+
if (!hintParts[0].equals(Hints.BASE)) throw new IllegalArgumentException(
70+
"Hint " + hint + " must begin with " + Hints.BASE);
71+
}
72+
73+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.scijava.ops.hints;
2+
3+
public class DefaultOpHints {
4+
5+
public static class Simplifiable {
6+
public static final String prefix = "simplification";
7+
public static final String YES = Hints.BASE + "." + prefix + ".YES";
8+
public static final String NO = Hints.BASE + "." + prefix + ".NO";
9+
}
10+
11+
public static class Adaptable {
12+
public static final String prefix = "adaptation";
13+
public static final String YES = Hints.BASE + "." + prefix + ".YES";
14+
public static final String NO = Hints.BASE + "." + prefix + ".NO";
15+
}
16+
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
package org.scijava.ops.hints;
3+
4+
public interface Hints {
5+
6+
public static final String BASE = "hints";
7+
8+
public String setHint(String hint);
9+
10+
public String getHint(String hintType);
11+
12+
public boolean isActive(String hint);
13+
14+
boolean containsHintType(String hintType);
15+
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
package org.scijava.ops.hints;
3+
4+
/**
5+
* An annotation used to record the hints that apply to a particular Op.
6+
*
7+
* @author Gabriel Selzer
8+
*/
9+
public @interface OpHints {
10+
11+
String[] hints() default {};
12+
}

0 commit comments

Comments
 (0)