- removed iterator code fromModifiedSelector
- lazier DigestAlgorithm.getValue (includes JUnit test)
- removed unncecessary nesting in EqualsComparator

//
// ----- Instantiate the interfaces -----
//
String className = null;
String pkg = "org.apache.tools.ant.types.selectors.modifiedselector";


what do these lines do in ModifiedSelector.configure? Eclipse says that they're never read, and as they're method variables, not class or instance variables (ie not public), I was very tempted to delete them, but I thought I'd better ask first in case they're present to support future functionality

Kev
Index: DigestAlgorithm.java
===================================================================
RCS file: 
/home/cvspublic/ant/src/main/org/apache/tools/ant/types/selectors/modifiedselector/DigestAlgorithm.java,v
retrieving revision 1.7
diff -u -r1.7 DigestAlgorithm.java
--- DigestAlgorithm.java        10 Jul 2004 17:15:37 -0000      1.7
+++ DigestAlgorithm.java        28 Feb 2005 03:46:53 -0000
@@ -1,5 +1,5 @@
 /*
- * Copyright  2003-2004 The Apache Software Foundation
+ * Copyright  2003-2005 The Apache Software Foundation
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -20,11 +20,11 @@
 
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileOutputStream;
 import java.security.DigestInputStream;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
 import java.security.NoSuchProviderException;
+
 import org.apache.tools.ant.BuildException;
 
 
@@ -78,7 +78,7 @@
     /**
      * Size of the read buffer to use.
      */
-    private int readBufferSize = 8 * 1024;
+    private static final int READ_BUFFER_SIZE = 8 * 1024;
 
 
     // -----  Algorithm-Configuration  -----
@@ -146,22 +146,18 @@
      * @return        The value for that file
      */
     // implementation adapted from ...taskdefs.Checksum, thanks to Magesh for 
hint
-    public String getValue(File file) {
-        initMessageDigest();
-        String checksum = null;
-        try {
-            if (!file.canRead()) {
-                return null;
-            }
-            FileInputStream fis = null;
-            FileOutputStream fos = null;
-            byte[] buf = new byte[readBufferSize];
+    public String getValue(final File file) {
+               String checksum = null; 
+       if (file.canRead()) {
+               initMessageDigest();
+               FileInputStream fis = null;
+            byte[] buf = new byte[READ_BUFFER_SIZE];
             try {
-                messageDigest.reset();
+               messageDigest.reset();
                 fis = new FileInputStream(file);
                 DigestInputStream dis = new DigestInputStream(fis,
                                                               messageDigest);
-                while (dis.read(buf, 0, readBufferSize) != -1) {
+                while (dis.read(buf, 0, READ_BUFFER_SIZE) != -1) {
                     // do nothing
                 }
                 dis.close();
@@ -178,13 +174,11 @@
                 }
                 checksum = checksumSb.toString();
             } catch (Exception e) {
-                return null;
+               //do nothing
             }
-        } catch (Exception e) {
-            return null;
-        }
-        return checksum;
-    }
+       }
+       return checksum;
+       }
 
 
     /**
Index: EqualComparator.java
===================================================================
RCS file: 
/home/cvspublic/ant/src/main/org/apache/tools/ant/types/selectors/modifiedselector/EqualComparator.java,v
retrieving revision 1.6
diff -u -r1.6 EqualComparator.java
--- EqualComparator.java        9 Mar 2004 16:48:49 -0000       1.6
+++ EqualComparator.java        28 Feb 2005 03:46:53 -0000
@@ -41,12 +41,10 @@
         if (o1 == null) {
             if (o2 == null) {
                 return 1;
-            } else {
-                return 0;
             }
-        } else {
-            return (o1.equals(o2)) ? 0 : 1;
+            return 0;
         }
+        return (o1.equals(o2)) ? 0 : 1;
     }
 
     /**
Index: ModifiedSelector.java
===================================================================
RCS file: 
/home/cvspublic/ant/src/main/org/apache/tools/ant/types/selectors/modifiedselector/ModifiedSelector.java,v
retrieving revision 1.15
diff -u -r1.15 ModifiedSelector.java
--- ModifiedSelector.java       7 Jan 2005 15:16:54 -0000       1.15
+++ ModifiedSelector.java       28 Feb 2005 03:46:55 -0000
@@ -21,7 +21,6 @@
 // Java
 import java.util.Comparator;
 import java.util.Vector;
-import java.util.Iterator;
 import java.io.File;
 
 // Ant
@@ -309,6 +308,7 @@
 
     /** Bean-Constructor. */
     public ModifiedSelector() {
+       //default
     }
 
 
@@ -382,8 +382,8 @@
         //
         // -----  Set the main attributes, pattern '*'  -----
         //
-        for (Iterator itConfig = configParameter.iterator(); 
itConfig.hasNext();) {
-            Parameter par = (Parameter) itConfig.next();
+        for (int i = 0, size = configParameter.size(); i < size; i++) {
+               Parameter par = (Parameter) configParameter.get(i);
             if (par.getName().indexOf(".") > 0) {
                 // this is a *.* parameter for later use
                 specialParameter.add(par);
@@ -467,8 +467,8 @@
         //
         // -----  Set the special attributes, pattern '*.*'  -----
         //
-        for (Iterator itSpecial = specialParameter.iterator(); 
itSpecial.hasNext();) {
-            Parameter par = (Parameter) itSpecial.next();
+        for (int i = 0, size = specialParameter.size(); i < size; i++) {
+               Parameter par = (Parameter) specialParameter.get(i);
             useParameter(par);
         }
         specialParameter = new Vector();
Index: DigestAlgorithmTest.java
===================================================================
RCS file: DigestAlgorithmTest.java
diff -N DigestAlgorithmTest.java
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ DigestAlgorithmTest.java    1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,87 @@
+package org.apache.tools.ant.types.selectors.modifiedselector;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.security.DigestInputStream;
+import java.security.MessageDigest;
+
+import junit.framework.TestCase;
+
+import org.apache.tools.ant.util.FileUtils;
+
+public class DigestAlgorithmTest extends TestCase {
+
+       private DigestAlgorithm da = new DigestAlgorithm();
+       private MessageDigest messageDigest = null;
+       private FileUtils fu = FileUtils.getFileUtils();
+       private File dir = new File(".");
+       private int readBufferSize = 8 * 1024;
+       
+       private File f = fu.createTempFile("TEST", "TEST", dir);
+       
+       /*
+        * @see TestCase#setUp()
+        */
+       protected void setUp() throws Exception {
+               super.setUp();
+       }
+
+       /*
+        * @see TestCase#tearDown()
+        */
+       protected void tearDown() throws Exception {
+               super.tearDown();
+       }
+       
+       public String getValueOld(File file) {
+               da.initMessageDigest();
+        String checksum = null;
+        try {
+            if (!file.canRead()) {
+                return null;
+            }
+            FileInputStream fis = null;
+            FileOutputStream fos = null;
+            byte[] buf = new byte[readBufferSize];
+            try {
+                messageDigest.reset();
+                fis = new FileInputStream(file);
+                DigestInputStream dis = new DigestInputStream(fis,
+                                                              messageDigest);
+                while (dis.read(buf, 0, readBufferSize) != -1) {
+                    // do nothing
+                }
+                dis.close();
+                fis.close();
+                fis = null;
+                byte[] fileDigest = messageDigest.digest();
+                StringBuffer checksumSb = new StringBuffer();
+                for (int i = 0; i < fileDigest.length; i++) {
+                    String hexStr = Integer.toHexString(0x00ff & 
fileDigest[i]);
+                    if (hexStr.length() < 2) {
+                        checksumSb.append("0");
+                    }
+                    checksumSb.append(hexStr);
+                }
+                checksum = checksumSb.toString();
+            } catch (Exception e) {
+                return null;
+            }
+        } catch (Exception e) {
+            return null;
+        }
+        return checksum;
+       }
+       
+       public void testGetValue() {
+               try {
+                       DigestAlgorithm da = new DigestAlgorithm();
+                       assertEquals(da.getValue(f), this.getValueOld(f));
+               } catch (Exception e) {
+                       e.printStackTrace();
+                       fail();
+               }
+       }
+
+}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to