2023-10-24 02:19:14 +04:00
|
|
|
|
package laba1Loco;
|
|
|
|
|
|
2023-11-19 21:36:49 +04:00
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.BufferedWriter;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileReader;
|
|
|
|
|
import java.io.FileWriter;
|
|
|
|
|
import java.io.IOException;
|
2023-11-20 01:46:03 +04:00
|
|
|
|
import java.util.ArrayList;
|
2023-11-19 21:36:49 +04:00
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import java.util.Collections;
|
2023-10-24 02:19:14 +04:00
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.List;
|
2023-11-19 21:36:49 +04:00
|
|
|
|
import java.util.Map;
|
2023-10-24 02:19:14 +04:00
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
public class TrainsGenericStorage {
|
2023-11-19 21:36:49 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записи ключа и значения элемента словаря
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static String _separatorForKeyValueWR = "|";
|
|
|
|
|
private static String _separatorForKeyValue = "\\|";
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записей коллекции данных в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
private String _separatorRecordsWR = ";";
|
|
|
|
|
private String _separatorRecords = "\\;";
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Разделитель для записи информации по объекту в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
private static String _separatorForObjectWR = ":";
|
|
|
|
|
private static String _separatorForObject = "\\:";
|
|
|
|
|
|
2023-11-20 01:46:03 +04:00
|
|
|
|
public boolean SaveDataSingle(String filename, String key){
|
|
|
|
|
if (new File(filename).exists()) {
|
|
|
|
|
new File(filename).delete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringBuilder data = new StringBuilder();
|
|
|
|
|
data.append(key).append("\n");
|
|
|
|
|
for (DrawingTrain elem : _trainStorages.get(key).getTrains(100))
|
|
|
|
|
data.append(elem != null ? ExtentionDrawingTrain.GetDataForSave(elem, _separatorForObjectWR) + "\n" : "");
|
|
|
|
|
|
|
|
|
|
if (data.length() == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
|
|
|
|
|
writer.write("TrainStorageSingle" + System.lineSeparator() + data.toString());
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean LoadDataSingle(String filename){
|
|
|
|
|
if (!new File(filename).exists()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
|
|
|
|
|
String s = reader.readLine();
|
|
|
|
|
if (s == null || s.length() == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (!s.startsWith("TrainStorageSingle"))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
String key = reader.readLine();
|
|
|
|
|
if (key == null || key.length() == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
TrainsGenericCollection<DrawingTrain, DrawningObjectTrain> collection = new TrainsGenericCollection<>(_pictureWidth, _pictureHeight);
|
|
|
|
|
List<String> trainsStrings = new ArrayList<String>();
|
|
|
|
|
|
|
|
|
|
s = reader.readLine();
|
|
|
|
|
while (s != null && s.length() != 0){
|
|
|
|
|
trainsStrings.add(s);
|
|
|
|
|
s = reader.readLine();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Collections.reverse(trainsStrings);
|
|
|
|
|
for (String elem : trainsStrings) {
|
|
|
|
|
DrawingTrain train = ExtentionDrawingTrain.CreateDrawingTrain(elem, _separatorForObject, _pictureWidth, _pictureHeight);
|
|
|
|
|
if (train == null || collection.Add(train) == -1)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_trainStorages.containsKey(key))
|
|
|
|
|
_trainStorages.remove(key);
|
|
|
|
|
_trainStorages.put(key, collection);
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-19 21:36:49 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сохранение информации по автомобилям в хранилище в файл
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">Путь и имя файла</param>
|
|
|
|
|
/// <returns>true - сохранение прошло успешно, false - ошибка при cохранении данных</returns>
|
|
|
|
|
public boolean SaveData(String filename)
|
|
|
|
|
{
|
|
|
|
|
if (new File(filename).exists()) {
|
|
|
|
|
new File(filename).delete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
StringBuilder data = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
for (Map.Entry<String, TrainsGenericCollection<DrawingTrain, DrawningObjectTrain>> record : _trainStorages.entrySet()) {
|
|
|
|
|
StringBuilder records = new StringBuilder();
|
|
|
|
|
for (DrawingTrain elem : record.getValue().getTrains(100)) {
|
|
|
|
|
records.append(elem != null ? ExtentionDrawingTrain.GetDataForSave(elem, _separatorForObjectWR) + _separatorRecordsWR : "");
|
|
|
|
|
}
|
|
|
|
|
data.append(record.getKey()).append(_separatorForKeyValueWR).append(records).append("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.length() == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
|
|
|
|
|
writer.write("TrainStorage" + System.lineSeparator() + data.toString());
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Загрузка информации по автомобилям в хранилище из файла
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="filename">Путь и имя файла</param>
|
|
|
|
|
/// <returns>true - загрузка прошла успешно, false - ошибка призагрузке данных</returns>
|
2023-11-20 01:46:03 +04:00
|
|
|
|
public boolean LoadData(String filename) {
|
2023-11-19 21:36:49 +04:00
|
|
|
|
if (!new File(filename).exists()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
|
|
|
|
|
String s = reader.readLine();
|
|
|
|
|
if (s == null || s.length() == 0)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
if (!s.startsWith("TrainStorage"))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
_trainStorages.clear();
|
|
|
|
|
s = reader.readLine();
|
|
|
|
|
while (s != null && s.length() != 0) {
|
|
|
|
|
String[] record = s.split(_separatorForKeyValue);
|
|
|
|
|
s = reader.readLine();
|
|
|
|
|
if (record.length != 2) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
TrainsGenericCollection<DrawingTrain, DrawningObjectTrain> collection = new TrainsGenericCollection<>(_pictureWidth, _pictureHeight);
|
|
|
|
|
String[] set = record[1].split(_separatorRecords);
|
|
|
|
|
List<String> reversedSet = Arrays.asList(set);
|
|
|
|
|
Collections.reverse(reversedSet);
|
|
|
|
|
for (String elem : reversedSet) {
|
|
|
|
|
DrawingTrain train = ExtentionDrawingTrain.CreateDrawingTrain(elem, _separatorForObject, _pictureWidth, _pictureHeight);
|
|
|
|
|
if (train == null || collection.Add(train) == -1)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
_trainStorages.put(record[0], collection);
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-10-24 02:19:14 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Словарь (хранилище)
|
|
|
|
|
/// </summary>
|
2023-11-05 01:47:52 +04:00
|
|
|
|
HashMap<String, TrainsGenericCollection<DrawingTrain, DrawningObjectTrain>> _trainStorages;
|
2023-10-24 02:19:14 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Возвращение списка названий наборов
|
|
|
|
|
/// </summary>
|
2023-11-05 01:47:52 +04:00
|
|
|
|
public List<String> Keys(){return _trainStorages.keySet().stream().collect(Collectors.toList());}
|
2023-10-24 02:19:14 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ширина окна отрисовки
|
|
|
|
|
/// </summary>
|
|
|
|
|
private int _pictureWidth;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Высота окна отрисовки
|
|
|
|
|
/// </summary>
|
|
|
|
|
private int _pictureHeight;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pictureWidth"></param>
|
|
|
|
|
/// <param name="pictureHeight"></param>
|
|
|
|
|
public TrainsGenericStorage(int pictureWidth, int pictureHeight)
|
|
|
|
|
{
|
2023-11-05 01:47:52 +04:00
|
|
|
|
_trainStorages = new HashMap<String, TrainsGenericCollection<DrawingTrain, DrawningObjectTrain>>();
|
2023-10-24 02:19:14 +04:00
|
|
|
|
_pictureWidth = pictureWidth;
|
|
|
|
|
_pictureHeight = pictureHeight;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Добавление набора
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название набора</param>
|
|
|
|
|
public void AddSet(String name)
|
|
|
|
|
{
|
2023-11-05 01:47:52 +04:00
|
|
|
|
if (_trainStorages.containsKey(name))
|
2023-10-24 02:19:14 +04:00
|
|
|
|
return;
|
2023-11-05 01:47:52 +04:00
|
|
|
|
_trainStorages.put(name, new TrainsGenericCollection<DrawingTrain, DrawningObjectTrain>(_pictureWidth, _pictureHeight));
|
2023-10-24 02:19:14 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление набора
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="name">Название набора</param>
|
|
|
|
|
public void DelSet(String name)
|
|
|
|
|
{
|
2023-11-05 01:47:52 +04:00
|
|
|
|
if (!_trainStorages.containsKey(name))
|
2023-10-24 02:19:14 +04:00
|
|
|
|
return;
|
2023-11-05 01:47:52 +04:00
|
|
|
|
_trainStorages.remove(name);
|
2023-10-24 02:19:14 +04:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Доступ к набору
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ind"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public TrainsGenericCollection<DrawingTrain, DrawningObjectTrain> get(String ind)
|
|
|
|
|
{
|
2023-11-05 01:47:52 +04:00
|
|
|
|
if (_trainStorages.containsKey(ind))
|
|
|
|
|
return _trainStorages.get(ind);
|
2023-10-24 02:19:14 +04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-11-05 01:47:52 +04:00
|
|
|
|
|
|
|
|
|
public DrawningObjectTrain get(String ind1, int ind2){
|
|
|
|
|
if (!_trainStorages.containsKey(ind1))
|
|
|
|
|
return null;
|
|
|
|
|
return _trainStorages.get(ind1).GetU(ind2);
|
|
|
|
|
}
|
2023-10-24 02:19:14 +04:00
|
|
|
|
}
|