Browse Source

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
master
Jesse N. Glick 13 years ago
parent
commit
f7f699feb5
2 changed files with 93 additions and 4 deletions
  1. +25
    -4
      src/main/org/apache/tools/ant/util/DeweyDecimal.java
  2. +68
    -0
      src/tests/junit/org/apache/tools/ant/util/DeweyDecimalTest.java

+ 25
- 4
src/main/org/apache/tools/ant/util/DeweyDecimal.java View File

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

}

+ 68
- 0
src/tests/junit/org/apache/tools/ant/util/DeweyDecimalTest.java View File

@@ -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, ...

}

Loading…
Cancel
Save