diff options
author | Jesse N. Glick <jglick@apache.org> | 2012-08-16 12:57:02 +0000 |
---|---|---|
committer | Jesse N. Glick <jglick@apache.org> | 2012-08-16 12:57:02 +0000 |
commit | f7f699feb5a3e201af37d7c86e71faad4b2b81a4 (patch) | |
tree | 4dc3627e6af9d332ca5a972258bf6cc3b46bb6ba | |
parent | d8aad6eec289c273806c86e890161042b7c5ecec (diff) | |
download | ant-f7f699feb5a3e201af37d7c86e71faad4b2b81a4.tar.gz |
Rounding out signature of DeweyDecimal and writing some tests.
git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@1373811 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | src/main/org/apache/tools/ant/util/DeweyDecimal.java | 29 | ||||
-rw-r--r-- | src/tests/junit/org/apache/tools/ant/util/DeweyDecimalTest.java | 68 |
2 files changed, 93 insertions, 4 deletions
diff --git a/src/main/org/apache/tools/ant/util/DeweyDecimal.java b/src/main/org/apache/tools/ant/util/DeweyDecimal.java index 72a394020..473eb0545 100644 --- a/src/main/org/apache/tools/ant/util/DeweyDecimal.java +++ b/src/main/org/apache/tools/ant/util/DeweyDecimal.java @@ -28,10 +28,10 @@ import java.util.StringTokenizer; * must begin with a number. * */ -public class DeweyDecimal { +public class DeweyDecimal implements Comparable<DeweyDecimal> { /** Array of components that make up DeweyDecimal */ - private int[] components; + private final int[] components; /** * Construct a DeweyDecimal from an array of integer components. @@ -58,7 +58,7 @@ public class DeweyDecimal { for (int i = 0; i < components.length; i++) { final String component = tokenizer.nextToken(); - if (component.equals("")) { + if (component.length() == 0) { throw new NumberFormatException("Empty component in string"); } @@ -194,7 +194,7 @@ public class DeweyDecimal { * * @return the string representation of DeweyDecimal. */ - public String toString() { + @Override public String toString() { final StringBuffer sb = new StringBuffer(); for (int i = 0; i < components.length; i++) { @@ -206,4 +206,25 @@ public class DeweyDecimal { return sb.toString(); } + + @Override public int compareTo(DeweyDecimal other) { + final int max = Math.max(other.components.length, components.length); + for (int i = 0; i < max; i++) { + final int component1 = (i < components.length) ? components[ i ] : 0; + final int component2 = (i < other.components.length) ? other.components[ i ] : 0; + if (component1 != component2) { + return component1 - component2; + } + } + return 0; + } + + @Override public int hashCode() { + return toString().hashCode(); + } + + @Override public boolean equals(Object o) { + return o instanceof DeweyDecimal && isEqual((DeweyDecimal) o); + } + } diff --git a/src/tests/junit/org/apache/tools/ant/util/DeweyDecimalTest.java b/src/tests/junit/org/apache/tools/ant/util/DeweyDecimalTest.java new file mode 100644 index 000000000..3d0188a09 --- /dev/null +++ b/src/tests/junit/org/apache/tools/ant/util/DeweyDecimalTest.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package org.apache.tools.ant.util; + +import static org.junit.Assert.*; +import org.junit.Test; + +@SuppressWarnings("ResultOfObjectAllocationIgnored") +public class DeweyDecimalTest { + + @Test public void parse() { + assertEquals("1.2.3", new DeweyDecimal("1.2.3").toString()); + } + + @Test(expected=NumberFormatException.class) public void misparseEmpty() { + new DeweyDecimal("1..2"); + } + + @Test(expected=NumberFormatException.class) public void misparseNonNumeric() { + new DeweyDecimal("1.2.3-beta-5"); + } + + @Test(expected=NumberFormatException.class) public void misparseFinalDot() { + new DeweyDecimal("1.2."); + } + + // XXX initial dots, empty string, null, negative numbers, ... + + @Test public void testHashCode() { + assertEquals(new DeweyDecimal("1.2.3").hashCode(), new DeweyDecimal("1.2.3").hashCode()); + } + + @Test public void testEquals() { + assertTrue(new DeweyDecimal("1.2.3").equals(new DeweyDecimal("1.2.3"))); + assertFalse(new DeweyDecimal("1.2.3").equals(new DeweyDecimal("1.2.4"))); + assertTrue(new DeweyDecimal("1.2.0").equals(new DeweyDecimal("1.2"))); + assertTrue(new DeweyDecimal("1.2").equals(new DeweyDecimal("1.2.0"))); + } + + @Test public void compareTo() { + assertTrue(new DeweyDecimal("1.2.3").compareTo(new DeweyDecimal("1.2")) > 0); + assertTrue(new DeweyDecimal("1.2").compareTo(new DeweyDecimal("1.2.3")) < 0); + assertTrue(new DeweyDecimal("1.2.3").compareTo(new DeweyDecimal("1.2.3")) == 0); + assertTrue(new DeweyDecimal("1.2.3").compareTo(new DeweyDecimal("1.1.4")) > 0); + assertTrue(new DeweyDecimal("1.2.3").compareTo(new DeweyDecimal("1.2.2.9")) > 0); + assertTrue(new DeweyDecimal("1.2.0").compareTo(new DeweyDecimal("1.2")) == 0); + assertTrue(new DeweyDecimal("1.2").compareTo(new DeweyDecimal("1.2.0")) == 0); + } + + // XXX isGreaterThan, ... + +} |