Skip to content

Instantly share code, notes, and snippets.

@komiya-atsushi
Created December 22, 2011 16:13
Show Gist options
  • Save komiya-atsushi/1510838 to your computer and use it in GitHub Desktop.
Save komiya-atsushi/1510838 to your computer and use it in GitHub Desktop.
2011.12.22 に書いたひどいコード
package hoge;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Fuga {
public static Iterable<Date> iterableDays(Date from, Date to) {
final Date localFrom = from;
final Date localTo = to;
return new Iterable<Date>() {
@Override
public Iterator<Date> iterator() {
return new Iterator<Date>() {
private Date from;
private Date to;
private Date current;
private Date nextObject;
@Override
public boolean hasNext() {
if (nextObject != null) {
return true;
}
if (this.current == null) {
this.from = localFrom;
this.to = localTo;
current = localFrom;
} else {
current = nextDay(this.current);
}
if (current.after(to)) {
return false;
}
nextObject = current;
return true;
}
@Override
public Date next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
Date result = nextObject;
nextObject = null;
return result;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
public static Date nextDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, 1);
return cal.getTime();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment