Cleanup JavaVersion

This commit includes a few minor cleanups to o/e/b/JavaVersion.java:
 - Stronger argument checking in JavaVersion#parse
 - Use JDK 8 string joiner
 - Keep an immutable copy of the version sequence
This commit is contained in:
Jason Tedor 2016-02-08 09:51:37 -05:00
parent 4c9e1361fc
commit ef88943e6e
2 changed files with 11 additions and 11 deletions

View File

@ -19,28 +19,26 @@
package org.elasticsearch.bootstrap; package org.elasticsearch.bootstrap;
import org.elasticsearch.common.Strings;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
class JavaVersion implements Comparable<JavaVersion> { class JavaVersion implements Comparable<JavaVersion> {
private final List<Integer> version; private final List<Integer> version;
public List<Integer> getVersion() { public List<Integer> getVersion() {
return Collections.unmodifiableList(version); return version;
} }
private JavaVersion(List<Integer> version) { private JavaVersion(List<Integer> version) {
this.version = version; this.version = Collections.unmodifiableList(version);
} }
public static JavaVersion parse(String value) { public static JavaVersion parse(String value) {
if (value == null) { Objects.requireNonNull(value);
throw new NullPointerException("value"); if (!isValid(value)) {
}
if ("".equals(value)) {
throw new IllegalArgumentException("value"); throw new IllegalArgumentException("value");
} }
@ -79,6 +77,6 @@ class JavaVersion implements Comparable<JavaVersion> {
@Override @Override
public String toString() { public String toString() {
return Strings.collectionToDelimitedString(version, "."); return version.stream().map(v -> Integer.toString(v)).collect(Collectors.joining("."));
} }
} }

View File

@ -36,8 +36,10 @@ public class JavaVersionTests extends ESTestCase {
} }
public void testToString() { public void testToString() {
JavaVersion javaVersion = JavaVersion.parse("1.7.0"); JavaVersion javaVersion170 = JavaVersion.parse("1.7.0");
assertThat("1.7.0", is(javaVersion.toString())); assertThat(javaVersion170.toString(), is("1.7.0"));
JavaVersion javaVersion9 = JavaVersion.parse("9");
assertThat(javaVersion9.toString(), is("9"));
} }
public void testCompare() { public void testCompare() {