Skip to content

Commit

Permalink
Add Possibility of Modifying an Existing Configuration
Browse files Browse the repository at this point in the history
Closes gh-55
  • Loading branch information
mnhock committed Jun 22, 2024
1 parent c7b0bc3 commit 4ec4cd9
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
27 changes: 27 additions & 0 deletions docs/USERGUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,33 @@ Taikai.builder()
.check();
```

### 3.4 Modifying an Existing Configuration
The `toBuilder` method allows you to create a new Builder instance from an existing Taikai configuration. This is useful if you need to modify an existing configuration.

```java
Taikai taikai = Taikai.builder()
.namespace("com.company.yourproject")
.excludeClass("com.company.yourproject.SomeClassToExclude")
.failOnEmpty(true)
.java(java -> java
.fieldsShouldNotBePublic())
.build();

// Modify the existing configuration
Taikai modifiedTaikai = taikai.toBuilder()
.namespace("com.company.newproject")
.excludeClass("com.company.yourproject.AnotherClassToExclude")
.failOnEmpty(false)
.java(java -> java
.classesShouldImplementHashCodeAndEquals()
.finalClassesShouldNotHaveProtectedMembers())
.build();

// Perform the check with the modified configuration
modifiedTaikai.check();
```


## 4. Rules Overview

Taikai's architecture rules cover a wide range of categories to enforce best practices and maintain consistency.
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/enofex/taikai/Taikai.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,15 @@ public static Builder builder() {
return new Builder();
}

public Builder toBuilder() {
return new Builder(this);
}

public static final class Builder {

private final Configurers configurers;
private final Collection<TaikaiRule> rules;
private final Set<String> excludedClasses;

private boolean failOnEmpty;
private String namespace;

Expand All @@ -88,6 +91,14 @@ public Builder() {
this.excludedClasses = new HashSet<>();
}

public Builder(Taikai taikai) {
this.configurers = new Configurers();
this.rules = taikai.rules();
this.excludedClasses = taikai.excludedClasses();
this.failOnEmpty = taikai.failOnEmpty();
this.namespace = taikai.namespace();
}

public Builder addRule(TaikaiRule rule) {
this.rules.add(rule);
return this;
Expand Down
29 changes: 29 additions & 0 deletions src/test/java/com/enofex/taikai/TaikaiTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

package com.enofex.taikai;

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -135,4 +136,32 @@ void shouldCheckRules() {

verify(mockRule, times(1)).check(VALID_NAMESPACE, Collections.emptySet());
}

@Test
void shouldRebuildTaikaiWithNewValues() {
Taikai taikai = Taikai.builder()
.namespace(VALID_NAMESPACE)
.excludeClass("com.enofex.taikai.ClassToExclude")
.failOnEmpty(true)
.java(java -> java
.fieldsShouldNotBePublic())
.build();

Taikai modifiedTaikai = taikai.toBuilder()
.namespace("com.enofex.newnamespace")
.excludeClass("com.enofex.taikai.AnotherClassToExclude")
.failOnEmpty(false)
.java(java -> java
.classesShouldImplementHashCodeAndEquals()
.finalClassesShouldNotHaveProtectedMembers())
.build();

assertFalse(modifiedTaikai.failOnEmpty());
assertEquals("com.enofex.newnamespace", modifiedTaikai.namespace());
assertEquals(2, modifiedTaikai.excludedClasses().size());
assertEquals(3, modifiedTaikai.rules().size());
assertTrue(modifiedTaikai.excludedClasses().contains("com.enofex.taikai.ClassToExclude"));
assertTrue(
modifiedTaikai.excludedClasses().contains("com.enofex.taikai.AnotherClassToExclude"));
}
}

0 comments on commit 4ec4cd9

Please sign in to comment.