Skip to content

Commit 6409b8f

Browse files
gselzerhinerm
authored andcommitted
Refactor DefaultOpEnvironment to use hints
1 parent ee30d8b commit 6409b8f

6 files changed

Lines changed: 230 additions & 62 deletions

File tree

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.scijava.ops.hints;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.NoSuchElementException;
6+
7+
import org.scijava.ops.hints.DefaultOpHints.Adaptable;
8+
9+
public class AdaptationHints implements Hints{
10+
11+
Map<String, String> hints;
12+
13+
private AdaptationHints(Map<String, String> map) {
14+
this.hints = map;
15+
}
16+
17+
public static AdaptationHints generateHints(Hints hints) {
18+
// collect all old hints that are not Adaptable
19+
Map<String, String> map = new HashMap<>();
20+
hints.getHints().entrySet().parallelStream().filter(e -> e
21+
.getKey() != Adaptable.prefix).forEach(e -> map.put(e.getKey(), e
22+
.getValue()));
23+
24+
// add Adaptable.NO
25+
AdaptationHints newHints = new AdaptationHints(map);
26+
newHints.setHint(Adaptable.NO);
27+
28+
return newHints;
29+
}
30+
31+
@Override
32+
public String setHint(String hint) {
33+
if (hint.equals(Adaptable.YES)) throw new IllegalArgumentException(
34+
"We cannot allow adaptation during adaptation; this would cause a recursive loop!");
35+
String prefix = getPrefix(hint);
36+
return hints.put(prefix, hint);
37+
}
38+
39+
@Override
40+
public String getHint(String prefix) {
41+
if (!hints.containsKey(prefix)) throw new NoSuchElementException(
42+
"No hint of type " + prefix + " is contained!");
43+
return hints.get(prefix);
44+
}
45+
46+
@Override
47+
public boolean containsHint(String hint) {
48+
String prefix = getPrefix(hint);
49+
return containsHintType(prefix) && hint.equals(hints.get(prefix));
50+
}
51+
52+
@Override
53+
public boolean containsHintType(String prefix) {
54+
return hints.containsKey(prefix);
55+
}
56+
57+
private String getPrefix(String hint) {
58+
return hint.split("\\.")[0];
59+
}
60+
61+
@Override
62+
public Map<String, String> getHints() {
63+
return hints;
64+
}
65+
66+
@Override
67+
public Hints getCopy() {
68+
return AdaptationHints.generateHints(this);
69+
}
70+
71+
}

scijava/scijava-ops/src/main/java/org/scijava/ops/hints/DefaultHints.java

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.util.HashMap;
44
import java.util.Map;
55
import java.util.NoSuchElementException;
6-
import java.util.Optional;
76

87
import org.scijava.ops.hints.DefaultOpHints.Adaptable;
98
import org.scijava.ops.hints.DefaultOpHints.Simplifiable;
@@ -15,6 +14,7 @@
1514
*/
1615
public class DefaultHints implements Hints {
1716

17+
// Hints are stored by their hint type (the middle term)
1818
private final Map<String, String> hints;
1919

2020
public DefaultHints() {
@@ -27,47 +27,47 @@ public DefaultHints(String... startingHints) {
2727
setHint(hint);
2828
}
2929

30+
private DefaultHints(Map<String, String> hints) {
31+
this.hints = hints;
32+
}
33+
3034
@Override
3135
public String setHint(String hint) {
32-
String[] hintParts = getHintParts(hint);
33-
validateHintParts(hint, hintParts);
34-
return hints.put(hintParts[0], hintParts[1]);
36+
String prefix = getPrefix(hint);
37+
return hints.put(prefix, hint);
3538
}
3639

3740
@Override
38-
public boolean containsHintType(String hintType) {
39-
String[] hintParts = getHintParts(hintType);
40-
return hints.containsKey(hintParts[1]);
41+
public boolean containsHintType(String prefix) {
42+
return hints.containsKey(prefix);
4143
}
4244

4345
@Override
44-
public boolean isActive(String hint) {
45-
return hints.containsKey(hint);
46+
public boolean containsHint(String hint) {
47+
String prefix = getPrefix(hint);
48+
return hints.containsKey(prefix) && hints.get(prefix).equals(
49+
hint);
4650
}
4751

4852
@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-
53+
public String getHint(String prefix) {
54+
if (!hints.containsKey(prefix)) throw new NoSuchElementException(
55+
"No hint of type " + prefix + " is contained!");
56+
return hints.get(prefix);
5557
}
5658

57-
private String buildHint(String base, String prefix, String suffix) {
58-
return String.join(".", base, prefix, suffix);
59+
private String getPrefix(String hint) {
60+
return hint.split("\\.")[0];
5961
}
6062

61-
private String[] getHintParts(String hint) {
62-
return hint.split(".");
63+
@Override
64+
public Map<String, String> getHints() {
65+
return hints;
6366
}
6467

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);
68+
@Override
69+
public Hints getCopy() {
70+
return new DefaultHints(new HashMap<>(getHints()));
7171
}
7272

7373
}

scijava/scijava-ops/src/main/java/org/scijava/ops/hints/DefaultOpHints.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ public class DefaultOpHints {
44

55
public static class Simplifiable {
66
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";
7+
public static final String YES = prefix + ".YES";
8+
public static final String NO = prefix + ".NO";
99
}
1010

1111
public static class Adaptable {
1212
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";
13+
public static final String YES = prefix + ".YES";
14+
public static final String NO = prefix + ".NO";
1515
}
1616

1717
}
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11

22
package org.scijava.ops.hints;
33

4-
public interface Hints {
4+
import java.util.Map;
55

6-
public static final String BASE = "hints";
6+
public interface Hints {
77

88
public String setHint(String hint);
99

1010
public String getHint(String hintType);
1111

12-
public boolean isActive(String hint);
12+
public boolean containsHint(String hint);
13+
14+
public boolean containsHintType(String hintType);
15+
16+
public Map<String, String> getHints();
1317

14-
boolean containsHintType(String hintType);
18+
public Hints getCopy();
1519

1620
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.scijava.ops.hints;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.NoSuchElementException;
6+
7+
import org.scijava.ops.hints.DefaultOpHints.Simplifiable;
8+
9+
10+
public class SimplificationHints implements Hints {
11+
12+
Map<String, String> hints;
13+
14+
private SimplificationHints(Map<String, String> map) {
15+
this.hints = map;
16+
}
17+
18+
public static SimplificationHints generateHints(Hints hints) {
19+
// collect all old hints that are not Adaptable
20+
Map<String, String> map = new HashMap<>();
21+
hints.getHints().entrySet().parallelStream().filter(e -> e
22+
.getKey() != Simplifiable.prefix).forEach(e -> map.put(e.getKey(), e
23+
.getValue()));
24+
25+
// add Simplifiable.NO
26+
SimplificationHints newHints = new SimplificationHints(map);
27+
newHints.setHint(Simplifiable.NO);
28+
29+
return newHints;
30+
}
31+
32+
@Override
33+
public String setHint(String hint) {
34+
if (hint.equals(Simplifiable.YES)) throw new IllegalArgumentException(
35+
"We cannot allow simplification during simplification; this would cause a recursive loop!");
36+
String prefix = getPrefix(hint);
37+
return hints.put(prefix, hint);
38+
}
39+
40+
@Override
41+
public String getHint(String prefix) {
42+
if (!hints.containsKey(prefix)) throw new NoSuchElementException(
43+
"No hint of type " + prefix + " is contained!");
44+
return hints.get(prefix);
45+
}
46+
47+
@Override
48+
public boolean containsHint(String hint) {
49+
String prefix = getPrefix(hint);
50+
return containsHintType(prefix) && hint.equals(hints.get(prefix));
51+
}
52+
53+
@Override
54+
public boolean containsHintType(String prefix) {
55+
return hints.containsKey(prefix);
56+
}
57+
58+
private String getPrefix(String hint) {
59+
return hint.split("\\.")[0];
60+
}
61+
62+
@Override
63+
public Map<String, String> getHints() {
64+
return hints;
65+
}
66+
67+
@Override
68+
public Hints getCopy() {
69+
return SimplificationHints.generateHints(this);
70+
}
71+
72+
}

0 commit comments

Comments
 (0)