Fixed AddonDescription to use a valid Builder pattern

AddonDescriptionBuilder has been renamed to AddonDescription.Builder ; and there is no longer public constructors in this class.

This change is obviously API breaking, but it's relatively "internal", so hopefully people didn't use it.
This commit is contained in:
Florian CUNY 2019-01-02 17:28:11 +01:00
parent 30c20216fa
commit c7a85c2b5a
4 changed files with 81 additions and 148 deletions

View File

@ -16,7 +16,6 @@ import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.InvalidDescriptionException; import org.bukkit.plugin.InvalidDescriptionException;
import org.bukkit.util.permissions.DefaultPermissions; import org.bukkit.util.permissions.DefaultPermissions;
import world.bentobox.bentobox.api.addons.AddonDescription.AddonDescriptionBuilder;
import world.bentobox.bentobox.api.addons.exceptions.InvalidAddonFormatException; import world.bentobox.bentobox.api.addons.exceptions.InvalidAddonFormatException;
import world.bentobox.bentobox.api.addons.exceptions.InvalidAddonInheritException; import world.bentobox.bentobox.api.addons.exceptions.InvalidAddonInheritException;
import world.bentobox.bentobox.managers.AddonsManager; import world.bentobox.bentobox.managers.AddonsManager;
@ -83,18 +82,19 @@ public class AddonClassLoader extends URLClassLoader {
} }
private AddonDescription asDescription(YamlConfiguration data) { private AddonDescription asDescription(YamlConfiguration data) {
AddonDescriptionBuilder adb = new AddonDescriptionBuilder(data.getString("name")) AddonDescription.Builder builder = new AddonDescription.Builder(data.getString("main"), data.getString("name"))
.withVersion(data.getString("version")) .version(data.getString("version"))
.withAuthor(data.getString("authors")); .authors(data.getString("authors"));
if (data.getString("depend") != null) { if (data.getString("depend") != null) {
adb.withDepend(Arrays.asList(data.getString("depend").split("\\s*,\\s*"))); builder.dependencies(Arrays.asList(data.getString("depend").split("\\s*,\\s*")));
} }
if (data.getString("softdepend") != null) { if (data.getString("softdepend") != null) {
adb.withSoftDepend(Arrays.asList(data.getString("softdepend").split("\\s*,\\s*"))); builder.softDependencies(Arrays.asList(data.getString("softdepend").split("\\s*,\\s*")));
} }
return adb.build();
}
return builder.build();
}
/* (non-Javadoc) /* (non-Javadoc)
* @see java.net.URLClassLoader#findClass(java.lang.String) * @see java.net.URLClassLoader#findClass(java.lang.String)

View File

@ -5,7 +5,7 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
/** /**
* @author Tastybento, Poslovitch * @author tastybento, Poslovitch
*/ */
public final class AddonDescription { public final class AddonDescription {
@ -13,83 +13,18 @@ public final class AddonDescription {
private String name; private String name;
private String version; private String version;
private String description; private String description;
private List<String> authors = new ArrayList<>(); private List<String> authors;
private List<String> dependencies = new ArrayList<>(); private List<String> dependencies;
private List<String> softDependencies = new ArrayList<>(); private List<String> softDependencies;
public AddonDescription() {} private AddonDescription(Builder builder) {
this.main = builder.main;
public AddonDescription(String main, String name, String version, String description, List<String> authors, List<String> dependencies, List<String> softDependencies) { this.name = builder.name;
this.main = main; this.version = builder.version;
this.name = name; this.description = builder.description;
this.version = version; this.authors = builder.authors;
this.description = description; this.dependencies = builder.dependencies;
this.authors = authors; this.softDependencies = builder.softDependencies;
this.dependencies = dependencies;
this.softDependencies = softDependencies;
}
/**
* @param main the main to set
*/
public void setMain(String main) {
this.main = main;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param version the version to set
*/
public void setVersion(String version) {
this.version = version;
}
/**
* @param description the description to set
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @param authors the authors to set
*/
public void setAuthors(List<String> authors) {
this.authors = authors;
}
/**
* @return the dependencies
*/
public List<String> getDependencies() {
return dependencies;
}
/**
* @return the softDependencies
*/
public List<String> getSoftDependencies() {
return softDependencies;
}
/**
* @param dependencies the dependencies to set
*/
public void setDependencies(List<String> dependencies) {
this.dependencies = dependencies;
}
/**
* @param softDependencies the softDependencies to set
*/
public void setSoftDependencies(List<String> softDependencies) {
this.softDependencies = softDependencies;
} }
public String getName() { public String getName() {
@ -112,44 +47,61 @@ public final class AddonDescription {
return authors; return authors;
} }
public static class AddonDescriptionBuilder{ /**
* @return the dependencies
private AddonDescription description; */
public List<String> getDependencies() {
public AddonDescriptionBuilder(String name){ return dependencies;
description = new AddonDescription();
description.setName(name);
}
public AddonDescriptionBuilder withAuthor(String... authors){
description.setAuthors(Arrays.asList(authors));
return this;
}
public AddonDescriptionBuilder withDescription(String desc){
description.setDescription(desc);
return this;
}
public AddonDescriptionBuilder withVersion(String version){
description.setVersion(version);
return this;
}
public AddonDescriptionBuilder withDepend(List<String> addons) {
description.setDependencies(addons);
return this;
}
public AddonDescriptionBuilder withSoftDepend(List<String> addons) {
description.setSoftDependencies(addons);
return this;
}
public AddonDescription build(){
return description;
}
} }
/**
* @return the softDependencies
*/
public List<String> getSoftDependencies() {
return softDependencies;
}
public static class Builder {
private String main;
private String name;
private String version;
private String description;
private List<String> authors = new ArrayList<>();
private List<String> dependencies = new ArrayList<>();
private List<String> softDependencies = new ArrayList<>();
public Builder(String main, String name) {
this.main = main;
this.name = name;
}
public Builder version(String version) {
this.version = version;
return this;
}
public Builder description(String description) {
this.description = description;
return this;
}
public Builder authors(String... authors) {
this.authors = Arrays.asList(authors);
return this;
}
public Builder dependencies(List<String> dependencies) {
this.dependencies = dependencies;
return this;
}
public Builder softDependencies(List<String> softDependencies) {
this.softDependencies = softDependencies;
return this;
}
public AddonDescription build() {
return new AddonDescription(this);
}
}
} }

View File

@ -44,7 +44,6 @@ public class AddonTest {
static BentoBox plugin; static BentoBox plugin;
static JavaPlugin javaPlugin; static JavaPlugin javaPlugin;
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
Server server = mock(Server.class); Server server = mock(Server.class);
@ -69,21 +68,14 @@ public class AddonTest {
when(server.getItemFactory()).thenReturn(itemFactory); when(server.getItemFactory()).thenReturn(itemFactory);
ItemMeta itemMeta = mock(ItemMeta.class); ItemMeta itemMeta = mock(ItemMeta.class);
when(itemFactory.getItemMeta(any())).thenReturn(itemMeta); when(itemFactory.getItemMeta(any())).thenReturn(itemMeta);
} }
class TestClass extends Addon { class TestClass extends Addon {
@Override
public void onEnable() { }
@Override @Override
public void onEnable() { public void onDisable() { }
}
@Override
public void onDisable() {
}
} }
@Test @Test
@ -118,7 +110,7 @@ public class AddonTest {
@Test @Test
public void testGetDescription() { public void testGetDescription() {
TestClass test = new TestClass(); TestClass test = new TestClass();
AddonDescription d = new AddonDescription(); AddonDescription d = new AddonDescription.Builder("main", "name").build();
assertNull(test.getDescription()); assertNull(test.getDescription());
test.setDescription(d); test.setDescription(d);
assertEquals(d, test.getDescription()); assertEquals(d, test.getDescription());
@ -159,14 +151,6 @@ public class AddonTest {
test.registerListener(listener); test.registerListener(listener);
} }
@Test
public void testSaveConfig() {
//TestClass test = new TestClass();
// This will wipe out the config.yml of BSB so I am commenting it out
//test.saveConfig();
}
@Test @Test
public void testSaveDefaultConfig() { public void testSaveDefaultConfig() {
TestClass test = new TestClass(); TestClass test = new TestClass();
@ -210,7 +194,6 @@ public class AddonTest {
test.saveResource("no_such_file", jarFile, false, true); test.saveResource("no_such_file", jarFile, false, true);
test.saveResource("no_such_file", jarFile, true, false); test.saveResource("no_such_file", jarFile, true, false);
test.saveResource("no_such_file", jarFile, true, true); test.saveResource("no_such_file", jarFile, true, true);
} }
@Test @Test
@ -242,7 +225,7 @@ public class AddonTest {
@Test @Test
public void testSetDescription() { public void testSetDescription() {
TestClass test = new TestClass(); TestClass test = new TestClass();
AddonDescription desc = new AddonDescription(); AddonDescription desc = new AddonDescription.Builder("main", "name").build();
test.setDescription(desc); test.setDescription(desc);
assertEquals(desc, test.getDescription()); assertEquals(desc, test.getDescription());
} }
@ -279,5 +262,4 @@ public class AddonTest {
TestClass test = new TestClass(); TestClass test = new TestClass();
assertEquals(Optional.empty(),test.getAddonByName("addon")); assertEquals(Optional.empty(),test.getAddonByName("addon"));
} }
} }

View File

@ -299,8 +299,7 @@ public class LocalesManagerTest {
AddonsManager am = mock(AddonsManager.class); AddonsManager am = mock(AddonsManager.class);
List<Addon> none = new ArrayList<>(); List<Addon> none = new ArrayList<>();
Addon addon = mock(Addon.class); Addon addon = mock(Addon.class);
AddonDescription desc = new AddonDescription(); AddonDescription desc = new AddonDescription.Builder("", "AcidIsland").build();
desc.setName("AcidIsland");
when(addon.getDescription()).thenReturn(desc); when(addon.getDescription()).thenReturn(desc);
// Create a tmp folder to jar up // Create a tmp folder to jar up
File localeDir = new File(LOCALE_FOLDER); File localeDir = new File(LOCALE_FOLDER);