Skip to content

Commit

Permalink
refactoring - extracting static methods in a private utility class fr…
Browse files Browse the repository at this point in the history
…om Bootique
  • Loading branch information
andrus committed Oct 8, 2017
1 parent d7b15b5 commit 71edbad
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 46 deletions.
26 changes: 3 additions & 23 deletions bootique/src/main/java/io/bootique/Bootique.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,6 @@ static Module createModule(Class<? extends Module> moduleType) {
}
}

static String[] mergeArrays(String[] a1, String[] a2) {
if (a1.length == 0) {
return a2;
}

if (a2.length == 0) {
return a1;
}

String[] merged = new String[a1.length + a2.length];
System.arraycopy(a1, 0, merged, 0, a1.length);
System.arraycopy(a2, 0, merged, a1.length, a2.length);

return merged;
}

static String[] toArray(Collection<String> collection) {
return collection.toArray(new String[collection.size()]);
}

/**
* A generic main method that auto-loads available modules and runs Bootique stack. Useful for apps that don't
* care to customize their "main()".
Expand Down Expand Up @@ -123,7 +103,7 @@ public static Bootique app(Collection<String> args) {
args = Collections.emptyList();
}

return app(toArray(Objects.requireNonNull(args)));
return app(BootiqueUtils.toArray(Objects.requireNonNull(args)));
}

/**
Expand Down Expand Up @@ -159,7 +139,7 @@ public Bootique shutdownManager(ShutdownManager shutdownManager) {
*/
public Bootique args(String... args) {
if (args != null) {
this.args = Bootique.mergeArrays(this.args, args);
this.args = BootiqueUtils.mergeArrays(this.args, args);
}
return this;
}
Expand All @@ -173,7 +153,7 @@ public Bootique args(String... args) {
*/
public Bootique args(Collection<String> args) {
if (args != null) {
this.args = Bootique.mergeArrays(this.args, Bootique.toArray(args));
this.args = BootiqueUtils.mergeArrays(this.args, BootiqueUtils.toArray(args));
}
return this;
}
Expand Down
36 changes: 36 additions & 0 deletions bootique/src/main/java/io/bootique/BootiqueUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.bootique;

import java.util.Collection;

/**
* Utils methods which used inside {@link Bootique} class, but can be moved outside.
*
* @since 0.24
*/
final class BootiqueUtils {

private BootiqueUtils() {
throw new AssertionError("Should not be called.");
}

static String[] mergeArrays(String[] a1, String[] a2) {
if (a1.length == 0) {
return a2;
}

if (a2.length == 0) {
return a1;
}

String[] merged = new String[a1.length + a2.length];
System.arraycopy(a1, 0, merged, 0, a1.length);
System.arraycopy(a2, 0, merged, a1.length, a2.length);

return merged;
}

static String[] toArray(Collection<String> collection) {
return collection.toArray(new String[collection.size()]);
}
}

26 changes: 3 additions & 23 deletions bootique/src/test/java/io/bootique/BootiqueStaticsTest.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package io.bootique;

import static java.util.Arrays.asList;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import io.bootique.Bootique;
import org.junit.Test;

import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Module;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class BootiqueStaticsTest {

Expand All @@ -19,22 +15,6 @@ public void testCreateModule() {
assertEquals("tm1", Guice.createInjector(m).getInstance(String.class));
}

@Test
public void testToArray() {
assertArrayEquals(new String[] {}, Bootique.toArray(asList()));
assertArrayEquals(new String[] { "a", "b", "c" }, Bootique.toArray(asList("a", "b", "c")));
}

@Test
public void testMergeArrays() {
assertArrayEquals(new String[] {}, Bootique.mergeArrays(new String[0], new String[0]));
assertArrayEquals(new String[] { "a" }, Bootique.mergeArrays(new String[] { "a" }, new String[0]));
assertArrayEquals(new String[] { "b" }, Bootique.mergeArrays(new String[0], new String[] { "b" }));
assertArrayEquals(new String[] { "b", "c", "d" },
Bootique.mergeArrays(new String[] { "b", "c" }, new String[] { "d" }));

}

static class TestModule implements Module {

@Override
Expand Down
24 changes: 24 additions & 0 deletions bootique/src/test/java/io/bootique/BootiqueUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.bootique;

import org.junit.Test;

import static java.util.Arrays.asList;
import static org.junit.Assert.assertArrayEquals;

public class BootiqueUtilsTest {

@Test
public void testToArray() {
assertArrayEquals(new String[]{}, BootiqueUtils.toArray(asList()));
assertArrayEquals(new String[]{"a", "b", "c"}, BootiqueUtils.toArray(asList("a", "b", "c")));
}

@Test
public void testMergeArrays() {
assertArrayEquals(new String[]{}, BootiqueUtils.mergeArrays(new String[0], new String[0]));
assertArrayEquals(new String[]{"a"}, BootiqueUtils.mergeArrays(new String[]{"a"}, new String[0]));
assertArrayEquals(new String[]{"b"}, BootiqueUtils.mergeArrays(new String[0], new String[]{"b"}));
assertArrayEquals(new String[]{"b", "c", "d"},
BootiqueUtils.mergeArrays(new String[]{"b", "c"}, new String[]{"d"}));
}
}

0 comments on commit 71edbad

Please sign in to comment.