You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by gh...@apache.org on 2006/05/09 14:38:02 UTC

svn commit: r405408 - in /incubator/harmony/enhanced/classlib/trunk/modules/luni/src: main/java/java/util/Calendar.java test/java/tests/api/java/util/CalendarTest.java

Author: gharley
Date: Tue May  9 05:38:00 2006
New Revision: 405408

URL: http://svn.apache.org/viewcvs?rev=405408&view=rev
Log:
HARMONY 447 : java.util.Calendar needs to implement interface Comparable<Calendar>

Modified:
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Calendar.java
    incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/CalendarTest.java

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Calendar.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Calendar.java?rev=405408&r1=405407&r2=405408&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Calendar.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/util/Calendar.java Tue May  9 05:38:00 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -32,8 +32,9 @@
  * @see GregorianCalendar
  * @see TimeZone
  */
-public abstract class Calendar implements Serializable, Cloneable {
-	
+public abstract class Calendar implements Serializable, Cloneable,
+		Comparable<Calendar> {
+
 	private static final long serialVersionUID = -1807547505821590642L;
 
 	/**
@@ -746,6 +747,36 @@
 		}
 		result.append(']');
 		return result.toString();
+	}
+
+	/**
+	 * Compares the times of the two Calendars, which represent the milliseconds
+	 * from the January 1, 1970 00:00:00.000 GMT (Gregorian).
+	 * 
+	 * @param anotherCalendar
+	 *            another calendar that is compared with.
+	 * @return 0 if the times of the two calendar are equal, -1 if the time of
+	 *         this calendar is before the other one, 1 if the time of this
+	 *         calendar is after the other one.
+	 * @throws NullPointerException
+	 *             if the argument of calendar is null.
+	 * @throws IllegalArgumentException
+	 *             if the argument of the calendar does not include a valid time
+	 *             value.
+	 */
+	public int compareTo(Calendar anotherCalendar) {
+		if (null == anotherCalendar) {
+			throw new NullPointerException();
+		}
+		long timeInMillis = getTimeInMillis();
+		long anotherTimeInMillis = anotherCalendar.getTimeInMillis();
+		if (timeInMillis > anotherTimeInMillis) {
+			return 1;
+		}
+		if (timeInMillis == anotherTimeInMillis) {
+			return 0;
+		}
+		return -1;
 	}
 
 	private static final ObjectStreamField[] serialPersistentFields = {

Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/CalendarTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/CalendarTest.java?rev=405408&r1=405407&r2=405408&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/CalendarTest.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/util/CalendarTest.java Tue May  9 05:38:00 2006
@@ -1,4 +1,4 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -376,6 +376,39 @@
 		assertTrue("incorrect fields", cal.get(Calendar.YEAR) == 1999
 				&& cal.get(Calendar.MONTH) == Calendar.NOVEMBER
 				&& cal.get(Calendar.DATE) == 25);
+	}
+
+	/**
+	 * @tests java.util.Calendar#compareTo(Calendar)
+	 */
+	public void test_compareToLjava_util_Calendar_null() {
+		Calendar cal = Calendar.getInstance();
+		try {
+			cal.compareTo(null);
+			fail("should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+	}
+
+	/**
+	 * @tests java.util.Calendar#compareTo(Calendar)
+	 */
+	public void test_compareToLjava_util_Calendar() {
+		Calendar cal = Calendar.getInstance();
+		cal.set(1997, 12, 13, 23, 57);
+		
+		Calendar anotherCal = Calendar.getInstance();
+		anotherCal.set(1997, 12, 13, 23, 57);
+		assertEquals(0, cal.compareTo(anotherCal));
+				
+		anotherCal = Calendar.getInstance();
+		anotherCal.set(1997, 11, 13, 24, 57);
+		assertEquals(1, cal.compareTo(anotherCal));
+
+		anotherCal = Calendar.getInstance();
+		anotherCal.set(1997, 12, 13, 23, 58);
+		assertEquals(-1, cal.compareTo(anotherCal));
 	}
 
 	protected void setUp() {