Last active
September 28, 2016 17:39
-
-
Save usbportnoy/ac7ff3f4dca20083c2301facce3f4fa2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Calendar rightNow = Calendar.getInstance(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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