Skip to content

Instantly share code, notes, and snippets.

@jakub-bochenski
Created April 4, 2014 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakub-bochenski/9971998 to your computer and use it in GitHub Desktop.
Save jakub-bochenski/9971998 to your computer and use it in GitHub Desktop.
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