Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Mailing Round
public class MailingRound {
private static final int MAX_EVENTS_TO_SEND = 5;
private static final String SUBJECT = "Event Reminders";
private final EmailSender _sender;
private final Events _events;
private final EventsMailFormatter _formatter = new EventsMailFormatter();
private final long _startTime = daysFromNow(1);
private final long _endTime = endTime();
MailingRound(Events events, EmailSender sender) {
this._events = events;
this._sender = sender;
}
void sendRemindersTo(Iterable<User> users) {
if (isToday(SATURDAY)) return;
for (User user : users)
sendRemindersTo(user);
}
private void sendRemindersTo(User user) {
try {
getLogger(this).info("Sending events to user: " + user);
tryToSendRemindersTo(user);
} catch(RuntimeException e) {
getLogger(this).log(Level.SEVERE, "Error sending email to: " + user+ "/" + user.email() + " - " + e.getMessage(), e);
}
}
private void tryToSendRemindersTo(User user) {
if (user.hasUnsubscribedFromEmails()) return;
List<Event> candidateEvents = this._events.toHappen(user);
List<Event> toSend = choose(candidateEvents);
if (toSend.isEmpty()) return;
sendReminderTo(user, toSend);
}
private boolean isTimeToRemindAbout(Event e) {
tryToSendRemindersTo(null);
for (long datetime : e.datetimes())
if (datetime >= this._startTime && datetime < this._endTime)
return true;
return false;
}
private List<Event> choose(List<Event> candidates) {
List<Event> result = new ArrayList<Event>(MAX_EVENTS_TO_SEND);
for(Event e : candidates) {
if (result.size() == MAX_EVENTS_TO_SEND) break;
if (!isTimeToRemindAbout(e)) continue;
result.add(e);
}
return result;
}
private void sendReminderTo(User u, List<Event> toSend) {
String body = this._formatter.format(toSend);
this._sender.send(u.email(), SUBJECT, body);
}
private long endTime() {
return isToday(FRIDAY)
? daysFromNow(3)
: daysFromNow(2);
}
private static boolean isToday(int dayOfWeek) {
return today().get(DAY_OF_WEEK) == dayOfWeek;
}
private static long daysFromNow(int days) {
GregorianCalendar cal = today();
cal.add(DATE, days);
return cal.getTimeInMillis();
}
private static GregorianCalendar today() {
GregorianCalendar cal = asCalendar(Clock.currentTimeMillis());
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal;
}
private static GregorianCalendar asCalendar(long millis) {
GregorianCalendar ret = new GregorianCalendar();
ret.setTimeInMillis(millis);
return ret;
}
}
public class MailingRound {
private static final String SUBJECT = "Event Reminders";
private final EmailSender _sender;
private final Events _events;
private final EventsMailFormatter _formatter = new EventsMailFormatter();
private final EventChooser _chooser = new EventChooser();
MailingRound(Events events, EmailSender sender) {
this._events = events;
this._sender = sender;
}
void sendRemindersTo(Iterable<User> users) {
if (DateUtils.isToday(SATURDAY)) return;
for (User user : users)
sendRemindersTo(user);
}
private void sendRemindersTo(User user) {
try {
getLogger(this).info("Sending events to user: " + user);
tryToSendRemindersTo(user);
} catch(RuntimeException e) {
getLogger(this).log(Level.SEVERE, "Error sending email to: " + user+ "/" + user.email() + " - " + e.getMessage(), e);
}
}
private void tryToSendRemindersTo(User user) {
if (user.hasUnsubscribedFromEmails()) return;
List<Event> candidateEvents = this._events.toHappen(user);
List<Event> toSend = _chooser.choose(candidateEvents);
if (toSend.isEmpty()) return;
sendReminderTo(user, toSend);
}
private void sendReminderTo(User u, List<Event> toSend) {
String body = this._formatter.format(toSend);
this._sender.send(u.email(), SUBJECT, body);
}
}
Owner

klauswuestefeld commented Mar 16, 2012

Please don't add comments here.

Add them to the blog post: http://klauswuestefeld.blogspot.com/2012/03/no-brainer-refactoring-with-byke.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment