/*
 * Copyright 2016 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.archive;

import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import org.gradle.api.file.RelativePath;
import org.gradle.api.internal.file.CopyActionProcessingStreamAction;
import org.gradle.api.internal.file.copy.CopyAction;
import org.gradle.api.internal.file.copy.CopyActionProcessingStream;
import org.gradle.api.internal.file.copy.FileCopyDetailsInternal;
import org.gradle.api.tasks.WorkResult;

import java.util.Comparator;
import java.util.Map;

public class ReproducibleOrderingCopyActionDecorator implements CopyAction {
    private final CopyAction delegate;
    private final Comparator<RelativePath> comparator;

    public ReproducibleOrderingCopyActionDecorator(CopyAction delegate) {
        this(delegate, Ordering.<RelativePath>natural());
    }

    public ReproducibleOrderingCopyActionDecorator(CopyAction delegate, Comparator<RelativePath> comparator) {
        this.delegate = delegate;
        this.comparator = comparator;
    }

    @Override
    public WorkResult execute(final CopyActionProcessingStream stream) {
        return delegate.execute(new CopyActionProcessingStream() {
            public void process(final CopyActionProcessingStreamAction action) {
                // Sort all of the files going into the underlying CopyAction
                final Map<RelativePath, FileCopyDetailsInternal> files = Maps.newTreeMap(comparator);
                stream.process(new CopyActionProcessingStreamAction() {
                    public void processFile(FileCopyDetailsInternal details) {
                        files.put(details.getRelativePath(), details);
                    }
                });
                // Call the underlying CopyAction with the sorted collection of files
                for (FileCopyDetailsInternal fileCopyDetailsInternal : files.values()) {
                    action.processFile(fileCopyDetailsInternal);
                }
            }
        });
    }
}

@@ -686,20 +686,21 @@
 mon.
-collect.Maps
+base.Function
 ;%0Aim
@@ -1080,16 +1080,74 @@
 esult;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Collections;%0A
 import j
@@ -1184,19 +1184,20 @@
 va.util.
-Map
+List
 ;%0A%0Apubli
@@ -2031,26 +2031,13 @@
 nal 
-Map%3CRelativePath, 
+List%3C
 File
@@ -2069,34 +2069,47 @@
 s = 
-Maps.newTreeMap(comparator
+new ArrayList%3CFileCopyDetailsInternal%3E(
 );%0A 
@@ -2292,19 +2292,19 @@
 les.
-put
+add
 (details
 .get
@@ -2303,12 +2303,168 @@
 ails
-.get
+);%0A                    %7D%0A                %7D);%0A                Collections.sort(files, Ordering.from(comparator).onResultOf(new Function%3CFileCopyDetailsInternal, 
 Rela
@@ -2475,19 +2475,176 @@
 Path
+%3E
 ()
-, details
+ %7B%0A                    @Override%0A                    public RelativePath apply(FileCopyDetailsInternal input) %7B%0A                        return input.getRelativePath(
 );%0A 
@@ -2674,32 +2674,33 @@
               %7D)
+)
 ;%0A              
@@ -2851,17 +2851,8 @@
 iles
-.values()
 ) %7B%0A

Allow duplicates in sorted archives