2222import java .io .FileWriter ;
2323import java .io .IOException ;
2424import java .io .ObjectInputStream ;
25+ import java .util .Map ;
26+ import java .util .LinkedHashMap ;
2527import java .util .ArrayList ;
26- import java .util .Arrays ;
2728import java .util .HashMap ;
2829import java .util .Iterator ;
29- import java .util .LinkedHashMap ;
3030import java .util .List ;
31+ import java .util .Arrays ;
32+
3133
3234import com .thoughtworks .xstream .XStream ;
3335import com .thoughtworks .xstream .io .xml .DomDriver ;
@@ -65,8 +67,7 @@ public static void main(String[] args) {
6567 }
6668
6769 try {
68- try {
69- ObjectInputStream inOld = xs .createObjectInputStream (new FileReader (oldFile ));
70+ try (ObjectInputStream inOld = xs .createObjectInputStream (new FileReader (oldFile ));){
7071 while (true ) {
7172 Command c1 = (Command )inOld .readObject ();
7273 oldCommands .put (c1 .getName (), c1 );
@@ -75,8 +76,7 @@ public static void main(String[] args) {
7576 // EOF exception shows that there is no more objects in ObjectInputStream, so do nothing here
7677 }
7778
78- try {
79- ObjectInputStream inNew = xs .createObjectInputStream (new FileReader (newFile ));
79+ try (ObjectInputStream inNew = xs .createObjectInputStream (new FileReader (newFile ));){
8080 while (true ) {
8181 Command c = (Command )inNew .readObject ();
8282 commands .put (c .getName (), c );
@@ -89,28 +89,26 @@ public static void main(String[] args) {
8989 }
9090
9191 // Check if any commands got added in new version
92- for (String key : commands .keySet ()) {
93- if (!oldCommands .containsKey (key )) {
94- addedCommands .add (commands . get ( key ));
92+ for (Map . Entry < String , Command > entry : commands .entrySet ()) {
93+ if (!oldCommands .containsKey (entry . getKey () )) {
94+ addedCommands .add (entry . getValue ( ));
9595 } else {
96- stableCommands .put (commands . get ( key ).getName (), commands . get ( key ));
96+ stableCommands .put (entry . getValue ( ).getName (), entry . getValue ( ));
9797 }
9898 }
9999
100100 // Check if any commands were removed in new version
101- for (String key : oldCommands .keySet ()) {
102- if (!commands .containsKey (key )) {
103- removedCommands .add (oldCommands . get ( key ));
104- if (stableCommands .get (key ) != null ) {
105- stableCommands .remove (key );
101+ for (Map . Entry < String , Command > entry : oldCommands .entrySet ()) {
102+ if (!commands .containsKey (entry . getKey () )) {
103+ removedCommands .add (entry . getValue ( ));
104+ if (stableCommands .get (entry . getKey () ) != null ) {
105+ stableCommands .remove (entry . getKey () );
106106 }
107107 }
108108 }
109109
110- try {
111- FileWriter fstream = new FileWriter (dirName + "/diff.txt" );
112- BufferedWriter out = new BufferedWriter (fstream );
113-
110+ try (FileWriter fstream = new FileWriter (dirName + "/diff.txt" );
111+ BufferedWriter out = new BufferedWriter (fstream );){
114112 // Print added commands
115113 out .write ("Added commands:\n " );
116114 for (Command c : addedCommands ) {
@@ -135,13 +133,13 @@ public static void main(String[] args) {
135133
136134 out .write ("\n Changes in command type (sync versus async)\n " );
137135 // Verify if the command was sync and became async and vice versa
138- for (String key : stableCommands .keySet ()) {
139- if (commands .get (key ) .isAsync () != oldCommands .get (key ).isAsync ()) {
136+ for (Map . Entry < String , Command > entry : stableCommands .entrySet ()) {
137+ if (commands .get (entry . getKey ()) .isAsync () != oldCommands .get (entry . getKey () ).isAsync ()) {
140138 String type = "Sync" ;
141- if (commands .get (key ).isAsync ()) {
139+ if (commands .get (entry . getKey () ).isAsync ()) {
142140 type = "Async" ;
143141 }
144- out .write ("\n \t " + stableCommands . get ( key ).getName () + " became " + type );
142+ out .write ("\n \t " + entry . getValue ( ).getName () + " became " + type );
145143 }
146144 }
147145
@@ -181,8 +179,11 @@ public static void main(String[] args) {
181179 // Compare stable request arguments of old and new version
182180 for (Iterator <String > i = stableReqArgs .keySet ().iterator (); i .hasNext ();) {
183181 String argName = i .next ();
184- if (oldCommand .getReqArgByName (argName ).isRequired () == newCommand .getReqArgByName (argName ).isRequired ()) {
185- i .remove ();
182+ if ((oldCommand .getReqArgByName (argName ) != null ) && (newCommand .getReqArgByName (argName ) != null ))
183+ {
184+ if (oldCommand .getReqArgByName (argName ).isRequired ().equals (newCommand .getReqArgByName (argName ).isRequired ())) {
185+ i .remove ();
186+ }
186187 }
187188 }
188189
@@ -245,9 +246,9 @@ public static void main(String[] args) {
245246 for (Argument stableArg : stableReqArgs .values ()) {
246247 String newRequired = "optional" ;
247248 String oldRequired = "optional" ;
248- if (oldCommand .getReqArgByName (stableArg .getName ()). isRequired () == true )
249+ if (( oldCommand .getReqArgByName (stableArg .getName ()) != null ) && ( oldCommand . getReqArgByName ( stableArg . getName ()). isRequired () == true ) )
249250 oldRequired = "required" ;
250- if (newCommand .getReqArgByName (stableArg .getName ()). isRequired () == true )
251+ if (( newCommand .getReqArgByName (stableArg .getName ()) != null ) && ( newCommand . getReqArgByName ( stableArg . getName ()). isRequired () == true ) )
251252 newRequired = "required" ;
252253 changedParameters .append (stableArg .getName () + " (old version - " + oldRequired + ", new version - " + newRequired + "), " );
253254 }
@@ -285,9 +286,6 @@ public static void main(String[] args) {
285286 }
286287 }
287288 }
288-
289- out .close ();
290-
291289 } catch (IOException e ) {
292290 e .printStackTrace ();
293291 }
0 commit comments