Техническая возможность сравнения директорий, а значит и поиска файлов в других клиентах

This commit is contained in:
Gravit 2018-10-18 19:22:29 +07:00
parent 615ed25e00
commit e5d3dd9a03
No known key found for this signature in database
GPG key ID: 061981E1E85D3216

View file

@ -143,6 +143,12 @@ public Diff diff(HashedDir other, FileNameMatcher matcher) {
HashedDir extra = other.sideDiff(this, matcher, new LinkedList<>(), false); HashedDir extra = other.sideDiff(this, matcher, new LinkedList<>(), false);
return new Diff(mismatch, extra); return new Diff(mismatch, extra);
} }
@LauncherAPI
public Diff compare(HashedDir other, FileNameMatcher matcher) {
HashedDir mismatch = sideDiff(other, matcher, new LinkedList<>(), true);
HashedDir extra = other.sideDiff(this, matcher, new LinkedList<>(), false);
return new Diff(mismatch, extra);
}
public void remove(String name) public void remove(String name)
{ {
map.remove(name); map.remove(name);
@ -261,6 +267,58 @@ private HashedDir sideDiff(HashedDir other, FileNameMatcher matcher, Deque<Strin
return diff; return diff;
} }
private HashedDir sideCompare(HashedDir other, FileNameMatcher matcher, Deque<String> path, boolean mismatchList) {
HashedDir diff = new HashedDir();
for (Entry<String, HashedEntry> mapEntry : map.entrySet()) {
String name = mapEntry.getKey();
HashedEntry entry = mapEntry.getValue();
path.add(name);
// Should update?
boolean shouldUpdate = matcher == null || matcher.shouldUpdate(path);
// Not found or of different type
Type type = entry.getType();
HashedEntry otherEntry = other.map.get(name);
if (otherEntry == null || otherEntry.getType() != type) {
if (shouldUpdate || mismatchList && otherEntry == null) {
diff.map.put(name, entry);
// Should be deleted!
if (!mismatchList)
entry.flag = true;
}
path.removeLast();
continue;
}
// Compare entries based on type
switch (type) {
case FILE:
HashedFile file = (HashedFile) entry;
HashedFile otherFile = (HashedFile) otherEntry;
if (mismatchList && shouldUpdate && file.isSame(otherFile))
diff.map.put(name, entry);
break;
case DIR:
HashedDir dir = (HashedDir) entry;
HashedDir otherDir = (HashedDir) otherEntry;
if (mismatchList || shouldUpdate) { // Maybe isn't need to go deeper?
HashedDir mismatch = dir.sideCompare(otherDir, matcher, path, mismatchList);
if (!mismatch.isEmpty())
diff.map.put(name, mismatch);
}
break;
default:
throw new AssertionError("Unsupported hashed entry type: " + type.name());
}
// Remove this path entry
path.removeLast();
}
return diff;
}
@Override @Override
public long size() { public long size() {
return map.values().stream().mapToLong(HashedEntry::size).sum(); return map.values().stream().mapToLong(HashedEntry::size).sum();