View Javadoc
1   package org.argeo.sync;
2   
3   import java.time.Instant;
4   import java.util.Set;
5   import java.util.TreeSet;
6   
7   /** Describes what happendend during a sync operation. */
8   public class SyncResult<T> {
9   	private final Set<T> added = new TreeSet<>();
10  	private final Set<T> modified = new TreeSet<>();
11  	private final Set<T> deleted = new TreeSet<>();
12  	private final Set<Error> errors = new TreeSet<>();
13  
14  	public Set<T> getAdded() {
15  		return added;
16  	}
17  
18  	public Set<T> getModified() {
19  		return modified;
20  	}
21  
22  	public Set<T> getDeleted() {
23  		return deleted;
24  	}
25  
26  	public Set<Error> getErrors() {
27  		return errors;
28  	}
29  
30  	public void addError(T sourcePath, T targetPath, Exception e) {
31  		Error error = new Error(sourcePath, targetPath, e);
32  		errors.add(error);
33  	}
34  
35  	public boolean noModification() {
36  		return modified.isEmpty() && deleted.isEmpty() && added.isEmpty();
37  	}
38  
39  	@Override
40  	public String toString() {
41  		if (noModification())
42  			return "No modification.";
43  		StringBuffer sb = new StringBuffer();
44  		for (T p : modified)
45  			sb.append("MOD ").append(p).append('\n');
46  		for (T p : deleted)
47  			sb.append("DEL ").append(p).append('\n');
48  		for (T p : added)
49  			sb.append("ADD ").append(p).append('\n');
50  		for (Error error : errors)
51  			sb.append(error).append('\n');
52  		return sb.toString();
53  	}
54  
55  	public class Error implements Comparable<Error> {
56  		private final T sourcePath;// if null this is a failed delete
57  		private final T targetPath;
58  		private final Exception exception;
59  		private final Instant timestamp = Instant.now();
60  
61  		public Error(T sourcePath, T targetPath, Exception e) {
62  			super();
63  			this.sourcePath = sourcePath;
64  			this.targetPath = targetPath;
65  			this.exception = e;
66  		}
67  
68  		public T getSourcePath() {
69  			return sourcePath;
70  		}
71  
72  		public T getTargetPath() {
73  			return targetPath;
74  		}
75  
76  		public Exception getException() {
77  			return exception;
78  		}
79  
80  		public Instant getTimestamp() {
81  			return timestamp;
82  		}
83  
84  		@Override
85  		public int compareTo(Error o) {
86  			return timestamp.compareTo(o.timestamp);
87  		}
88  
89  		@Override
90  		public int hashCode() {
91  			return timestamp.hashCode();
92  		}
93  
94  		@Override
95  		public String toString() {
96  			return "ERR " + timestamp + (sourcePath == null ? "Deletion failed" : "Copy failed " + sourcePath) + " "
97  					+ targetPath + " " + exception.getMessage();
98  		}
99  
100 	}
101 }