001 package org.hackystat.utilities.time.interval; 002 003 import java.util.Iterator; 004 import java.util.NoSuchElementException; 005 006 import org.hackystat.utilities.time.period.Week; 007 008 /** 009 * Provides an iterator over week intervals. 010 * 011 * @author Hongbing Kou 012 */ 013 public class WeekIterator implements Iterator<Week> { 014 /** End week. */ 015 private final Week endWeek; 016 /** Current week. */ 017 private Week currentWeek; 018 019 /** 020 * Creates an iterator. 021 * 022 * @param weekInterval Week interval. 023 */ 024 WeekIterator(WeekInterval weekInterval) { 025 Week startWeek = weekInterval.getStartWeek(); 026 this.endWeek = weekInterval.getEndWeek(); 027 this.currentWeek = startWeek.dec(); 028 } 029 030 /** 031 * Required for iterator(). It will throw UnSupportedMethodException. 032 */ 033 public void remove() { 034 throw new UnsupportedOperationException("remove() is not supported by week iterator."); 035 } 036 037 /** 038 * Whether it is still inside the day interval. 039 * 040 * @return True if it is still in the interval. 041 */ 042 public boolean hasNext() { 043 return this.currentWeek.compareTo(this.endWeek) < 0; 044 } 045 046 /** 047 * Gets the next day. 048 * 049 * @return Next day. 050 */ 051 public Week next() { 052 this.currentWeek = this.currentWeek.inc(); 053 054 if (this.currentWeek.compareTo(this.endWeek) > 0) { 055 throw new NoSuchElementException("Reaches the end of week interval already."); 056 } 057 058 return this.currentWeek; 059 } 060 }