I have a FutureContent class which just holds a static reference to a Future class. I’m not sure why it was done this way, it means every method call now does not have to be static. Is there another reason it was done this way ?
//One possible use of Future class
FutureContent.future.deleteContent("test");
public class FutureContent {
public static Future future = new Future();
}
public class Future{
private final Object lock = new Object();
private String str;
private Hashtable content = new Hashtable();
public void addContent(Object key, Object value){
synchronized(lock){
if(content.containsKey(key)){
content.remove(key);
content.put(key, value);
}
else {
content.put(key, value);
}
}
}
public void clearContent(){
content.clear();
}
public void deleteContent(Object key){
if(content.containsKey(key)){
content.remove(key);
}
}
public boolean getBoolean(Object key){
if(!content.contains(key)){
return false;
}
else {
return ((Boolean)content.get(key)).booleanValue();
}
}
public void addBoolean(Object key , boolean value){
Boolean b = new Boolean(value);
synchronized(lock){
if(content.containsKey(key)){
content.remove(key);
content.put(key, b);
}
else {
content.put(key, b);
}
}
}
public Object getContent(Object key){
return content.get(key);
}
public void setString(String str){
synchronized(lock){
this.str = str;
lock.notifyAll();
}
}
public String getString(){
synchronized(lock){
while(this.str == null)
try {
lock.wait();
} catch (InterruptedException e) {
return null;
}
return this.str;
}
}
private JSONObject value;
public void set(JSONObject t){
synchronized(lock){
value = t;
lock.notifyAll();
}
}
public JSONObject get(){
synchronized(lock){
while(value == null)
try {
lock.wait();
} catch (InterruptedException e) {
return null;
}
return value;
}
}
}
It’s an approach to provide a pre-allocated object which is typically immutable and can be shared across instances. A common example is
Collections.EMPTY_LISTwith the signature