Why does the code not use generic for loops?

If there is a good reason for this, it should be documented.

If not, the code should use the new generic loop as it's simpler and
less prone to errors (cannot use wrong index).

On 14 May 2016 at 02:47,  <ggreg...@apache.org> wrote:
> Author: ggregory
> Date: Sat May 14 01:47:35 2016
> New Revision: 1743780
>
> URL: http://svn.apache.org/viewvc?rev=1743780&view=rev
> Log:
> [CODEC-212] Create a minimal Digest command line utility: 
> org.apache.commons.codec.digest.Digest.
>
> Added:
>     
> commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/Digest.java
> Modified:
>     commons/proper/codec/trunk/src/changes/changes.xml
>
> Modified: commons/proper/codec/trunk/src/changes/changes.xml
> URL: 
> http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/changes/changes.xml?rev=1743780&r1=1743779&r2=1743780&view=diff
> ==============================================================================
> --- commons/proper/codec/trunk/src/changes/changes.xml (original)
> +++ commons/proper/codec/trunk/src/changes/changes.xml Sat May 14 01:47:35 
> 2016
> @@ -47,6 +47,7 @@ The <action> type attribute can be add,u
>        <action dev="ggregory" type="fix" issue="CODEC-207" due-to="Gary 
> Gregory">Charsets Javadoc breaks build when using Java 8</action>
>        <action dev="ggregory" type="fix" issue="CODEC-199" due-to="Yossi 
> Tamari">Bug in HW rule in Soundex</action>
>        <action dev="ggregory" type="fix" issue="CODEC-209" due-to="Gary 
> Gregory">Javadoc for SHA-224 DigestUtils methods should mention Java 1.8.0 
> restriction instead of 1.4.0.</action>
> +      <action dev="ggregory" type="add" issue="CODEC-212" due-to="Gary 
> Gregory">Create a minimal Digest command line utility: 
> org.apache.commons.codec.digest.Digest</action>
>        <action dev="ggregory" type="add" issue="CODEC-211" due-to="Gary 
> Gregory">Create enum MessageDigestAlgorithm and deprecate class 
> MessageDigestAlgorithms</action>
>        <action dev="ggregory" type="add" issue="CODEC-210" due-to="Gary 
> Gregory">Add DigestUtils.getDigest(String, MessageDigest)</action>
>        <action dev="ggregory" type="add" issue="CODEC-208" due-to="Gary 
> Gregory">Make some DigestUtils APIs public</action>
>
> Added: 
> commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/Digest.java
> URL: 
> http://svn.apache.org/viewvc/commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/Digest.java?rev=1743780&view=auto
> ==============================================================================
> --- 
> commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/Digest.java
>  (added)
> +++ 
> commons/proper/codec/trunk/src/main/java/org/apache/commons/codec/digest/Digest.java
>  Sat May 14 01:47:35 2016
> @@ -0,0 +1,120 @@
> +/*
> + * Licensed to the Apache Software Foundation (ASF) under one or more
> + * contributor license agreements.  See the NOTICE file distributed with
> + * this work for additional information regarding copyright ownership.
> + * The ASF licenses this file to You 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 org.apache.commons.codec.digest;
> +
> +import java.io.File;
> +import java.io.IOException;
> +import java.security.MessageDigest;
> +
> +import org.apache.commons.codec.binary.Hex;
> +
> +/**
> + * A minimal command line to run digest over files.
> + *
> + * @see #main(String[])
> + */
> +public class Digest {
> +
> +    /**
> +     * Runs the digest algorithm in {@code args[0]} on the file in {@code 
> args[1]}. If there is no {@code args[1]}, use
> +     * standard input.
> +     *
> +     * <p>
> +     * The algorithm can also be {@code ALL} or {@code *} to output one line 
> for each known algorithm.
> +     * </p>
> +     *
> +     * @param args
> +     *            {@code args[0]} is one of {@link MessageDigestAlgorithm} 
> name, {@link MessageDigest} name, {@code ALL}
> +     *            , or {@code *}. {@code args[1]} is a FILE.
> +     * @throws IOException
> +     */
> +    public static void main(String[] args) throws IOException {
> +        new Digest(args).run();
> +    }
> +
> +    private final String algorithm;
> +    private final String[] args;
> +    private final String source;
> +
> +    private Digest(final String[] args) {
> +        if (args == null) {
> +            throw new IllegalArgumentException("args");
> +        }
> +        if (args.length == 0) {
> +            throw new IllegalArgumentException(
> +                    String.format("Usage: java %s [algorithm] 
> [FILE|DIRECTORY]", Digest.class.getName()));
> +        }
> +        this.args = args;
> +        algorithm = args[0];
> +        source = args.length == 1 ? null : args[1];
> +    }
> +
> +    private void println(String prefix, final byte[] digest) {
> +        final String sourceDesc = source == null ? "-" : source;
> +        System.out.println(prefix + Hex.encodeHexString(digest) + " " + 
> sourceDesc);
> +    }
> +
> +    private void run() throws IOException {
> +        if (algorithm.equalsIgnoreCase("ALL") || algorithm.equals("*")) {
> +            run(MessageDigestAlgorithm.values());
> +            return;
> +        }
> +        final MessageDigest messageDigest = DigestUtils.getDigest(algorithm, 
> null);
> +        if (messageDigest != null) {
> +            run("", messageDigest);
> +        } else {
> +            run("", 
> MessageDigestAlgorithm.valueOf(algorithm).getMessageDigest());
> +        }
> +    }
> +
> +    private void run(MessageDigestAlgorithm[] digestAlgorithms) throws 
> IOException {
> +        for (int i = 0; i < digestAlgorithms.length; i++) {
> +            final MessageDigestAlgorithm messageDigestAlgorithm = 
> digestAlgorithms[i];
> +            if (messageDigestAlgorithm.isAvailable()) {
> +                run(messageDigestAlgorithm.getAlgorithm() + " ", 
> messageDigestAlgorithm);
> +            }
> +        }
> +    }
> +
> +    private void run(String prefix, final MessageDigest messageDigest) 
> throws IOException {
> +        if (source == null) {
> +            println(prefix, DigestUtils.digest(messageDigest, System.in));
> +            return;
> +        }
> +        final File file = new File(source);
> +        if (file.isFile()) {
> +            println(prefix, DigestUtils.digest(messageDigest, file));
> +        } else if (file.isDirectory()) {
> +            run(prefix, messageDigest, file.listFiles());
> +        }
> +    }
> +
> +    private void run(String prefix, MessageDigest messageDigest, File[] 
> files) throws IOException {
> +        for (int i = 0; i < files.length; i++) {
> +            println(prefix, DigestUtils.digest(messageDigest, files[i]));
> +        }
> +    }
> +
> +    private void run(String prefix, final MessageDigestAlgorithm 
> messageDigestAlgorithm) throws IOException {
> +        run(prefix, messageDigestAlgorithm.getMessageDigest());
> +    }
> +
> +    @Override
> +    public String toString() {
> +        return String.format("%s[%s]", super.toString(), args);
> +    }
> +}
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org

Reply via email to