56 lines
1.5 KiB
Java
56 lines
1.5 KiB
Java
package cz.kamma.term.service;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.reflect.TypeToken;
|
|
import cz.kamma.term.model.ConnectionInfo;
|
|
|
|
import java.io.*;
|
|
import java.lang.reflect.Type;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class ConnectionManager {
|
|
private static final String FILE_NAME = "connections.json";
|
|
private final Gson gson = new Gson();
|
|
private List<ConnectionInfo> connections = new ArrayList<>();
|
|
|
|
public ConnectionManager() {
|
|
load();
|
|
}
|
|
|
|
public List<ConnectionInfo> getConnections() {
|
|
return connections;
|
|
}
|
|
|
|
public void addConnection(ConnectionInfo connection) {
|
|
connections.add(connection);
|
|
save();
|
|
}
|
|
|
|
public void removeConnection(ConnectionInfo connection) {
|
|
connections.remove(connection);
|
|
save();
|
|
}
|
|
|
|
public void save() {
|
|
try (Writer writer = new FileWriter(FILE_NAME)) {
|
|
gson.toJson(connections, writer);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
private void load() {
|
|
File file = new File(FILE_NAME);
|
|
if (file.exists()) {
|
|
try (Reader reader = new FileReader(file)) {
|
|
Type listType = new TypeToken<ArrayList<ConnectionInfo>>(){}.getType();
|
|
connections = gson.fromJson(reader, listType);
|
|
if (connections == null) connections = new ArrayList<>();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|