View Javadoc

1   /*
2    * 
3    * 
4    */
5   package zipdiff;
6   
7   import java.util.*;
8   import java.util.zip.ZipEntry;
9   
10  /***
11   * Used to keep track of difference between 2 zip files.
12   * 
13   * @author Sean C. Sullivan
14   */
15  public class Differences {
16      private final Map added = new java.util.HashMap();
17      private final Map removed = new java.util.HashMap();
18      private final Map changed = new java.util.HashMap();
19      private final Map ignored = new java.util.HashMap();
20      private String filename1;
21      private String filename2;
22  
23      public Differences() {
24          // todo 
25      }
26  
27      public void setFilename1(String filename) {
28          filename1 = filename;
29      }
30  
31      public void setFilename2(String filename) {
32          filename2 = filename;
33      }
34  
35      public String getFilename1() {
36          return filename1;
37      }
38  
39      public String getFilename2() {
40          return filename2;
41      }
42  
43      public void fileAdded(String fqn, ZipEntry ze) {
44          added.put(fqn, ze);
45      }
46  
47      public void fileRemoved(String fqn, ZipEntry ze) {
48          removed.put(fqn, ze);
49      }
50  
51      public void fileIgnored(String fqn, ZipEntry ze) {
52          ignored.put(fqn, ze);
53      }
54  
55      public void fileChanged(String fqn, ZipEntry z1, ZipEntry z2) {
56          ZipEntry[] entries = new ZipEntry[2];
57          entries[0] = z1;
58          entries[1] = z2;
59          changed.put(fqn, entries);
60      }
61  
62      public Map getAdded() {
63          return added;
64      }
65  
66      public Map getRemoved() {
67          return removed;
68      }
69  
70      public Map getChanged() {
71          return changed;
72      }
73  
74      public Map getIgnored() {
75          return ignored;
76      }
77  
78      public boolean hasDifferences() {
79          return ((getChanged().size() > 0) || (getAdded().size() > 0) || (getRemoved().size() > 0));
80      }
81  
82      public String toString() {
83          StringBuffer sb = new StringBuffer();
84  
85          if (added.size() == 1) {
86              sb.append("1 file was");
87          } else {
88              sb.append(added.size() + " files were");
89          }
90          sb.append(" added to " + this.getFilename2() + "\n");
91  
92          Iterator iter = added.keySet().iterator();
93          while (iter.hasNext()) {
94              String name = (String) iter.next();
95              sb.append("\t[added] " + name + "\n");
96          }
97  
98          if (removed.size() == 1) {
99              sb.append("1 file was");
100         } else {
101             sb.append(removed.size() + " files were");
102         }
103         sb.append(" removed from " + this.getFilename2() + "\n");
104 
105         iter = removed.keySet().iterator();
106         while (iter.hasNext()) {
107             String name = (String) iter.next();
108             sb.append("\t[removed] " + name + "\n");
109         }
110 
111         if (changed.size() == 1) {
112             sb.append("1 file changed\n");
113         } else {
114             sb.append(changed.size() + " files changed\n");
115         }
116 
117         Set keys = getChanged().keySet();
118         iter = keys.iterator();
119         while (iter.hasNext()) {
120             String name = (String) iter.next();
121             ZipEntry[] entries = (ZipEntry[]) getChanged().get(name);
122             sb.append("\t[changed] " + name + " ");
123             sb.append(" ( size " + entries[0].getSize());
124             sb.append(" : " + entries[1].getSize());
125             sb.append(" )\n");
126         }
127         int differenceCount = added.size() + changed.size() + removed.size();
128 
129         sb.append("Total differences: " + differenceCount);
130         return sb.toString();
131     }
132 
133 }