Skip to content

Commit

Permalink
Rounding out signature of DeweyDecimal and writing some tests.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@1373811 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
jglick committed Aug 16, 2012
1 parent cf92656 commit d8b2342
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 4 deletions.
29 changes: 25 additions & 4 deletions src/main/org/apache/tools/ant/util/DeweyDecimal.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
* 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.
Expand All @@ -58,7 +58,7 @@ public DeweyDecimal(final String string)

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");
}

Expand Down Expand Up @@ -194,7 +194,7 @@ public boolean isGreaterThanOrEqual(final DeweyDecimal other) {
*
* @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++) {
Expand All @@ -206,4 +206,25 @@ public String toString() {

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);
}

}
68 changes: 68 additions & 0 deletions src/tests/junit/org/apache/tools/ant/util/DeweyDecimalTest.java
Original file line number Diff line number Diff line change
@@ -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, ...

}

0 comments on commit d8b2342

Please sign in to comment.