Skip to content

Commit

Permalink
Merge pull request #6065 from bstansberry/WFCORE-6889
Browse files Browse the repository at this point in the history
[WFCORE-6889] Add a ModuleDependency.Builder class
  • Loading branch information
yersan authored Jul 18, 2024
2 parents 338cbd9 + cdc6805 commit bf2124b
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,91 @@
*/
public final class ModuleDependency implements Serializable {

public static final class Builder {
private final ModuleLoader moduleLoader;
@SuppressWarnings("deprecation")
private final ModuleIdentifier identifier;
private boolean export;
private boolean optional;
private boolean importServices;
private boolean userSpecified;
private String reason;

public static Builder of(ModuleLoader moduleLoader, String moduleName) {
return new Builder(moduleLoader, moduleName);
}

private Builder(ModuleLoader moduleLoader, String moduleName) {
this.moduleLoader = moduleLoader;
//noinspection deprecation
this.identifier = ModuleIdentifier.create(moduleName);
}

/**
* Sets whether the dependent module should export this dependency's resources.
*
* @param export {@code true} if the dependencies resources should be exported
* @return this builder
*/
public Builder setExport(boolean export) {
this.export = export;
return this;
}

/**
* Sets whether the dependent module should be able to import services from this dependency.
*
* @param importServices {@code true} if the dependent module should be able to load services from the dependency
* @return this builder
*/
public Builder setImportServices(boolean importServices) {
this.importServices = importServices;
return this;
}

/**
* Sets whether this dependency is optional.
*
* @param optional {@code true} if the dependencys is optional
* @return this builder
*/
public Builder setOptional(boolean optional) {
this.optional = optional;
return this;
}

/**
* Sets whether this dependency was explicitly specified by the user.
*
* @param userSpecified {@code true} if this dependency was specified by the user, {@code false} if it was automatically added
* @return this builder
*/
public Builder setUserSpecified(boolean userSpecified) {
this.userSpecified = userSpecified;
return this;
}

/**
* Sets an informational reason describing why this dependency was added.
*
* @param reason the reason. May be {@code null}
* @return this builder
*/
public Builder setReason(String reason) {
this.reason = reason;
return this;
}

/**
* Build a {@code ModuleDependency} using this builder's settings.
*
* @return the {@code ModuleDependency}. Will not return {@code null}
*/
public ModuleDependency build() {
return new ModuleDependency(moduleLoader, identifier, optional, export, importServices, userSpecified, reason);
}
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
Expand Down Expand Up @@ -50,14 +135,17 @@ public String toString() {
private final boolean userSpecified;
private final String reason;

// NOTE: this constructor isn't deprecated because it's used in over 100 places, and perhaps 40+ more if
// the uses of the equivalent c'tor taking ModuleIdentifier make a simple switch to this. Changing all that
// code to use the builder just to clear a deprecation warning is a simple way to introduce bugs.
/**
* Construct a new instance.
*
* @param moduleLoader the module loader of the dependency (if {@code null}, then use the default server module loader)
* @param identifier the module identifier
* @param optional {@code true} if this is an optional dependency
* @param export {@code true} if resources should be exported by default
* @param importServices
* @param importServices {@code true} if the dependent module should be able to load services from the dependency
* @param userSpecified {@code true} if this dependency was specified by the user, {@code false} if it was automatically added
*/
public ModuleDependency(final ModuleLoader moduleLoader, final String identifier, final boolean optional, final boolean export, final boolean importServices, final boolean userSpecified) {
Expand All @@ -71,10 +159,13 @@ public ModuleDependency(final ModuleLoader moduleLoader, final String identifier
* @param identifier the module identifier
* @param optional {@code true} if this is an optional dependency
* @param export {@code true} if resources should be exported by default
* @param importServices
* @param importServices {@code true} if the dependent module should be able to load services from the dependency
* @param userSpecified {@code true} if this dependency was specified by the user, {@code false} if it was automatically added
* @param reason reason for adding implicit module dependency
*
* @deprecated Use a {@link Builder}
*/
@Deprecated(forRemoval = true)
public ModuleDependency(final ModuleLoader moduleLoader, final String identifier, final boolean optional, final boolean export, final boolean importServices, final boolean userSpecified, String reason) {
this(moduleLoader, ModuleIdentifier.create(identifier), optional, export, importServices, userSpecified, reason);
}
Expand All @@ -86,9 +177,12 @@ public ModuleDependency(final ModuleLoader moduleLoader, final String identifier
* @param identifier the module identifier
* @param optional {@code true} if this is an optional dependency
* @param export {@code true} if resources should be exported by default
* @param importServices
* @param importServices {@code true} if the dependent module should be able to load services from the dependency
* @param userSpecified {@code true} if this dependency was specified by the user, {@code false} if it was automatically added
*
* @deprecated Use a {@link Builder} or @link ModuleDependency(ModuleLoader, String, boolean, boolean, boolean, boolean)}
*/
@Deprecated(forRemoval = true)
public ModuleDependency(final ModuleLoader moduleLoader, final ModuleIdentifier identifier, final boolean optional, final boolean export, final boolean importServices, final boolean userSpecified) {
this(moduleLoader, identifier, optional, export, importServices, userSpecified, null);
}
Expand All @@ -100,10 +194,13 @@ public ModuleDependency(final ModuleLoader moduleLoader, final ModuleIdentifier
* @param identifier the module identifier
* @param optional {@code true} if this is an optional dependency
* @param export {@code true} if resources should be exported by default
* @param importServices
* @param importServices {@code true} if the dependent module should be able to load services from the dependency
* @param userSpecified {@code true} if this dependency was specified by the user, {@code false} if it was automatically added
* @param reason reason for adding implicit module dependency
*
* @deprecated Use a {@link Builder}
*/
@Deprecated(forRemoval = true)
public ModuleDependency(final ModuleLoader moduleLoader, final ModuleIdentifier identifier, final boolean optional, final boolean export, final boolean importServices, final boolean userSpecified, String reason) {
this.identifier = identifier;
this.optional = optional;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright The WildFly Authors
* SPDX-License-Identifier: Apache-2.0
*/

package org.jboss.as.server.deployment.module;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.jboss.modules.ModuleLoader;
import org.junit.Test;

public class ModuleDependencyUnitTestCase {

private static final String MODULE_NAME = "foo";
private static final ModuleLoader TEST_LOADER = new ModuleLoader(ModuleLoader.NO_FINDERS);

@Test
public void testBasicBuilder() {
ModuleDependency dep = ModuleDependency.Builder.of(TEST_LOADER, MODULE_NAME).build();
assertEquals(TEST_LOADER, dep.getModuleLoader());
assertEquals(MODULE_NAME, dep.getIdentifier().getName());
assertFalse(dep.isExport());
assertFalse(dep.isImportServices());
assertFalse(dep.isOptional());
assertFalse(dep.isUserSpecified());
assertNotNull(dep.getReason());
assertTrue(dep.getReason().isEmpty());
}

@Test
public void testSpecifiedBuilder() {

ModuleDependency dep = ModuleDependency.Builder.of(TEST_LOADER, MODULE_NAME)
.setExport(true)
.setImportServices(true)
.setOptional(true)
.setUserSpecified(true)
.setReason(MODULE_NAME)
.build();

assertEquals(TEST_LOADER, dep.getModuleLoader());
assertEquals(MODULE_NAME, dep.getIdentifier().getName());
assertTrue(dep.isExport());
assertTrue(dep.isImportServices());
assertTrue(dep.isOptional());
assertTrue(dep.isUserSpecified());
assertFalse(dep.getReason().isEmpty());
assertEquals(MODULE_NAME, dep.getReason().get());
}
}

0 comments on commit bf2124b

Please sign in to comment.