From 27db1e2890527a9d249dd0a3343460fc89d7dd54 Mon Sep 17 00:00:00 2001 From: Ivan Olexyn Date: Sun, 17 May 2020 16:57:44 +0200 Subject: [PATCH] + turn the lists into maps. --- src/com/olexyn/ensync/Tools.java | 40 ++++++------------- .../ensync/artifacts/SyncDirectory.java | 25 ++++++------ 2 files changed, 25 insertions(+), 40 deletions(-) diff --git a/src/com/olexyn/ensync/Tools.java b/src/com/olexyn/ensync/Tools.java index 2d795b5..2003279 100644 --- a/src/com/olexyn/ensync/Tools.java +++ b/src/com/olexyn/ensync/Tools.java @@ -6,7 +6,7 @@ import java.io.*; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; -import java.util.Iterator; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -19,9 +19,6 @@ public class Tools { } - - - /** * Convert BufferedReader to String. * @@ -54,8 +51,6 @@ public class Tools { } - - public List fileToLines(File file) { String filePath = file.getPath(); List lines = null; @@ -68,28 +63,17 @@ public class Tools { } + public Map mapMinus(Map fromA, Map substractB) { - - - public List mapMinus(Map fromA, - Map substractB) { - - List difference = new ArrayList<>(); - Iterator iterator = fromA.entrySet().iterator(); - while (iterator.hasNext()) { - Map.Entry entry = (Map.Entry) iterator.next(); - String key = (String) entry.getKey(); - //String path - - - + Map difference = new HashMap<>(); + for (Map.Entry entry : fromA.entrySet()) { + String key = entry.getKey(); if (fromA.containsKey(key) && !substractB.containsKey(key)) { SyncFile file = fromA.get(key); - difference.add(file); + difference.put(key, file); } - } return difference; } @@ -110,8 +94,7 @@ public class Tools { * @param path String * @param sb StringBuilder */ - public void writeSbToFile(String path, - StringBuilder sb) { + public void writeSbToFile(String path, StringBuilder sb) { try { BufferedWriter bw = new BufferedWriter(new FileWriter(new File(path))); bw.write(sb.toString()); @@ -128,13 +111,14 @@ public class Tools { * @param path String * @param list StringBuilder */ - public void writeStringListToFile(String path, - List list) { + public void writeStringListToFile(String path, List list) { File file = new File(path); File parent = new File(file.getParent()); - if (!parent.exists()){ + if (!parent.exists()) { - x.execute(new String[]{"mkdir", "-p", parent.getPath()}); + x.execute(new String[]{"mkdir", + "-p", + parent.getPath()}); } diff --git a/src/com/olexyn/ensync/artifacts/SyncDirectory.java b/src/com/olexyn/ensync/artifacts/SyncDirectory.java index 3453466..4d4bf58 100644 --- a/src/com/olexyn/ensync/artifacts/SyncDirectory.java +++ b/src/com/olexyn/ensync/artifacts/SyncDirectory.java @@ -21,9 +21,9 @@ public class SyncDirectory { private SyncMap syncMap; public String path = null; - public List listCreated = new ArrayList<>(); - public List listDeleted = new ArrayList<>(); - public List listModified = new ArrayList<>(); + public Map listCreated = new HashMap<>(); + public Map listDeleted = new HashMap<>(); + public Map listModified = new HashMap<>(); Tools tools = new Tools(); @@ -115,9 +115,7 @@ public class SyncDirectory { Map substractB = readFreshState(); listDeleted = tools.mapMinus(fromA, substractB); - - - + } @@ -125,9 +123,9 @@ public class SyncDirectory { * Compare the OLD and NEW pools. * List is cleared and created each time. */ - public List makeListModified() { + public Map makeListModified() { - listModified = new ArrayList<>(); + listModified.clear(); Map stateFileMap = readStateFile(); for (var freshFileEntry : readFreshState().entrySet()) { @@ -141,7 +139,7 @@ public class SyncDirectory { if (stateFileMap.containsKey(freshFileKey)) { if (freshFile.getTimeModified() > freshFile.getTimeModifiedFromStateFile()) { - listModified.add(freshFile); + listModified.put(freshFileKey, freshFile); } } } @@ -206,7 +204,8 @@ public class SyncDirectory { public void doCreate() { - for (SyncFile createdFile : listCreated) { + for (var entry : listCreated.entrySet()) { + SyncFile createdFile = entry.getValue(); for (var otherEntry : syncMap.syncDirectories.entrySet()) { SyncDirectory otherSD = otherEntry.getValue(); @@ -226,7 +225,8 @@ public class SyncDirectory { */ public void doDelete() { - for (SyncFile deletedFile : listDeleted) { + for (var entry: listDeleted.entrySet()) { + SyncFile deletedFile = entry.getValue(); for (var otherEntry : syncMap.syncDirectories.entrySet()) { SyncDirectory otherSD = otherEntry.getValue(); @@ -255,7 +255,8 @@ public class SyncDirectory { public void doModify() { - for (SyncFile modifiedFile : this.listModified) { + for (var entry : listModified.entrySet()) { + SyncFile modifiedFile = entry.getValue(); for (var otherEntry : syncMap.syncDirectories.entrySet()) { SyncDirectory otherSD = otherEntry.getValue();