/*
 * 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.changedetection.rules;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import org.gradle.api.NonNullApi;
import org.gradle.api.internal.changedetection.state.FileCollectionSnapshot;
import org.gradle.api.internal.changedetection.state.TaskExecution;

import java.util.Iterator;
import java.util.Map;

@NonNullApi
public abstract class AbstractNamedFileSnapshotTaskStateChanges implements TaskStateChanges {
    protected final TaskExecution previous;
    protected final TaskExecution current;
    private final String title;

    protected AbstractNamedFileSnapshotTaskStateChanges(TaskExecution previous, TaskExecution current, String title) {
        this.previous = previous;
        this.current = current;
        this.title = title;
    }

    private ImmutableSortedMap<String, FileCollectionSnapshot> getPrevious() {
        return getSnapshot(previous);
    }

    private ImmutableSortedMap<String, FileCollectionSnapshot> getCurrent() {
        return getSnapshot(current);
    }

    protected abstract ImmutableSortedMap<String, FileCollectionSnapshot> getSnapshot(TaskExecution execution);

    protected Iterator<TaskStateChange> getFileChanges(final boolean includeAdded) {
        return Iterators.concat(Iterables.transform(getCurrent().entrySet(), new Function<Map.Entry<String, FileCollectionSnapshot>, Iterator<TaskStateChange>>() {
            @Override
            public Iterator<TaskStateChange> apply(Map.Entry<String, FileCollectionSnapshot> entry) {
                String propertyName = entry.getKey();
                FileCollectionSnapshot currentSnapshot = entry.getValue();
                FileCollectionSnapshot previousSnapshot = getPrevious().get(propertyName);
                if (previousSnapshot == null) {
                    return Iterators.emptyIterator();
                }
                String propertyTitle = title + " property '" + propertyName + "'";
                return currentSnapshot.iterateContentChangesSince(previousSnapshot, propertyTitle, includeAdded);
            }
        }).iterator());
    }
}

@@ -695,118 +695,34 @@
 mon.
-base.Function;%0Aimport com.google.common.collect.ImmutableSortedMap;%0Aimport com.google.common.collect.Iterables
+collect.ImmutableSortedMap
 ;%0Aim
@@ -943,16 +943,44 @@
 ution;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -1015,19 +1015,20 @@
 va.util.
-Map
+List
 ;%0A%0A@NonN
@@ -1924,99 +1924,184 @@
     
-return Iterators.concat(Iterables.transform(getCurrent().entrySet(), new Function%3CMap.Entry
+final List%3CIterator%3CTaskStateChange%3E%3E changes = new ArrayList%3CIterator%3CTaskStateChange%3E%3E();%0A        SortedMapDiffUtil.diff(getPrevious(), getCurrent(), new PropertyDiffListener
 %3CStr
@@ -2132,36 +2132,8 @@
 hot%3E
-, Iterator%3CTaskStateChange%3E%3E
 () %7B
@@ -2178,86 +2178,43 @@
 lic 
-Iterator%3CTaskStateChange%3E apply(Map.Entry%3CString, FileCollectionSnapshot%3E entr
+void removed(String previousPropert
 y) %7B
@@ -2230,49 +2230,88 @@
     
+%7D%0A%0A
     
-String propertyName = entry.getKey();
+        @Override%0A            public void added(String currentProperty) %7B
 %0A   
@@ -2323,281 +2323,169 @@
     
+%7D%0A%0A
     
-FileCollectionSnapshot currentSnapshot = entry.getValue();%0A                FileCollectionSnapshot previousSnapshot = getPrevious().get(propertyName);%0A                if (previousSnapshot == null) %7B%0A                    return Iterators.emptyIterator();%0A                %7D
+        @Override%0A            public void maybeChanged(String property, FileCollectionSnapshot previousSnapshot, FileCollectionSnapshot currentSnapshot) %7B
 %0A   
@@ -2552,20 +2552,16 @@
 property
-Name
  + %22'%22;%0A
@@ -2576,23 +2576,28 @@
         
-return 
+changes.add(
 currentS
@@ -2677,16 +2677,17 @@
 deAdded)
+)
 ;%0A      
@@ -2704,16 +2704,58 @@
       %7D)
+;%0A%0A        return Iterators.concat(changes
 .iterato

Use SortedMapDiff for file changes, too