/** * <p>Constructs an <code>Iterator</code> over each day in a date * range defined by a focus date and range style.</p> * * <p>For instance, passing Thursday, July 4, 2002 and a * <code>RANGE_MONTH_SUNDAY</code> will return an <code>Iterator</code> * that starts with Sunday, June 30, 2002 and ends with Saturday, August 3, * 2002, returning a Calendar instance for each intermediate day.</p> * * @param focus the date to work with, either {@code Date} or {@code Calendar}, not null * @param rangeStyle the style constant to use. Must be one of the range * styles listed for the {@link #iterator(Calendar, int)} method. * @return the date iterator, not null * @throws IllegalArgumentException if the date is <code>null</code> * @throws ClassCastException if the object type is not a {@code Date} or {@code Calendar} */ public static Iterator<?> iterator(final Object focus, final int rangeStyle) { if (focus == null) { throw new IllegalArgumentException("The date must not be null"); } if (focus instanceof Date) { return iterator((Date) focus, rangeStyle); } else if (focus instanceof Calendar) { return iterator((Calendar) focus, rangeStyle); } else { throw new ClassCastException("Could not iterate based on " + focus); } }
/** * <p>Constructs an <code>Iterator</code> over each day in a date * range defined by a focus date and range style.</p> * * <p>For instance, passing Thursday, July 4, 2002 and a * <code>RANGE_MONTH_SUNDAY</code> will return an <code>Iterator</code> * that starts with Sunday, June 30, 2002 and ends with Saturday, August 3, * 2002, returning a Calendar instance for each intermediate day.</p> * * <p>This method provides an iterator that returns Calendar objects. * The days are progressed using {@link Calendar#add(int, int)}.</p> * * @param focus the date to work with, not null * @param rangeStyle the style constant to use. Must be one of * {@link DateUtils#RANGE_MONTH_SUNDAY}, * {@link DateUtils#RANGE_MONTH_MONDAY}, * {@link DateUtils#RANGE_WEEK_SUNDAY}, * {@link DateUtils#RANGE_WEEK_MONDAY}, * {@link DateUtils#RANGE_WEEK_RELATIVE}, * {@link DateUtils#RANGE_WEEK_CENTER} * @return the date iterator, not null, not null * @throws IllegalArgumentException if the date is <code>null</code> * @throws IllegalArgumentException if the rangeStyle is invalid */ public static Iterator<Calendar> iterator(final Date focus, final int rangeStyle) { validateDateNotNull(focus); final Calendar gval = Calendar.getInstance(); gval.setTime(focus); return iterator(gval, rangeStyle); }
/** * Tests the iterator exceptions * * @throws java.lang.Exception so we don't have to catch it */ @Test public void testIteratorEx() throws Exception { try { DateUtils.iterator(Calendar.getInstance(), -9999); } catch (final IllegalArgumentException ex) {} try { DateUtils.iterator((Date) null, DateUtils.RANGE_WEEK_CENTER); fail(); } catch (final IllegalArgumentException ex) {} try { DateUtils.iterator((Calendar) null, DateUtils.RANGE_WEEK_CENTER); fail(); } catch (final IllegalArgumentException ex) {} try { DateUtils.iterator((Object) null, DateUtils.RANGE_WEEK_CENTER); fail(); } catch (final IllegalArgumentException ex) {} try { DateUtils.iterator("", DateUtils.RANGE_WEEK_CENTER); fail(); } catch (final ClassCastException ex) {} }
/** * Tests the calendar iterator for month-based ranges * * @throws java.lang.Exception so we don't have to catch it */ @Test public void testMonthIterator() throws Exception { Iterator<?> it = DateUtils.iterator(date1, DateUtils.RANGE_MONTH_SUNDAY); assertWeekIterator(it, dateParser.parse("January 27, 2002"), dateParser.parse("March 2, 2002")); it = DateUtils.iterator(date1, DateUtils.RANGE_MONTH_MONDAY); assertWeekIterator(it, dateParser.parse("January 28, 2002"), dateParser.parse("March 3, 2002")); it = DateUtils.iterator(date2, DateUtils.RANGE_MONTH_SUNDAY); assertWeekIterator(it, dateParser.parse("October 28, 2001"), dateParser.parse("December 1, 2001")); it = DateUtils.iterator(date2, DateUtils.RANGE_MONTH_MONDAY); assertWeekIterator(it, dateParser.parse("October 29, 2001"), dateParser.parse("December 2, 2001")); }
centered.add(Calendar.DATE, -3); Iterator<?> it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_SUNDAY); assertWeekIterator(it, sunday); it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_MONDAY); assertWeekIterator(it, monday); it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_RELATIVE); assertWeekIterator(it, today); it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_CENTER); assertWeekIterator(it, centered); it = DateUtils.iterator((Object) now, DateUtils.RANGE_WEEK_CENTER); assertWeekIterator(it, centered); it = DateUtils.iterator((Object) now.getTime(), DateUtils.RANGE_WEEK_CENTER); assertWeekIterator(it, centered); try { fail(); } catch (final NoSuchElementException ex) {} it = DateUtils.iterator(now, DateUtils.RANGE_WEEK_CENTER); it.next(); try {
/** * <p>Constructs an <code>Iterator</code> over each day in a date * range defined by a focus date and range style.</p> * * <p>For instance, passing Thursday, July 4, 2002 and a * <code>RANGE_MONTH_SUNDAY</code> will return an <code>Iterator</code> * that starts with Sunday, June 30, 2002 and ends with Saturday, August 3, * 2002, returning a Calendar instance for each intermediate day.</p> * * @param focus the date to work with, either {@code Date} or {@code Calendar}, not null * @param rangeStyle the style constant to use. Must be one of the range * styles listed for the {@link #iterator(Calendar, int)} method. * @return the date iterator, not null * @throws IllegalArgumentException if the date is <code>null</code> * @throws ClassCastException if the object type is not a {@code Date} or {@code Calendar} */ public static Iterator<?> iterator(final Object focus, final int rangeStyle) { if (focus == null) { throw new IllegalArgumentException("The date must not be null"); } if (focus instanceof Date) { return iterator((Date) focus, rangeStyle); } else if (focus instanceof Calendar) { return iterator((Calendar) focus, rangeStyle); } else { throw new ClassCastException("Could not iterate based on " + focus); } }
/** * <p>Constructs an <code>Iterator</code> over each day in a date * range defined by a focus date and range style.</p> * * <p>For instance, passing Thursday, July 4, 2002 and a * <code>RANGE_MONTH_SUNDAY</code> will return an <code>Iterator</code> * that starts with Sunday, June 30, 2002 and ends with Saturday, August 3, * 2002, returning a Calendar instance for each intermediate day.</p> * * @param focus the date to work with, either {@code Date} or {@code Calendar}, not null * @param rangeStyle the style constant to use. Must be one of the range * styles listed for the {@link #iterator(Calendar, int)} method. * @return the date iterator, not null * @throws IllegalArgumentException if the date is <code>null</code> * @throws ClassCastException if the object type is not a {@code Date} or {@code Calendar} */ public static Iterator<?> iterator(final Object focus, final int rangeStyle) { if (focus == null) { throw new IllegalArgumentException("The date must not be null"); } if (focus instanceof Date) { return iterator((Date) focus, rangeStyle); } else if (focus instanceof Calendar) { return iterator((Calendar) focus, rangeStyle); } else { throw new ClassCastException("Could not iterate based on " + focus); } }
/** * <p>Constructs an <code>Iterator</code> over each day in a date * range defined by a focus date and range style.</p> * * <p>For instance, passing Thursday, July 4, 2002 and a * <code>RANGE_MONTH_SUNDAY</code> will return an <code>Iterator</code> * that starts with Sunday, June 30, 2002 and ends with Saturday, August 3, * 2002, returning a Calendar instance for each intermediate day.</p> * * @param focus the date to work with, either {@code Date} or {@code Calendar}, not null * @param rangeStyle the style constant to use. Must be one of the range * styles listed for the {@link #iterator(Calendar, int)} method. * @return the date iterator, not null * @throws IllegalArgumentException if the date is <code>null</code> * @throws ClassCastException if the object type is not a {@code Date} or {@code Calendar} */ public static Iterator<?> iterator(final Object focus, final int rangeStyle) { if (focus == null) { throw new IllegalArgumentException("The date must not be null"); } if (focus instanceof Date) { return iterator((Date) focus, rangeStyle); } else if (focus instanceof Calendar) { return iterator((Calendar) focus, rangeStyle); } else { throw new ClassCastException("Could not iterate based on " + focus); } }
/** * <p>Constructs an <code>Iterator</code> over each day in a date * range defined by a focus date and range style.</p> * * <p>For instance, passing Thursday, July 4, 2002 and a * <code>RANGE_MONTH_SUNDAY</code> will return an <code>Iterator</code> * that starts with Sunday, June 30, 2002 and ends with Saturday, August 3, * 2002, returning a Calendar instance for each intermediate day.</p> * * <p>This method provides an iterator that returns Calendar objects. * The days are progressed using {@link Calendar#add(int, int)}.</p> * * @param focus the date to work with, not null * @param rangeStyle the style constant to use. Must be one of * {@link DateUtils#RANGE_MONTH_SUNDAY}, * {@link DateUtils#RANGE_MONTH_MONDAY}, * {@link DateUtils#RANGE_WEEK_SUNDAY}, * {@link DateUtils#RANGE_WEEK_MONDAY}, * {@link DateUtils#RANGE_WEEK_RELATIVE}, * {@link DateUtils#RANGE_WEEK_CENTER} * @return the date iterator, not null, not null * @throws IllegalArgumentException if the date is <code>null</code> * @throws IllegalArgumentException if the rangeStyle is invalid */ public static Iterator<Calendar> iterator(final Date focus, final int rangeStyle) { validateDateNotNull(focus); final Calendar gval = Calendar.getInstance(); gval.setTime(focus); return iterator(gval, rangeStyle); }
/** * <p>Constructs an <code>Iterator</code> over each day in a date * range defined by a focus date and range style.</p> * * <p>For instance, passing Thursday, July 4, 2002 and a * <code>RANGE_MONTH_SUNDAY</code> will return an <code>Iterator</code> * that starts with Sunday, June 30, 2002 and ends with Saturday, August 3, * 2002, returning a Calendar instance for each intermediate day.</p> * * <p>This method provides an iterator that returns Calendar objects. * The days are progressed using {@link Calendar#add(int, int)}.</p> * * @param focus the date to work with, not null * @param rangeStyle the style constant to use. Must be one of * {@link DateUtils#RANGE_MONTH_SUNDAY}, * {@link DateUtils#RANGE_MONTH_MONDAY}, * {@link DateUtils#RANGE_WEEK_SUNDAY}, * {@link DateUtils#RANGE_WEEK_MONDAY}, * {@link DateUtils#RANGE_WEEK_RELATIVE}, * {@link DateUtils#RANGE_WEEK_CENTER} * @return the date iterator, not null, not null * @throws IllegalArgumentException if the date is <code>null</code> * @throws IllegalArgumentException if the rangeStyle is invalid */ public static Iterator<Calendar> iterator(final Date focus, final int rangeStyle) { validateDateNotNull(focus); final Calendar gval = Calendar.getInstance(); gval.setTime(focus); return iterator(gval, rangeStyle); }
/** * <p>Constructs an <code>Iterator</code> over each day in a date * range defined by a focus date and range style.</p> * * <p>For instance, passing Thursday, July 4, 2002 and a * <code>RANGE_MONTH_SUNDAY</code> will return an <code>Iterator</code> * that starts with Sunday, June 30, 2002 and ends with Saturday, August 3, * 2002, returning a Calendar instance for each intermediate day.</p> * * <p>This method provides an iterator that returns Calendar objects. * The days are progressed using {@link Calendar#add(int, int)}.</p> * * @param focus the date to work with, not null * @param rangeStyle the style constant to use. Must be one of * {@link DateUtils#RANGE_MONTH_SUNDAY}, * {@link DateUtils#RANGE_MONTH_MONDAY}, * {@link DateUtils#RANGE_WEEK_SUNDAY}, * {@link DateUtils#RANGE_WEEK_MONDAY}, * {@link DateUtils#RANGE_WEEK_RELATIVE}, * {@link DateUtils#RANGE_WEEK_CENTER} * @return the date iterator, not null, not null * @throws IllegalArgumentException if the date is <code>null</code> * @throws IllegalArgumentException if the rangeStyle is invalid */ public static Iterator<Calendar> iterator(final Date focus, final int rangeStyle) { validateDateNotNull(focus); final Calendar gval = Calendar.getInstance(); gval.setTime(focus); return iterator(gval, rangeStyle); }