Created
April 4, 2014 10:34
-
-
Save jakub-bochenski/9971998 to your computer and use it in GitHub Desktop.
This file contains 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
import java.io.IOException; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.text.DateFormat; | |
import java.util.Date; | |
import java.util.Scanner; | |
public class Container { | |
public static class ApplicationSettings { | |
// we'll not show this one | |
public String getStatusEndpointAddress() { | |
// TODO Auto-generated method stub | |
return null; | |
} | |
public static ApplicationSettings getInstance() { | |
// TODO Auto-generated method stub | |
return null; | |
} | |
} | |
public static class StopwatchExpert { | |
private static final long TRESHOLD = 1000; | |
public boolean didEnoughTimeExpire(Date date) { | |
return date.getTime() - System.currentTimeMillis() > TRESHOLD; | |
} | |
} | |
public static class Sample { | |
private final URL address; | |
public Sample() throws MalformedURLException { | |
ApplicationSettings settings = ApplicationSettings.getInstance(); | |
this.address = new URL(settings.getStatusEndpointAddress()); | |
}; | |
public Boolean checkUpdates() { | |
URLConnection openConnection; | |
try { | |
openConnection = address.openConnection(); | |
} catch (IOException e1) { | |
e1.printStackTrace(); | |
throw new RuntimeException(); | |
} | |
try { | |
String string = new Scanner(openConnection.getInputStream()).useDelimiter("\\Z").next(); | |
Date parsed = DateFormat.getDateTimeInstance().parse(string); | |
return new StopwatchExpert().didEnoughTimeExpire(parsed); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
throw new RuntimeException(); | |
} finally { | |
try { | |
openConnection.getInputStream().close(); | |
} catch (IOException e) { | |
// this will never happen | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment