ICU-6544 make VersionInfo a Comparable

X-SVN-Rev: 24613
This commit is contained in:
Steven R. Loomis 2008-09-19 22:01:11 +00:00
parent fe4e67b5a6
commit 41f11a47de
2 changed files with 53 additions and 1 deletions

View file

@ -187,6 +187,42 @@ public final class VersionInfoTest extends TestFmwk
}
}
/**
* Test Comparable interface
*/
public void TestComparable() {
try {
VersionInfo v = VersionInfo.ICU_VERSION;
String s = "Some String";
if(v.compareTo(s) >0) {
errln("VersionInfo.compareTo(String) returned >0, should have thrown exception");
} else {
errln("VersionInfo.compareTo(String) returned <=0, should have thrown exception");
}
} catch(ClassCastException cce) {
logln("Pass: compareTo(String) returned " + cce.toString());
}
for (int i = 0; i < COMPARE_NOT_EQUAL_STRING_.length; i += 2) {
VersionInfo v1 =
VersionInfo.getInstance(COMPARE_NOT_EQUAL_STRING_[i]);
Object v2 =
VersionInfo.getInstance(COMPARE_NOT_EQUAL_STRING_[i + 1]);
if (v1.compareTo(v2) == 0) {
errln(COMPARE_NOT_EQUAL_STRING_[i] + " should not equal " +
COMPARE_NOT_EQUAL_STRING_[i + 1]);
}
}
for (int i = 0; i < COMPARE_EQUAL_STRING_.length - 1; i ++) {
VersionInfo v1 =
VersionInfo.getInstance(COMPARE_EQUAL_STRING_[i]);
Object v2 =
VersionInfo.getInstance(COMPARE_EQUAL_STRING_[i + 1]);
if (v1.compareTo(v2) != 0) {
errln(COMPARE_EQUAL_STRING_[i] + " should equal " +
COMPARE_EQUAL_STRING_[i + 1]);
}
}
}
// private methods --------------------------------------------------
/**

View file

@ -14,7 +14,7 @@ import java.util.HashMap;
* @author synwee
* @stable ICU 2.6
*/
public final class VersionInfo
public final class VersionInfo implements Comparable
{
// public data members -------------------------------------------------
@ -398,6 +398,21 @@ public final class VersionInfo
return m_version_ - other.m_version_;
}
/**
* Compares other with this VersionInfo.
* @param other VersionInfo to be compared. Throws ClassCastException if not a VersionInfo.
* @return 0 if the argument is a VersionInfo object that has version
* information equals to this object.
* Less than 0 if the argument is a VersionInfo object that has
* version information greater than this object.
* Greater than 0 if the argument is a VersionInfo object that
* has version information less than this object.
* @draft ICU 4.2
*/
public int compareTo(Object other) {
return compareTo((VersionInfo)other);
}
// private data members ----------------------------------------------
/**
@ -473,4 +488,5 @@ public final class VersionInfo
{
return (major << 24) | (minor << 16) | (milli << 8) | micro;
}
}