/*
 * Barrier.java
 * 
 * Author: nystrom
 * Creation date: Feb 4, 2005
 */
package polyglot.frontend.goals;

import java.util.Iterator;

import polyglot.frontend.*;
import polyglot.util.InternalCompilerError;

/**
 * Comment for <code>Barrier</code>
 *
 * @author nystrom
 */
public abstract class Barrier extends AbstractGoal {
    Scheduler scheduler;
    
    public Barrier(Scheduler scheduler) {
        super(null);
        this.scheduler = scheduler;
    }

    public Barrier(String name, Scheduler scheduler) {
        super(null, name);
        this.scheduler = scheduler;
    }

    /* (non-Javadoc)
     * @see polyglot.frontend.goals.Goal#createPass(polyglot.frontend.ExtensionInfo)
     */
    public Pass createPass(ExtensionInfo extInfo) {
        return new EmptyPass(this);
    }
    
    /* (non-Javadoc)
     * @see polyglot.frontend.goals.Goal#reached()
     */
    public int distanceFromGoal() {
        int distance = 0;
        
        for (Iterator i = scheduler.jobs().iterator(); i.hasNext(); ) {
            Job job = (Job) i.next();
            Goal subgoal = goalForJob(job);
            if (! scheduler.reached(subgoal)) {
                try {
                    scheduler.addPrerequisiteDependency(this, subgoal);
                    distance++;
                }
                catch (CyclicDependencyException e) {
                    throw new InternalCompilerError(e.getMessage());
                }
            }
        }
        
        return distance;
    }
    
    public abstract Goal goalForJob(Job job); 
    
    public int hashCode() {
        return System.identityHashCode(this);
    }

    public boolean equals(Object o) {
        return this == o;
    }
}

@@ -103,16 +103,65 @@
 goals;%0A%0A
+import java.util.*;%0Aimport java.util.Collection;%0A
 import j
@@ -1552,59 +1552,603 @@
   %7D%0A
-    %0A    public abstract Goal goalForJob(Job job); 
+%0A//    public Collection prerequisiteGoals(Scheduler scheduler) %7B%0A//        List l = new ArrayList();%0A//        l.addAll(super.prerequisiteGoals(scheduler));%0A//        %0A//        for (Iterator i = scheduler.jobs().iterator(); i.hasNext(); ) %7B%0A//            Job job = (Job) i.next();%0A//            Goal subgoal = goalForJob(job);%0A//            l.add(subgoal);%0A//        %7D%0A//        %0A//        return l;%0A//    %7D%0A    %0A    public abstract Goal goalForJob(Job job); %0A%0A    public String toString() %7B%0A        if (name == null) %7B%0A            return super.toString();%0A        %7D%0A        return name;%0A    %7D
 %0A   
@@ -2173,24 +2173,56 @@
 ashCode() %7B%0A
+        if (name == null) %7B%0A    
         retu
@@ -2263,73 +2263,336 @@
     
-%7D%0A%0A    public boolean equals(Object o) %7B%0A        return this == o
+    %7D%0A        else %7B%0A            return name.hashCode();%0A        %7D%0A    %7D%0A%0A    public boolean equals(Object o) %7B%0A        if (name == null) %7B%0A            return this == o;%0A        %7D%0A        else if (o instanceof Barrier) %7B%0A            Barrier b = (Barrier) o;%0A            return name.equals(b.name);%0A        %7D%0A        return false
 ;%0A  

Change hashCode and equals methods to prevent duplicate barriers from being created.