/*
 * Copyright 2015 the original author or authors.
 *
 * Licensed 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.gradle.api.internal.file;

import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
import org.gradle.api.file.FileTreeElement;
import org.gradle.internal.UncheckedException;
import org.gradle.internal.serialize.Encoder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class FileTreeElementHasher {
    private static final byte HASH_PATH_SEPARATOR = (byte) '/';
    private static final byte HASH_FIELD_SEPARATOR = (byte) '\t';
    private static final byte HASH_RECORD_SEPARATOR = (byte) '\n';

    public static final int calculateHashForFileMetadata(Collection<? extends FileTreeElement> allFileTreeElements) {
        Collection<FileTreeElement> sortedFileTreeElement = sortForHashing(allFileTreeElements);

        BufferedStreamingHasher hasher = new BufferedStreamingHasher();
        Encoder encoder = hasher.getEncoder();
        try {
            for (FileTreeElement fileTreeElement : sortedFileTreeElement) {
                for (String pathPart : fileTreeElement.getRelativePath().getSegments()) {
                    encoder.writeString(pathPart);
                    encoder.writeByte(HASH_PATH_SEPARATOR);
                }
                if (!fileTreeElement.isDirectory()) {
                    encoder.writeByte(HASH_FIELD_SEPARATOR);
                    encoder.writeLong(fileTreeElement.getSize());
                    encoder.writeByte(HASH_FIELD_SEPARATOR);
                    encoder.writeLong(fileTreeElement.getLastModified());
                }
                encoder.writeByte(HASH_RECORD_SEPARATOR);
            }
            return hasher.checksum();
        } catch (IOException e) {
            throw UncheckedException.throwAsUncheckedException(e);
        }
    }

    public static Hasher createHasher() {
        return Hashing.crc32().newHasher();
    }

    public static final int calculateHashForFilePaths(Collection<? extends FileTreeElement> allFileTreeElements) {
        Collection<FileTreeElement> sortedFileTreeElement = sortForHashing(allFileTreeElements);

        BufferedStreamingHasher hasher = new BufferedStreamingHasher();
        Encoder encoder = hasher.getEncoder();
        try {
            for (FileTreeElement fileTreeElement : sortedFileTreeElement) {
                for (String pathPart : fileTreeElement.getRelativePath().getSegments()) {
                    encoder.writeString(pathPart);
                    encoder.writeByte(HASH_PATH_SEPARATOR);
                }
                encoder.writeByte(HASH_RECORD_SEPARATOR);
            }
            return hasher.checksum();
        } catch (IOException e) {
            throw UncheckedException.throwAsUncheckedException(e);
        }

    }

    private static Collection<FileTreeElement> sortForHashing(Collection<? extends FileTreeElement> allFileTreeElements) {
        List<FileTreeElement> sortedFileTreeElement = new ArrayList<FileTreeElement>(allFileTreeElements);
        Collections.sort(sortedFileTreeElement, FileTreeElementComparator.INSTANCE);
        return sortedFileTreeElement;
    }
}

@@ -914,20 +914,17 @@
 il.Array
-List
+s
 ;%0Aimport
@@ -949,61 +949,8 @@
 ion;
-%0Aimport java.util.Collections;%0Aimport java.util.List;
 %0A%0Apu
@@ -1304,35 +1304,24 @@
 ) %7B%0A        
-Collection%3C
 FileTreeElem
@@ -1315,33 +1315,34 @@
  FileTreeElement
-%3E
+%5B%5D
  sortedFileTreeE
@@ -2594,27 +2594,16 @@
         
-Collection%3C
 FileTree
@@ -2601,33 +2601,34 @@
  FileTreeElement
-%3E
+%5B%5D
  sortedFileTreeE
@@ -3350,27 +3350,16 @@
  static 
-Collection%3C
 FileTree
@@ -3361,25 +3361,26 @@
 eTreeElement
-%3E
+%5B%5D
  sortForHash
@@ -3452,21 +3452,16 @@
         
-List%3C
 FileTree
@@ -3467,17 +3467,18 @@
 eElement
-%3E
+%5B%5D
  sortedF
@@ -3498,22 +3498,11 @@
 t = 
-new ArrayList%3C
+all
 File
@@ -3516,13 +3516,22 @@
 ment
-%3E(all
+s.toArray(new 
 File
@@ -3541,17 +3541,19 @@
 eElement
-s
+%5B0%5D
 );%0A     
@@ -3555,26 +3555,21 @@
         
-Collection
+Array
 s.sort(s

Use Arrays.sort and use result in array