Skip to content

Commit e522f26

Browse files
author
Ashley Frieze
committed
Line endings and JavaDoc
1 parent c188c4a commit e522f26

File tree

3 files changed

+63
-61
lines changed

3 files changed

+63
-61
lines changed

docs/VariablesAndValues.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ E.g.
9494
Supplier<List<String>> list = let(ArrayList::new);
9595

9696
it("can use the object", () -> {
97-
list.get().add("Hello");
98-
assertThat(list.get().get(0), is("Hello"));
97+
list.get().add("Hello");
98+
assertThat(list.get().get(0), is("Hello"));
9999
});
100100
```
101101

@@ -105,7 +105,7 @@ can be replaced by
105105
List<String> list = unbox(let(ArrayList::new), List.class);
106106

107107
it("can use the object as though it was not in a supplier", () -> {
108-
list.add("Hello");
109-
assertThat(list.get(0), is("Hello"));
108+
list.add("Hello");
109+
assertThat(list.get(0), is("Hello"));
110110
});
111111
```

src/main/java/com/greghaskins/spectrum/Unboxer.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,20 @@
1111
* can be used with <code>let</code> and {@link Variable}
1212
*/
1313
public interface Unboxer {
14-
/**
15-
* Provide a proxy object to the contents of a supplier to reduce the number of
16-
* {@link Supplier#get} calls in your code. Note, if using {@link Variable} then you
17-
* will want to keep a reference to the original object so you can use {@link Variable#set}.
18-
* @param supplier supplier of object
19-
* @param asClass target type of the unboxer - must be interface
20-
* @param <T> type within the supplier
21-
* @param <R> type of the unboxer object
22-
* @return a proxy to the contents of the supplier
23-
*/
24-
@SuppressWarnings("unchecked")
25-
static <T extends S, R extends S, S> R unbox(Supplier<T> supplier, Class<S> asClass) {
26-
return (R) Proxy.newProxyInstance(asClass.getClassLoader(), new Class[] {asClass},
27-
(Object proxy, Method method, Object[] args) -> method.invoke(supplier.get(), args));
28-
}
14+
/**
15+
* Provide a proxy object to the contents of a supplier to reduce the number of
16+
* {@link Supplier#get} calls in your code. Note, if using {@link Variable} then you
17+
* will want to keep a reference to the original object so you can use {@link Variable#set}.
18+
* @param supplier supplier of object
19+
* @param asClass target type of the unboxer - must be interface
20+
* @param <T> type within the supplier
21+
* @param <R> (inferred) type of the unboxer object (allows generic types to be preserved)
22+
* @param <S> class of the interface to return
23+
* @return a proxy to the contents of the supplier
24+
*/
25+
@SuppressWarnings("unchecked")
26+
static <T extends S, R extends S, S> R unbox(Supplier<T> supplier, Class<S> asClass) {
27+
return (R) Proxy.newProxyInstance(asClass.getClassLoader(), new Class[] {asClass},
28+
(Object proxy, Method method, Object[] args) -> method.invoke(supplier.get(), args));
29+
}
2930
}

src/test/java/specs/UnboxerSpecs.java

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,53 @@
77
import static org.hamcrest.core.Is.is;
88
import static org.junit.Assert.assertThat;
99

10-
import java.util.ArrayList;
11-
import java.util.List;
12-
1310
import com.greghaskins.spectrum.Spectrum;
1411
import com.greghaskins.spectrum.Variable;
12+
1513
import org.junit.runner.RunWith;
1614

15+
import java.util.ArrayList;
16+
import java.util.List;
17+
1718
@RunWith(Spectrum.class)
1819
public class UnboxerSpecs {
19-
{
20-
describe("Using unboxer on let", () -> {
21-
List<String> list = unbox(let(ArrayList::new), List.class);
22-
23-
it("can use the object as though it was not in a supplier", () -> {
24-
list.add("Hello");
25-
assertThat(list.get(0), is("Hello"));
26-
});
27-
28-
it("can use multi-parameter methods correctly", () -> {
29-
list.add("a");
30-
list.add("b");
31-
list.add(0, "_");
32-
33-
assertThat(list.size(), is(3));
34-
assertThat(list.get(0), is("_"));
35-
});
36-
});
37-
38-
describe("Using unboxer with variable", () -> {
39-
Variable<ArrayList<String>> listVariable = new Variable<>(new ArrayList<>());
40-
List<String> list = unbox(listVariable, List.class);
41-
42-
it("can read the variable contents", () -> {
43-
list.add("World");
44-
assertThat(list.size(), is(1));
45-
assertThat(list.get(0), is("World"));
46-
});
47-
48-
it("can still read the same list in the next spec", () -> {
49-
assertThat(list.size(), is(1));
50-
});
51-
52-
it("can reset the content via the original variable object", () -> {
53-
listVariable.set(new ArrayList<>());
54-
assertThat(list.size(), is(0));
55-
});
56-
});
57-
}
20+
{
21+
describe("Using unboxer on let", () -> {
22+
List<String> list = unbox(let(ArrayList::new), List.class);
23+
24+
it("can use the object as though it was not in a supplier", () -> {
25+
list.add("Hello");
26+
assertThat(list.get(0), is("Hello"));
27+
});
28+
29+
it("can use multi-parameter methods correctly", () -> {
30+
list.add("a");
31+
list.add("b");
32+
list.add(0, "_");
33+
34+
assertThat(list.size(), is(3));
35+
assertThat(list.get(0), is("_"));
36+
});
37+
});
38+
39+
describe("Using unboxer with variable", () -> {
40+
Variable<ArrayList<String>> listVariable = new Variable<>(new ArrayList<>());
41+
List<String> list = unbox(listVariable, List.class);
42+
43+
it("can read the variable contents", () -> {
44+
list.add("World");
45+
assertThat(list.size(), is(1));
46+
assertThat(list.get(0), is("World"));
47+
});
48+
49+
it("can still read the same list in the next spec", () -> {
50+
assertThat(list.size(), is(1));
51+
});
52+
53+
it("can reset the content via the original variable object", () -> {
54+
listVariable.set(new ArrayList<>());
55+
assertThat(list.size(), is(0));
56+
});
57+
});
58+
}
5859
}

0 commit comments

Comments
 (0)