Skip to content

Commit af3f787

Browse files
committed
Add Bosk.simple factory method
1 parent 224ca7a commit af3f787

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

bosk-core/src/main/java/works/bosk/Bosk.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public class Bosk<R extends StateTreeNode> implements BoskInfo<R> {
100100
private volatile R currentRoot;
101101

102102
/**
103-
* @param name Any string that identifies this object.
103+
* @param name A distinctive identifier string. The bosk framework doesn't use this, so there are no requirements on this string: it can be anything that identifies the object.
104104
* @param rootType The @{link Type} of the root node of the state tree, whose {@link Reference#path path} is <code>"/"</code>.
105105
* @param defaultRootFunction The root object to use if the driver chooses not to supply one,
106106
* and instead delegates {@link BoskDriver#initialRoot} all the way to the local driver.
@@ -155,6 +155,18 @@ public Bosk(String name, Type rootType, DefaultRootFunction<R> defaultRootFuncti
155155
this(name, rootType, defaultRootFunction, driverFactory, Bosk.simpleRegistrar());
156156
}
157157

158+
/**
159+
* Convenience method to create a bosk with only the basic functionality,
160+
* to get going quickly.
161+
* To customize the bosk behaviour later,
162+
* you can inline this into your call site and modify it as desired.
163+
* @param name A distinctive identifier string. The bosk framework doesn't use this, so there are no requirements on this string: it can be anything that identifies the object.
164+
* @param initialRoot The starting value of the bosk state tree, before any updates.
165+
*/
166+
public static <RR extends StateTreeNode> Bosk<RR> simple(String name, RR initialRoot) {
167+
return new Bosk<>(requireNonNull(name), initialRoot.getClass(), b -> initialRoot, simpleDriver(), simpleRegistrar());
168+
}
169+
158170
public interface DefaultRootFunction<RR extends StateTreeNode> {
159171
RR apply(Bosk<RR> bosk) throws InvalidTypeException;
160172
}

bosk-core/src/test/java/works/bosk/BoskConstructorTest.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,9 @@ void driverInitialRoot_matches() {
114114
@Test
115115
void defaultRoot_matches() {
116116
SimpleTypes root = newEntity();
117-
{
118-
Bosk<StateTreeNode> valueBosk = new Bosk<>(boskName(), SimpleTypes.class, _ -> root, Bosk.simpleDriver(), Bosk.simpleRegistrar());
119-
try (var _ = valueBosk.readContext()) {
120-
assertSame(root, valueBosk.rootReference().value());
121-
}
122-
}
123-
124-
{
125-
Bosk<StateTreeNode> functionBosk = new Bosk<StateTreeNode>(boskName(), SimpleTypes.class, _ -> root, Bosk.simpleDriver(), Bosk.simpleRegistrar());
126-
try (var _ = functionBosk.readContext()) {
127-
assertSame(root, functionBosk.rootReference().value());
128-
}
117+
Bosk<StateTreeNode> valueBosk = Bosk.simple(boskName(), root);
118+
try (var _ = valueBosk.readContext()) {
119+
assertSame(root, valueBosk.rootReference().value());
129120
}
130121
}
131122

0 commit comments

Comments
 (0)