You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by jg...@apache.org on 2012/08/16 14:57:03 UTC
svn commit: r1373811 - in /ant/core/trunk/src:
main/org/apache/tools/ant/util/DeweyDecimal.java
tests/junit/org/apache/tools/ant/util/DeweyDecimalTest.java
Author: jglick
Date: Thu Aug 16 12:57:02 2012
New Revision: 1373811
URL: http://svn.apache.org/viewvc?rev=1373811&view=rev
Log:
Rounding out signature of DeweyDecimal and writing some tests.
Added:
ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/DeweyDecimalTest.java (with props)
Modified:
ant/core/trunk/src/main/org/apache/tools/ant/util/DeweyDecimal.java
Modified: ant/core/trunk/src/main/org/apache/tools/ant/util/DeweyDecimal.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/util/DeweyDecimal.java?rev=1373811&r1=1373810&r2=1373811&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/util/DeweyDecimal.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/util/DeweyDecimal.java Thu Aug 16 12:57:02 2012
@@ -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);
+ }
+
}
Added: ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/DeweyDecimalTest.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/DeweyDecimalTest.java?rev=1373811&view=auto
==============================================================================
--- ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/DeweyDecimalTest.java (added)
+++ ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/DeweyDecimalTest.java Thu Aug 16 12:57:02 2012
@@ -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, ...
+
+}
Propchange: ant/core/trunk/src/tests/junit/org/apache/tools/ant/util/DeweyDecimalTest.java
------------------------------------------------------------------------------
svn:eol-style = native