Skip to content

Instantly share code, notes, and snippets.

@usbportnoy
Last active September 28, 2016 17:39
Show Gist options
  • Save usbportnoy/ac7ff3f4dca20083c2301facce3f4fa2 to your computer and use it in GitHub Desktop.
Save usbportnoy/ac7ff3f4dca20083c2301facce3f4fa2 to your computer and use it in GitHub Desktop.
Calendar rightNow = Calendar.getInstance();
public class OnlineStatus {
public enum States {Unknown, Online}
private int onlineThresholdHrs;
public void setOnlineThresholdHrs(int hours) {
this.onlineThresholdHrs = hours;
}
public States getStatus(Date lastOnline, Calendar calendar) {
int currentHour = calendar.get(Calendar.HOUR_OF_DAY);
calendar.set(
Calendar.HOUR_OF_DAY,
currentHour + onlineThresholdHrs);
Date onlineThreshold = calendar.getTime();
if (lastOnline.before(onlineThreshold)) {
return States.Online;
} else {
return States.Unknown;
}
}
}
@Test
public void onlineTest() throws Exception {
Profile profile = new Profile();
//sets instance to right now
Calendar calendar = Calendar.getInstance();
//get time of right now and set users online status with it
profile.setLastOnline(calendar.getTime());
//create and configure
OnlineStatus onlineStatus = new OnlineStatus();
onlineStatus.setOnlineThresholdHrs(1);
OnlineStatus.States status = onlineStatus.getStatus(
profile.getLastOnline(),
calendar
);
assertEquals(status, OnlineStatus.States.Online);
}
public class Profile {
Date lastOnline;
public Date getLastOnline() {
return lastOnline;
}
public void setLastOnline(Date lastOnline) {
this.lastOnline = lastOnline;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment