Skip to content

Commit

Permalink
feat: add generics support
Browse files Browse the repository at this point in the history
  • Loading branch information
jonas-grgt committed Mar 20, 2024
1 parent b087535 commit 1b66278
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 7 deletions.
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# 👷‍ Bob
Lightweight Builder generator for Java
# 👷‍Bob
🪶Lightweight Builder generator for Java

## Why Bob?

Expand Down Expand Up @@ -37,8 +37,6 @@ public class Car {
this.color = color;
this.price = price;
}

// getters toString and hashcode left out for brevity
}
```

Expand Down Expand Up @@ -123,7 +121,16 @@ public class Cup<T, R extends String> {
Can be used as:

```java
Cup<BigDecimal, String> string = new CupBuilder<BigDecimal, String>().topping("String")
Cup<BigDecimal, String> string = new CupBuilder<BigDecimal, String>().topping("cream")
.contents(BigDecimal.ZERO)
.build();
```

or alternatively:

```java
GenericsAreBuildableBuilder.of(BigDecimal.class, String.class)
.topping("cream")
.contents(BigDecimal.ZERO)
.build();
```
21 changes: 19 additions & 2 deletions src/test/java/io/jonasg/bob/BobFeaturesTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void useConstructorAnnotatedWithBuildableConstructor() {

@Test
void useConstructorWithTheGreatestNumberOfParameters() {
Cute.blackBoxTest()
Cute.blackBoxTest()
.given()
.processors(List.of(BuildableProcessor.class))
.andSourceFiles("/tests/successful-compilation/UseConstructorWithTheGreatestNumberOfParameters/UseConstructorWithTheGreatestNumberOfParameters.java")
Expand All @@ -141,7 +141,24 @@ void useConstructorWithTheGreatestNumberOfParameters() {
.generatedSourceFile("io.jonasg.bob.test.builder.UseConstructorWithTheGreatestNumberOfParametersBuilder")
.matches(
CuteApi.ExpectedFileObjectMatcherKind.BINARY,
JavaFileObjectUtils.readFromResource("/tests/successful-compilation/useConstructorWithTheGreatestNumberOfParameters/Expected_UseConstructorWithTheGreatestNumberOfParameters.java"))
JavaFileObjectUtils.readFromResource("/tests/successful-compilation/UseConstructorWithTheGreatestNumberOfParameters/Expected_UseConstructorWithTheGreatestNumberOfParameters.java"))
.executeTest();
}

@Test
void genericsAreBuildable() {
Cute.blackBoxTest()
.given()
.processors(List.of(BuildableProcessor.class))
.andSourceFiles("/tests/successful-compilation/GenericsAreBuildable/GenericsAreBuildable.java")
.whenCompiled()
.thenExpectThat()
.compilationSucceeds()
.andThat()
.generatedSourceFile("io.jonasg.bob.test.builder.GenericsAreBuildableBuilder")
.matches(
CuteApi.ExpectedFileObjectMatcherKind.BINARY,
JavaFileObjectUtils.readFromResource("/tests/successful-compilation/GenericsAreBuildable/Expected_GenericsAreBuildable.java"))
.executeTest();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.jonasg.bob.test.builder;

import io.jonasg.bob.test.GenericsAreBuildable;
import java.lang.Class;
import java.lang.String;

public final class GenericsAreBuildableBuilder<T, R extends String> {
private T contents;

private R topping;

public GenericsAreBuildableBuilder() {
}

public GenericsAreBuildableBuilder<T, R> contents(T contents) {
this.contents = contents;
return this;
}

public GenericsAreBuildableBuilder<T, R> topping(R topping) {
this.topping = topping;
return this;
}

public GenericsAreBuildable<T, R> build() {
return new GenericsAreBuildable<T, R>(contents, topping);
}

public static <T, R extends String> GenericsAreBuildableBuilder<T, R> of(Class<T> Ttype,
Class<R> Rtype) {
return new GenericsAreBuildableBuilder<>();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.jonasg.bob.test;

import java.lang.String;
import io.jonasg.bob.Buildable;

@Buildable
public class GenericsAreBuildable<T, R extends String> {

private T contents;
private R topping;

public GenericsAreBuildable(T contents, R topping) {
this.contents = contents;
this.topping = topping;
}
}

0 comments on commit 1b66278

Please sign in to comment.