/*
 * Copyright (C) 2014  Camptocamp
 *
 * This file is part of MapFish Print
 *
 * MapFish Print is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MapFish Print is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MapFish Print.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.mapfish.print.processor;

import java.util.HashSet;
import java.util.Set;


/**
 * Models a dependency between two processors.
 */
public class ProcessorDependency {

    private final Class<? extends AbstractProcessor<?, ?>> required;
    private final Class<? extends AbstractProcessor<?, ?>> dependent;
    private final Set<String> commonInputs;
    
    /**
     * Constructor.
     * The processor <code>dependent</code> requires the processor <code>required</code>.
     * 
     * @param required The processor which is required to be executed before the other.
     * @param dependent The processor which requires the other to be executed first.
     * @param commonInputs The dependency is only enforced if the two processors have these inputs in common.
     */
    public ProcessorDependency(
            final Class<? extends AbstractProcessor<?, ?>> required,
            final Class<? extends AbstractProcessor<?, ?>> dependent,
            final Set<String> commonInputs) {
        this.required = required;
        this.dependent = dependent;
        this.commonInputs = commonInputs;
    }
    
    /**
     * Constructor.
     * The processor <code>dependent</code> requires the processor <code>required</code>.
     * 
     * @param required The processor which is required to be executed before the other.
     * @param dependent The processor which requires the other to be executed first.
     */
    public ProcessorDependency(
            final Class<? extends AbstractProcessor<?, ?>> required,
            final Class<? extends AbstractProcessor<?, ?>> dependent) {
        this(required, dependent, new HashSet<String>());
    }

    /**
     * Returns the processor which is required to be executed before the other.
     */
    public final Class<? extends AbstractProcessor<?, ?>> getRequired() {
        return this.required;
    }

    /**
     * Returns the processor which requires the other to be executed first.
     */
    public final Class<? extends AbstractProcessor<?, ?>> getDependent() {
        return this.dependent;
    }

    /**
     * The inputs that both processors must have in common.
     */
    public final Set<String> getCommonInputs() {
        return this.commonInputs;
    }

}

@@ -766,16 +766,86 @@
 essor;%0A%0A
+import com.google.common.collect.Sets;%0A%0Aimport java.util.Collections;%0A
 import j
@@ -1007,32 +1007,24 @@
 s%3C? extends 
-Abstract
 Processor%3C?,
@@ -1068,32 +1068,24 @@
 s%3C? extends 
-Abstract
 Processor%3C?,
@@ -1265,33 +1265,32 @@
 d%3C/code%3E.%0A     *
- 
 %0A     * @param r
@@ -1627,32 +1627,24 @@
 s%3C? extends 
-Abstract
 Processor%3C?,
@@ -1688,32 +1688,24 @@
 s%3C? extends 
-Abstract
 Processor%3C?,
@@ -1864,16 +1864,32 @@
 nputs = 
+Sets.newHashSet(
 commonIn
@@ -1884,32 +1884,33 @@
 Set(commonInputs
+)
 ;%0A    %7D%0A    %0A   
@@ -2275,32 +2275,24 @@
 s%3C? extends 
-Abstract
 Processor%3C?,
@@ -2336,32 +2336,24 @@
 s%3C? extends 
-Abstract
 Processor%3C?,
@@ -2560,32 +2560,24 @@
 s%3C? extends 
-Abstract
 Processor%3C?,
@@ -2763,16 +2763,8 @@
 nds 
-Abstract
 Proc
@@ -2967,24 +2967,52 @@
      return 
+Collections.unmodifiableSet(
 this.commonI
@@ -3012,23 +3012,24 @@
 commonInputs
+)
 ;%0A    %7D%0A%0A%7D%0A
@@ -3026,10 +3026,542 @@
 %0A    %7D%0A%0A
+    /**%0A     * Add a common input to this dependency.%0A     *%0A     * @param inputName the name of the input to add%0A     */%0A    public final void addCommonInput(final String inputName) %7B%0A        this.commonInputs.add(inputName);%0A    %7D%0A%0A    @Override%0A    public final String toString() %7B%0A        return %22ProcessorDependency%7B%22 +%0A               %22required=%22 + this.required.getSimpleName() +%0A               %22, dependent=%22 + this.dependent.getSimpleName() +%0A               %22, commonInputs=%22 + this.commonInputs +%0A               '%7D';%0A    %7D%0A
 %7D%0A

generalize ProcessorDependency from AbstractProcessor to Processor.