juzu.impl.fs.Snapshot Maven / Gradle / Ivy
/*
* Copyright 2013 eXo Platform SAS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package juzu.impl.fs;
import juzu.impl.common.Tools;
import juzu.impl.fs.spi.ReadFileSystem;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
/** @author Julien Viet */
public class Snapshot extends HashMap {
/** . */
private FileSystemScanner scanner;
/** Changes since previous snapshot. */
Map changes;
public Snapshot(FileSystemScanner scanner) {
this.scanner = scanner;
this.changes = new LinkedHashMap();
}
public boolean hasChanges() {
return changes.size() > 0;
}
public Map getChanges() {
return changes;
}
public Snapshot scan() throws IOException {
return scan(this);
}
private static
Snapshot
scan(final Snapshot
current) throws IOException {
//
final FileSystemScanner
scanner = current.scanner;
final ReadFileSystem
fs = scanner.fs;
final ArrayList stack = scanner.stack;
// Create a new snapshot
final Snapshot next = new Snapshot
(scanner);
// Traverse map
fs.traverse(scanner, new Visitor
() {
public void enterDir(P dir, String name) throws IOException {
stack.add(name);
}
public void file(P file, String name) throws IOException {
long stamp = scanner.stampOf(file);
stack.add(name);
String id = Tools.join('/', stack);
stack.remove(stack.size() - 1);
next.put(id, stamp);
//
Long data = current.get(id);
if (data == null) {
next.changes.put(id, Change.ADD);
} else {
if (scanner.isModified(data, stamp)) {
next.changes.put(id, Change.UPDATE);
}
}
}
public void leaveDir(P dir, String name) throws IOException {
stack.remove(scanner.stack.size() - 1);
}
});
// Now find all removed entries
for (String id : current.keySet()) {
if (!next.containsKey(id)) {
next.changes.put(id, Change.REMOVE);
}
}
//
return next;
}
}