Skip to content

Commit

Permalink
Improve documentation and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
mnhock committed Sep 1, 2024
1 parent b41d477 commit 13202fd
Show file tree
Hide file tree
Showing 19 changed files with 224 additions and 229 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void shouldFulfillConstraints() {
.constantsShouldFollowConventions()
.interfacesShouldNotHavePrefixI()))
.logging(logging -> logging
.loggersShouldFollowConventions(Logger.class, "logger", EnumSet.of(PRIVATE, FINAL)))
.loggersShouldFollowConventions(Logger.class, "logger", List.of(PRIVATE, FINAL)))
.test(test -> test
.junit5(junit5 -> junit5
.classesShouldNotBeAnnotatedWithDisabled()
Expand Down
186 changes: 90 additions & 96 deletions docs/USERGUIDE.md

Large diffs are not rendered by default.

20 changes: 12 additions & 8 deletions src/main/java/com/enofex/taikai/Taikai.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.enofex.taikai;

import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;
import static java.util.Objects.requireNonNullElse;

Expand All @@ -14,10 +15,8 @@
import com.tngtech.archunit.ArchConfiguration;
import com.tngtech.archunit.core.domain.JavaClasses;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Stream;

Expand All @@ -26,14 +25,14 @@ public final class Taikai {
private final boolean failOnEmpty;
private final String namespace;
private final JavaClasses classes;
private final Set<String> excludedClasses;
private final Collection<String> excludedClasses;
private final Collection<TaikaiRule> rules;

private Taikai(Builder builder) {
this.failOnEmpty = builder.failOnEmpty;
this.namespace = builder.namespace;
this.classes = builder.classes;
this.excludedClasses = requireNonNullElse(builder.excludedClasses, Collections.emptySet());
this.excludedClasses = requireNonNullElse(builder.excludedClasses, emptyList());
this.rules = Stream.concat(
builder.configurers.all().stream().flatMap(configurer -> configurer.rules().stream()),
builder.rules.stream())
Expand All @@ -59,7 +58,7 @@ public JavaClasses classes() {
return this.classes;
}

public Set<String> excludedClasses() {
public Collection<String> excludedClasses() {
return this.excludedClasses;
}

Expand All @@ -83,15 +82,15 @@ public static final class Builder {

private final Configurers configurers;
private final Collection<TaikaiRule> rules;
private final Set<String> excludedClasses;
private final Collection<String> excludedClasses;
private boolean failOnEmpty;
private String namespace;
private JavaClasses classes;

public Builder() {
this.configurers = new Configurers();
this.rules = new ArrayList<>();
this.excludedClasses = new HashSet<>();
this.excludedClasses = new ArrayList<>();
}

public Builder(Taikai taikai) {
Expand Down Expand Up @@ -138,6 +137,11 @@ public Builder excludeClasses(Collection<String> classNames) {
return this;
}

public Builder excludeClasses(String... classNames) {
this.excludedClasses.addAll(Arrays.asList(classNames));
return this;
}

public Builder java(Customizer<JavaConfigurer> customizer) {
return configure(customizer, JavaConfigurer::new);
}
Expand Down
43 changes: 23 additions & 20 deletions src/main/java/com/enofex/taikai/TaikaiRule.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.enofex.taikai;

import static com.enofex.taikai.TaikaiRule.Configuration.defaultConfiguration;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;
import static java.util.Objects.requireNonNull;
import static java.util.Objects.requireNonNullElse;

import com.tngtech.archunit.base.DescribedPredicate;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.lang.ArchRule;
import java.util.Collections;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.Collection;
import java.util.stream.Stream;

public final class TaikaiRule {
Expand All @@ -19,7 +20,7 @@ public final class TaikaiRule {

private TaikaiRule(ArchRule archRule, Configuration configuration) {
this.archRule = requireNonNull(archRule);
this.configuration = requireNonNullElse(configuration, Configuration.defaultConfiguration());
this.configuration = requireNonNullElse(configuration, defaultConfiguration());
}

public ArchRule archRule() {
Expand All @@ -31,18 +32,19 @@ public Configuration configuration() {
}

public static TaikaiRule of(ArchRule archRule) {
return new TaikaiRule(archRule, Configuration.defaultConfiguration());
return new TaikaiRule(archRule, defaultConfiguration());
}

public static TaikaiRule of(ArchRule archRule, Configuration configuration) {
return new TaikaiRule(archRule, configuration);
}

public void check(String globalNamespace) {
check(globalNamespace, null, Collections.emptySet());
check(globalNamespace, null, emptySet());
}

public void check(String globalNamespace, JavaClasses classes, Set<String> excludedClasses) {
public void check(String globalNamespace, JavaClasses classes,
Collection<String> excludedClasses) {
if (this.configuration.javaClasses() != null) {
this.archRule.check(this.configuration.javaClasses());
} else if (classes != null) {
Expand All @@ -56,7 +58,7 @@ public void check(String globalNamespace, JavaClasses classes, Set<String> exclu
throw new TaikaiException("Namespace is not provided");
}

Set<String> allExcludedClasses = allExcludedClasses(excludedClasses);
Collection<String> allExcludedClasses = allExcludedClasses(excludedClasses);

if (allExcludedClasses.isEmpty()) {
this.archRule.check(Namespace.from(namespace, this.configuration.namespaceImport));
Expand All @@ -72,28 +74,28 @@ public boolean test(JavaClass javaClass) {
}
}

private Set<String> allExcludedClasses(Set<String> excludedClasses) {
private Collection<String> allExcludedClasses(Collection<String> excludedClasses) {
return Stream.concat(
this.configuration.excludedClasses != null
? this.configuration.excludedClasses.stream() : Stream.empty(),
excludedClasses != null
? excludedClasses.stream() : Stream.empty())
.collect(Collectors.toSet());
? excludedClasses.stream() : Stream.empty()
).toList();
}

public static final class Configuration {

private final String namespace;
private final Namespace.IMPORT namespaceImport;
private final JavaClasses javaClasses;
private final Set<String> excludedClasses;
private final Collection<String> excludedClasses;

private Configuration(String namespace, Namespace.IMPORT namespaceImport,
JavaClasses javaClasses, Set<String> excludedClasses) {
JavaClasses javaClasses, Collection<String> excludedClasses) {
this.namespace = namespace;
this.namespaceImport = requireNonNullElse(namespaceImport, Namespace.IMPORT.WITHOUT_TESTS);
this.javaClasses = javaClasses;
this.excludedClasses = excludedClasses != null ? excludedClasses : Collections.emptySet();
this.excludedClasses = excludedClasses != null ? excludedClasses : emptyList();
}

public String namespace() {
Expand All @@ -108,7 +110,7 @@ public JavaClasses javaClasses() {
return this.javaClasses;
}

public Set<String> excludedClasses() {
public Collection<String> excludedClasses() {
return this.excludedClasses;
}

Expand All @@ -120,15 +122,16 @@ public static Configuration of(String namespace) {
return new Configuration(namespace, Namespace.IMPORT.WITHOUT_TESTS, null, null);
}

public static Configuration of(String namespace, Set<String> excludedClasses) {
public static Configuration of(String namespace, Collection<String> excludedClasses) {
return new Configuration(namespace, Namespace.IMPORT.WITHOUT_TESTS, null, excludedClasses);
}

public static Configuration of(Namespace.IMPORT namespaceImport) {
return new Configuration(null, namespaceImport, null, null);
}

public static Configuration of(Namespace.IMPORT namespaceImport, Set<String> excludedClasses) {
public static Configuration of(Namespace.IMPORT namespaceImport,
Collection<String> excludedClasses) {
return new Configuration(null, namespaceImport, null, excludedClasses);
}

Expand All @@ -137,19 +140,19 @@ public static Configuration of(String namespace, Namespace.IMPORT namespaceImpor
}

public static Configuration of(String namespace, Namespace.IMPORT namespaceImport,
Set<String> excludedClasses) {
Collection<String> excludedClasses) {
return new Configuration(namespace, namespaceImport, null, excludedClasses);
}

public static Configuration of(JavaClasses javaClasses) {
return new Configuration(null, null, javaClasses, null);
}

public static Configuration of(JavaClasses javaClasses, Set<String> excludedClasses) {
public static Configuration of(JavaClasses javaClasses, Collection<String> excludedClasses) {
return new Configuration(null, null, javaClasses, excludedClasses);
}

public static Configuration of(Set<String> excludedClasses) {
public static Configuration of(Collection<String> excludedClasses) {
return new Configuration(null, null, null, excludedClasses);
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/enofex/taikai/java/ImportsConfigurer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.enofex.taikai.java;

import static com.enofex.taikai.TaikaiRule.Configuration.defaultConfiguration;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
import static com.tngtech.archunit.library.dependencies.SlicesRuleDefinition.slices;

Expand All @@ -16,7 +17,7 @@ public final class ImportsConfigurer extends AbstractConfigurer {
}

public ImportsConfigurer shouldNotImport(String packageIdentifier) {
return shouldNotImport(packageIdentifier, Configuration.defaultConfiguration());
return shouldNotImport(packageIdentifier, defaultConfiguration());
}

public ImportsConfigurer shouldNotImport(String packageIdentifier, Configuration configuration) {
Expand All @@ -28,7 +29,7 @@ public ImportsConfigurer shouldNotImport(String packageIdentifier, Configuration
}

public ImportsConfigurer shouldNotImport(String regex, String notImportClassesRegex) {
return shouldNotImport(regex, notImportClassesRegex, Configuration.defaultConfiguration());
return shouldNotImport(regex, notImportClassesRegex, defaultConfiguration());
}

public ImportsConfigurer shouldNotImport(String regex, String notImportClassesRegex,
Expand Down
Loading

0 comments on commit 13202fd

Please sign in to comment.