mirror of
https://github.com/klmp200/PQT_Gestionnaire_vente_stock.git
synced 2024-12-22 15:31:08 +00:00
Module Server, packg tools : création packg io, création clss ISerialFileManager, SimpleSerialFileManager et SimpleSerialFileManagerFactory
This commit is contained in:
parent
b2f1acfdc7
commit
dce4d4a4d2
@ -0,0 +1,12 @@
|
||||
package com.pqt.server.tools.io;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
//TODO écrire javadoc
|
||||
public interface ISerialFileManager<T> {
|
||||
List<T> loadListFromFile();
|
||||
Set<T> loadSetFromFile();
|
||||
void saveListToFile(List<T> list);
|
||||
void saveSetToFile(Set<T> set);
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
package com.pqt.server.tools.io;
|
||||
|
||||
import com.pqt.server.tools.FileUtil;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.*;
|
||||
|
||||
//TODO vérifier que le save écrase bien le contenu précédent du fichier
|
||||
public class SimpleSerialFileManager<T> implements ISerialFileManager<T> {
|
||||
|
||||
private Path filePath;
|
||||
private Class<T> clazz;
|
||||
|
||||
SimpleSerialFileManager(String filePath, Class<T> clazz){
|
||||
this(Paths.get(filePath), clazz);
|
||||
}
|
||||
|
||||
SimpleSerialFileManager(Path filePath, Class<T> clazz){
|
||||
this.filePath = filePath;
|
||||
this.clazz = clazz;
|
||||
try{
|
||||
FileUtil.createFileIfNotExist(filePath);
|
||||
}catch (IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<T> loadListFromFile() {
|
||||
try{
|
||||
if(!FileUtil.createFileIfNotExist(filePath)){
|
||||
List<T> loadedEntries = new ArrayList<>();
|
||||
fillCollection(loadedEntries);
|
||||
return loadedEntries;
|
||||
}
|
||||
}catch(IOException | ClassNotFoundException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<T> loadSetFromFile() {
|
||||
try{
|
||||
if(!FileUtil.createFileIfNotExist(filePath)){
|
||||
Set<T> loadedEntries = new HashSet<>();
|
||||
fillCollection(loadedEntries);
|
||||
return loadedEntries;
|
||||
}
|
||||
}catch(IOException | ClassNotFoundException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void fillCollection(Collection<T> collection) throws IOException, ClassNotFoundException {
|
||||
if(collection==null) return;
|
||||
try(FileInputStream fis = new FileInputStream(filePath.toString());
|
||||
ObjectInputStream ois = new ObjectInputStream(fis)){
|
||||
boolean end = false;
|
||||
do{
|
||||
try{
|
||||
Object obj = ois.readObject();
|
||||
if(clazz.isInstance(obj)){
|
||||
T ae = clazz.cast(obj);
|
||||
collection.add(ae);
|
||||
}
|
||||
}catch (EOFException e){
|
||||
end = true;
|
||||
}
|
||||
}while(!end);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveListToFile(List<T> list) {
|
||||
save(list);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveSetToFile(Set<T> set) {
|
||||
save(set);
|
||||
}
|
||||
|
||||
private void save(Collection<T> collection){
|
||||
try{
|
||||
FileUtil.createFileIfNotExist(filePath);
|
||||
}catch (IOException e){
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
try(FileOutputStream fos = new FileOutputStream(filePath.toString());
|
||||
ObjectOutputStream oos = new ObjectOutputStream(fos)){
|
||||
|
||||
collection.forEach(p -> {
|
||||
try {
|
||||
oos.writeObject(p);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
});
|
||||
}catch(IOException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.pqt.server.tools.io;
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class SimpleSerialFileManagerFactory {
|
||||
protected SimpleSerialFileManagerFactory(){}
|
||||
|
||||
public static <T> ISerialFileManager<T> getFileManager(Class<T> clazz, String filePath){
|
||||
return new SimpleSerialFileManager<>(filePath, clazz);
|
||||
}
|
||||
|
||||
public static <T> ISerialFileManager<T> getFileManager(Class<T> clazz, Path filePath){
|
||||
return new SimpleSerialFileManager<>(filePath, clazz);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user