harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r548357 - in /harmony/enhanced/classlib/branches/java6/modules/luni/src: main/java/java/util/Calendar.java test/java/tests/api/java/util/CalendarTest.java
Date Mon, 18 Jun 2007 13:53:51 GMT
Author: tellison
Date: Mon Jun 18 06:53:50 2007
New Revision: 548357

URL: http://svn.apache.org/viewvc?view=rev&rev=548357
Log:
Apply patch HARMONY-4200 ([classlib][luni][java6] New fields/methods in java.util.Calendar
for java6)

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

Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/Calendar.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/Calendar.java?view=diff&rev=548357&r1=548356&r2=548357
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/Calendar.java
(original)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/Calendar.java
Mon Jun 18 06:53:50 2007
@@ -23,6 +23,7 @@
 import java.io.ObjectOutputStream;
 import java.io.ObjectStreamField;
 import java.io.Serializable;
+import java.text.DateFormatSymbols;
 
 /**
  * Calendar is an abstract class which provides the conversion between Dates and
@@ -63,10 +64,36 @@
 	protected boolean isTimeSet;
 
 	/**
+	 * A specifier for all styles.
+	 * 
+	 * @since 1.6
+	 */
+	public static final int ALL_STYLES = 0;
+
+	/**
+	 * A specifier for a short name
+	 * 
+	 * @since 1.6
+	 */
+	public static final int SHORT = 1;
+
+	/**
+	 * A specifier for a long name
+	 * 
+	 * @since 1.6
+	 */
+	public static final int LONG = 2;
+
+	/**
 	 * The time in milliseconds since January 1, 1970.
 	 */
 	protected long time;
 
+	/**
+	 * The version of the serialized data of the class
+	 */
+	int serialVersionOnStream = 1;
+
 	transient int lastTimeFieldSet;
 
 	transient int lastDateFieldSet;
@@ -797,6 +824,149 @@
 			return 0;
 		}
 		return -1;
+	}
+
+	/**
+	 * answers the display name for given field, style and locale
+	 * 
+	 * @param field
+	 *            the field of the calendar
+	 * @param style
+	 *            the style of the name
+	 * @param locale
+	 *            the locale to use
+	 * @return the display name
+	 * @since 1.6
+	 */
+	public String getDisplayName(int field, int style, Locale locale) {
+		if (field < 0 || field >= FIELD_COUNT) {
+			throw new IllegalArgumentException();
+		}
+		if (ALL_STYLES == style) {
+			if (!lenient) {
+				throw new IllegalArgumentException();
+			}
+			style = SHORT;
+		}
+		if (SHORT != style && LONG != style) {
+			throw new IllegalArgumentException();
+		}
+		DateFormatSymbols symbol = new DateFormatSymbols(locale);
+		int fid = get(field);
+		switch (field) {
+		case MONTH:
+			return (LONG == style ? symbol.getMonths()[fid] : symbol
+					.getShortMonths()[fid]);
+		case DAY_OF_WEEK:
+			return (LONG == style ? symbol.getWeekdays()[fid] : symbol
+					.getShortWeekdays()[fid]);
+		case AM_PM:
+			return symbol.getAmPmStrings()[fid];
+		case ERA:
+			return symbol.getEras()[fid];
+		default:
+			return null;
+		}
+	}
+
+	/**
+	 * answers a map of display names for given field, style and locale
+	 * 
+	 * @param field
+	 *            the field of the calendar
+	 * @param style
+	 *            the style of the name
+	 * @param locale
+	 *            the locale to use
+	 * @return a map of display names
+	 * @since 1.6
+	 */
+	public Map<String, Integer> getDisplayNames(int field, int style,
+			Locale locale) {
+		if (field < 0 || field >= FIELD_COUNT) {
+			throw new IllegalArgumentException();
+		}
+		if (ALL_STYLES != style && SHORT != style && LONG != style) {
+			throw new IllegalArgumentException();
+		}
+		complete();
+		DateFormatSymbols symbol = new DateFormatSymbols(locale);
+		Map<String, Integer> ret = new HashMap<String, Integer>();
+		switch (field) {
+		case MONTH:
+			String[] months;
+			switch (style) {
+			case LONG:
+				months = symbol.getMonths();
+				break;
+			case SHORT:
+				months = symbol.getShortMonths();
+				break;
+			case ALL_STYLES:
+				months = symbol.getMonths();
+				for (int i = 0; i < months.length; i++) {
+					if (!months[i].equals("") && months[i] != null) {
+						ret.put(months[i], i);
+					}
+				}
+				months = symbol.getShortMonths();
+				break;
+			default:
+				throw new IllegalArgumentException();
+			}
+			for (int i = 0; i < months.length; i++) {
+				if (!months[i].equals("") && months[i] != null) {
+					ret.put(months[i], i);
+				}
+			}
+			break;
+		case DAY_OF_WEEK:
+			String[] weekDays;
+			switch (style) {
+			case LONG:
+				weekDays = symbol.getWeekdays();
+				break;
+			case SHORT:
+				weekDays = symbol.getShortWeekdays();
+				break;
+			case ALL_STYLES:
+				weekDays = symbol.getWeekdays();
+				for (int i = 0; i < weekDays.length; i++) {
+					if (!weekDays[i].equals("") && weekDays[i] != null) {
+						ret.put(weekDays[i], i);
+					}
+				}
+				weekDays = symbol.getWeekdays();
+				break;
+			default:
+				throw new IllegalArgumentException();
+			}
+			for (int i = 0; i < weekDays.length; i++) {
+				if (!weekDays[i].equals("") && weekDays[i] != null) {
+					ret.put(weekDays[i], i);
+				}
+			}
+			break;
+		case AM_PM:
+			String[] amPms = symbol.getAmPmStrings();
+			for (int i = 0; i < amPms.length; i++) {
+				if (!amPms[i].equals("") && amPms[i] != null) {
+					ret.put(amPms[i], i);
+				}
+			}
+			break;
+		case ERA:
+			String[] eras = symbol.getEras();
+			for (int i = 0; i < eras.length; i++) {
+				if (!eras[i].equals("") && eras[i] != null) {
+					ret.put(eras[i], i);
+				}
+			}
+			break;
+		default:
+			ret = null;
+		}
+		return ret;
 	}
 
 	private static final ObjectStreamField[] serialPersistentFields = {

Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/CalendarTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/CalendarTest.java?view=diff&rev=548357&r1=548356&r2=548357
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/CalendarTest.java
(original)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/test/java/tests/api/java/util/CalendarTest.java
Mon Jun 18 06:53:50 2007
@@ -17,9 +17,14 @@
 
 package tests.api.java.util;
 
+import java.text.DateFormatSymbols;
+import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.List;
 import java.util.Locale;
+import java.util.Map;
 import java.util.TimeZone;
 
 public class CalendarTest extends junit.framework.TestCase {
@@ -449,7 +454,7 @@
         Calendar cal = Calendar.getInstance();
 
         int year = Integer.MIN_VALUE + 71;
-        cal.setTimeZone(TimeZone.getTimeZone("GMT"));;
+		cal.setTimeZone(TimeZone.getTimeZone("GMT"));
         cal.set(Calendar.YEAR, year + 1900);
         cal.set(Calendar.MONTH, Calendar.JANUARY);
         cal.set(Calendar.DATE, 1);
@@ -461,22 +466,10 @@
         assertEquals(6017546357372606464L, cal.getTimeInMillis());
     }
 
-    /**
-     * @tests {@link java.util.Calendar#getActualMaximum(int)}
-     */
-    public void test_getActualMaximum_I() {
-    	Calendar c = new MockCalendar();
-    	assertEquals("should be equal to 0", 0, c.getActualMaximum(0));
-    }
-    
-    /**
-     * @tests {@link java.util.Calendar#getActualMinimum(int)}
-     */
-    public void test_getActualMinimum_I() {
-    	Calendar c = new MockCalendar();
-    	assertEquals("should be equal to 0", 0, c.getActualMinimum(0));
-    }
-
+	private static final Locale[] locales = new Locale[] { Locale.getDefault(),
+			Locale.US, Locale.UK, Locale.TAIWAN, Locale.PRC, Locale.KOREA,
+			Locale.JAPAN, Locale.ITALIAN, Locale.GERMAN, Locale.ENGLISH,
+			Locale.CHINA, Locale.CANADA, Locale.FRANCE };
 
     private class MockCalendar extends Calendar {
 
@@ -521,6 +514,306 @@
 		}
     }
 
+	/**
+	 * @tests {@link java.util.Calendar#getDisplayName(int, int, Locale)}
+	 * @since 1.6
+	 */
+	public void test_getDisplayNameIILjava_util_Locale() {
+		Calendar cal = Calendar.getInstance();
+		for (int field = 0; field < Calendar.FIELD_COUNT; field++) {
+			for (Locale locale : locales) {
+				DateFormatSymbols symbols = new DateFormatSymbols(locale);
+				String value = null;
+				switch (field) {
+				case Calendar.AM_PM:
+					cal.set(Calendar.AM_PM, Calendar.AM);
+					value = symbols.getAmPmStrings()[0];
+					assertEquals(cal.getDisplayName(field, Calendar.SHORT,
+							locale), value);
+					assertEquals(cal.getDisplayName(field, Calendar.LONG,
+							locale), value);
+					cal.set(Calendar.AM_PM, Calendar.PM);
+					value = symbols.getAmPmStrings()[1];
+					assertEquals(cal.getDisplayName(field, Calendar.SHORT,
+							locale), value);
+					assertEquals(cal.getDisplayName(field, Calendar.LONG,
+							locale), value);
+					break;
+				case Calendar.ERA:
+					cal.set(Calendar.ERA, GregorianCalendar.BC);
+					value = symbols.getEras()[0];
+					assertEquals(cal.getDisplayName(field, Calendar.SHORT,
+							locale), value);
+					assertEquals(cal.getDisplayName(field, Calendar.LONG,
+							locale), value);
+					cal.set(Calendar.ERA, GregorianCalendar.AD);
+					value = symbols.getEras()[1];
+					assertEquals(cal.getDisplayName(field, Calendar.SHORT,
+							locale), value);
+					assertEquals(cal.getDisplayName(field, Calendar.LONG,
+							locale), value);
+					break;
+				case Calendar.MONTH:
+					cal.set(Calendar.DAY_OF_MONTH, 1);
+					for (int month = 0; month <= 11; month++) {
+						cal.set(Calendar.MONTH, month);
+						value = symbols.getShortMonths()[month];
+						assertEquals(cal.getDisplayName(field, Calendar.SHORT,
+								locale), value);
+						value = symbols.getMonths()[month];
+						assertEquals(cal.getDisplayName(field, Calendar.LONG,
+								locale), value);
+					}
+					break;
+				case Calendar.DAY_OF_WEEK:
+					for (int day = 1; day <= 7; day++) {
+						cal.set(Calendar.DAY_OF_WEEK, day);
+						value = symbols.getShortWeekdays()[day];
+						assertEquals(cal.getDisplayName(field, Calendar.SHORT,
+								locale), value);
+						value = symbols.getWeekdays()[day];
+						assertEquals(cal.getDisplayName(field, Calendar.LONG,
+								locale), value);
+					}
+					break;
+				default:
+					assertNull(cal
+							.getDisplayName(field, Calendar.SHORT, locale));
+					assertNull(cal.getDisplayName(field, Calendar.LONG, locale));
+				}
+			}
+		}
+
+		cal.setLenient(true);
+
+		try {
+			cal.getDisplayName(-1, Calendar.SHORT, Locale.US);
+			fail("Should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+		try {
+			cal.getDisplayName(Calendar.FIELD_COUNT, Calendar.LONG, Locale.US);
+			fail("Should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+		try {
+			cal.getDisplayName(Calendar.MONTH, -1, Locale.US);
+			fail("Should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+		try {
+			cal.getDisplayName(Calendar.MONTH, 3, Locale.US);
+			fail("Should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+		try {
+			cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, null);
+			fail("Should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+		try {
+			cal.getDisplayName(-1, Calendar.SHORT, null);
+			fail("Should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+		try {
+			cal.getDisplayName(Calendar.MONTH, -1, null);
+			fail("Should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+		// in lenient mode, following cases pass
+		cal.set(Calendar.SECOND, 999);
+		cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US);
+		// test for ALL_STYLES, it is equal to use SHORT
+		for (int field = 0; field < Calendar.FIELD_COUNT; field++) {
+			for (Locale locale : locales) {
+				String result = cal.getDisplayName(field, Calendar.ALL_STYLES,
+						locale);
+				if (field == Calendar.AM_PM || field == Calendar.ERA
+						|| field == Calendar.MONTH
+						|| field == Calendar.DAY_OF_WEEK) {
+					assertEquals(result, cal.getDisplayName(field,
+							Calendar.SHORT, locale));
+				} else {
+					assertNull(result);
+				}
+			}
+		}
+
+		// invalid value for an un-related field when the calendar is not
+		// lenient
+		cal.setLenient(false);
+		assertNotNull(cal.getDisplayName(Calendar.MONTH, Calendar.SHORT,
+				Locale.US));
+		cal.set(Calendar.SECOND, 999);
+		try {
+			cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.US);
+			fail("Should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+		try {
+			cal.getDisplayName(Calendar.MONTH, Calendar.ALL_STYLES, Locale.US);
+			fail("Should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+	}
+
+	/**
+	 * @tests {@link java.util.Calendar#getDisplayNames(int, int, Locale)}
+	 * @since 1.6
+	 */
+	public void test_getDisplayNamesIILjava_util_Locale() {
+		assertEquals(0, Calendar.ALL_STYLES);
+		assertEquals(1, Calendar.SHORT);
+		assertEquals(2, Calendar.LONG);
+
+		Calendar cal = Calendar.getInstance(Locale.US);
+
+		for (int field = 0; field < Calendar.FIELD_COUNT; field++) {
+			for (Locale locale : locales) {
+				Map<String, Integer> shortResult = cal.getDisplayNames(field,
+						Calendar.SHORT, locale);
+				Map<String, Integer> longResult = cal.getDisplayNames(field,
+						Calendar.LONG, locale);
+				Map<String, Integer> allResult = cal.getDisplayNames(field,
+						Calendar.ALL_STYLES, locale);
+				DateFormatSymbols symbols = new DateFormatSymbols(locale);
+				String[] values = null;
+				switch (field) {
+				case Calendar.AM_PM:
+				case Calendar.ERA:
+					values = (field == Calendar.AM_PM) ? symbols
+							.getAmPmStrings() : symbols.getEras();
+					assertDisplayNameMap(values, shortResult, 0);
+					assertDisplayNameMap(values, longResult, 0);
+					assertDisplayNameMap(values, allResult, 0);
+					break;
+				case Calendar.MONTH:
+					values = symbols.getShortMonths();
+					assertDisplayNameMap(values, shortResult, 0);
+					values = symbols.getMonths();
+					assertDisplayNameMap(values, longResult, 0);
+					assertTrue(allResult.size() >= shortResult.size());
+					assertTrue(allResult.size() >= longResult.size());
+					assertTrue(allResult.size() <= shortResult.size()
+							+ longResult.size());
+					break;
+				case Calendar.DAY_OF_WEEK:
+					values = symbols.getShortWeekdays();
+					assertDisplayNameMap(values, shortResult, 1);
+					values = symbols.getWeekdays();
+					assertDisplayNameMap(values, longResult, 1);
+					assertTrue(allResult.size() >= shortResult.size());
+					assertTrue(allResult.size() >= longResult.size());
+					assertTrue(allResult.size() <= shortResult.size()
+							+ longResult.size());
+					break;
+				default:
+					assertNull(shortResult);
+					assertNull(longResult);
+					assertNull(allResult);
+				}
+			}
+		}
+
+		cal.setLenient(true);
+
+		try {
+			cal.getDisplayNames(-1, Calendar.SHORT, Locale.US);
+			fail("Should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+		try {
+			cal.getDisplayNames(Calendar.FIELD_COUNT, Calendar.LONG, Locale.US);
+			fail("Should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+		try {
+			cal.getDisplayNames(Calendar.MONTH, -1, Locale.US);
+			fail("Should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+		try {
+			cal.getDisplayNames(Calendar.MONTH, 3, Locale.US);
+			fail("Should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+		try {
+			cal.getDisplayNames(Calendar.MONTH, Calendar.SHORT, null);
+			fail("Should throw NullPointerException");
+		} catch (NullPointerException e) {
+			// expected
+		}
+		try {
+			cal.getDisplayNames(-1, Calendar.SHORT, null);
+			fail("Should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+		try {
+			cal.getDisplayNames(Calendar.MONTH, -1, null);
+			fail("Should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+		cal.set(Calendar.SECOND, 999);
+		cal.getDisplayNames(Calendar.MONTH, Calendar.SHORT, Locale.US);
+
+		// RI fails here
+		// invalid value for an un-related field when the calendar is not
+		// lenient
+		cal.setLenient(false);
+		cal.set(Calendar.SECOND, 999);
+		try {
+			cal.getDisplayNames(Calendar.MONTH, Calendar.SHORT, Locale.US);
+			fail("Should throw IllegalArgumentException");
+		} catch (IllegalArgumentException e) {
+			// expected
+		}
+	}
+
+	private void assertDisplayNameMap(String[] values,
+			Map<String, Integer> result, int shift) {
+		List<String> trimValue = new ArrayList<String>();
+		for (String value : values) {
+			if (value.trim().length() > 0) {
+				trimValue.add(value);
+			}
+		}
+		assertEquals(trimValue.size(), result.size());
+		for (int i = 0; i < trimValue.size(); i++) {
+			assertEquals(i + shift, result.get(trimValue.get(i)).intValue());
+		}
+	}
+
+	/**
+	 * @tests {@link java.util.Calendar#getActualMaximum(int)}
+	 */
+	public void test_getActualMaximum_I() {
+		Calendar c = new MockCalendar();
+		assertEquals("should be equal to 0", 0, c.getActualMaximum(0));
+	}
+
+	/**
+	 * @tests {@link java.util.Calendar#getActualMinimum(int)}
+	 */
+	public void test_getActualMinimum_I() {
+		Calendar c = new MockCalendar();
+		assertEquals("should be equal to 0", 0, c.getActualMinimum(0));
+	}
 
 	protected void setUp() {
 		defaultLocale = Locale.getDefault();



Mime
View raw message