public class Storage implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public static List<Message> MessageList = Collections.synchronizedList(new ArrayList<Message>()); //Fail safe if multiple threads modify them.
public static List<Group> GroupList = Collections.synchronizedList(new ArrayList<Group>());
protected Storage() {
super();
}
static private Storage _instance = null;
//initialized: Storage.instance();
static public Storage instance() {
if(_instance == null) {
_instance = new Storage();
}
return _instance;
}
}
I have the upper class which creates a single object. I want to save this object with its Lists to a file. Then when my app starts and I instantiate Storage I want it to read the file and if it is empty create a new Storage, but if its not then read the previous instance of Storage and create this new based on the old. Basically meaning that I want the contents of GroupList and MessageList to be persistent.
EDIT: because I didn’t make it clear enough.
Where do I place the code needed to check and read a previous instance of this class in? I guess in the constructor, but then will my Lists also get the values of the other object? I dont know where/how to code that.
EDIT2: pasting solution.
public class Storage implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public static List<Message> MessageList = Collections.synchronizedList(new ArrayList<Message>()); //Fail safe if multiple threads modify them.
public static List<Group> GroupList = Collections.synchronizedList(new ArrayList<Group>());
protected Storage() {
super();
}
static private Storage _instance = null;
//initialized: Storage.instance();
public static synchronized Storage instance(){
initialize();
if(_instance == null) {
_instance = new Storage();
}
return _instance;
}
public static synchronized void persist(){
FileOutputStream fos = null;
ObjectOutputStream out = null;
try{
fos = new FileOutputStream("Storage.txt");
out = new ObjectOutputStream(fos);
out.writeObject(instance());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
protected static synchronized void initialize(){
FileInputStream fis = null;
ObjectInputStream in = null;
try{
fis = new FileInputStream("Storage.txt");
in = new ObjectInputStream(fis);
_instance = (Storage)in.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (OptionalDataException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static synchronized void addElement(Message message){
if(!MessageList.contains(message)){
MessageList.add(message);
persist();
Log.i("STORAGE-addElement", "Added: " + message);
}
}
public static synchronized void addElement(Group group){
if(!GroupList.contains(group)){
GroupList.add(group);
persist();
Log.i("STORAGE-addElement", "Added: " + group);
}
}
public static synchronized void removeElement(Message message){
if(!MessageList.contains(message)){
MessageList.remove(message);
persist();
Log.i("STORAGE-removeElement", "Removed: " + message);
}
}
public static synchronized void removeElement(Group group){
if(!GroupList.contains(group)){
GroupList.remove(group);
persist();
Log.i("STORAGE-removeElement", "Removed: " + group);
}
}
public static synchronized void wipeAll(){
MessageList.clear();
GroupList.clear();
persist();
Log.i("STORAGE-wipeAll", "Wiped all data");
}
}
Thanks for your help! 🙂
You can add the following methods to your Storage object:
You can call
initialize()from your staticinstancemethod instead of calling the constructor directly.