<commit_before>package org.sugarj.cleardep.build;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.sugarj.cleardep.BuildUnit;
import org.sugarj.cleardep.BuildUnit.State;
import org.sugarj.cleardep.dependency.BuildRequirement;
import org.sugarj.cleardep.output.BuildOutput;
import org.sugarj.cleardep.stamp.LastModifiedStamper;
import org.sugarj.common.path.Path;

public abstract class CompileCycleAtOnceBuilder<T extends Serializable, Out extends BuildOutput> extends Builder<ArrayList<T>, Out> {

  public static <X> ArrayList<X> singletonArrayList(X elem) {
    return new ArrayList<X>(Collections.<X> singletonList(elem));
  }

  private final BuilderFactory<ArrayList<T>, Out, ? extends CompileCycleAtOnceBuilder<T,Out>> factory;
  
  public CompileCycleAtOnceBuilder(T input, BuilderFactory<ArrayList<T>, Out, ? extends CompileCycleAtOnceBuilder<T,Out>> factory) {
    this(singletonArrayList(input), factory);
  }

  public CompileCycleAtOnceBuilder(ArrayList<T> input,BuilderFactory<ArrayList<T>, Out, ? extends CompileCycleAtOnceBuilder<T,Out>> factory) {
    super(input);
    this.factory = factory;
  }

  protected abstract Path singletonPersistencePath(T input);

  private List<BuildUnit<Out>> cyclicResults;

  @Override
  protected String cyclicTaskDescription(List<BuildRequirement<?>> cycle) {
    return this.taskDescription();
  }
  
  @Override
  public void requires(Path p) {
    for (BuildUnit<Out> result : cyclicResults) {
      result.requires(p, defaultStamper().stampOf(p));
    }
  }

  @Override
  public void generates(Path p) {
    for (BuildUnit<Out> result : cyclicResults) {
      result.generates(p, LastModifiedStamper.instance.stampOf(p));
     
    }
  }
  
  @Override
  public void setState(State state) {
    for (BuildUnit<Out> result : cyclicResults) {
      result.setState(state);
    }
  }

  @Override
  protected Path persistentPath() {
    if (this.input.size() == 1) {
      return this.singletonPersistencePath(this.input.get(0));
    } else {
      throw new AssertionError("Should not occur");
    }
  }

  @Override
  protected boolean canBuildCycle(List<BuildRequirement<?>> cycle) {
    for (BuildRequirement<?> req : cycle) {
      if (req.req.factory != this.factory) {
        System.out.println("Not the same factory");
        return false;
      }
      if (!(req.req.input instanceof ArrayList<?>)) {
        System.out.println("No array list input");
        return false;
      }
    }
    return true;
  }

  @Override
  protected void buildCycle(List<BuildRequirement<?>> cycle) throws Throwable {
   ArrayList<BuildUnit<Out>> cyclicResults = new ArrayList<>();
   ArrayList<T> inputs = new ArrayList<>();
   
    for (BuildRequirement<?> req : cycle) {
      cyclicResults.add((BuildUnit<Out>) req.unit);
      inputs.addAll((ArrayList<T>) req.req.input);
    }
    <commit_msg>temp fixed compile errors in CompileCycleAtOnceBuilder<commit_after>package org.sugarj.cleardep.build;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.sugarj.cleardep.BuildUnit;
import org.sugarj.cleardep.BuildUnit.State;
import org.sugarj.cleardep.dependency.BuildRequirement;
import org.sugarj.cleardep.output.BuildOutput;
import org.sugarj.cleardep.stamp.LastModifiedStamper;
import org.sugarj.common.path.Path;

public abstract class CompileCycleAtOnceBuilder<T extends Serializable, Out extends BuildOutput> extends Builder<ArrayList<T>, Out> {

  public static <X> ArrayList<X> singletonArrayList(X elem) {
    return new ArrayList<X>(Collections.<X> singletonList(elem));
  }

  private final BuilderFactory<ArrayList<T>, Out, ? extends CompileCycleAtOnceBuilder<T,Out>> factory;
  
  public CompileCycleAtOnceBuilder(T input, BuilderFactory<ArrayList<T>, Out, ? extends CompileCycleAtOnceBuilder<T,Out>> factory) {
    this(singletonArrayList(input), factory);
  }

  public CompileCycleAtOnceBuilder(ArrayList<T> input,BuilderFactory<ArrayList<T>, Out, ? extends CompileCycleAtOnceBuilder<T,Out>> factory) {
    super(input);
    this.factory = factory;
  }

  protected abstract Path singletonPersistencePath(T input);

  private List<BuildUnit<Out>> cyclicResults;

  //@Override
  protected String cyclicTaskDescription(List<BuildRequirement<?>> cycle) {
    return this.taskDescription();
  }
  
  @Override
  public void requires(Path p) {
    for (BuildUnit<Out> result : cyclicResults) {
      result.requires(p, defaultStamper().stampOf(p));
    }
  }

  @Override
  public void generates(Path p) {
    for (BuildUnit<Out> result : cyclicResults) {
      result.generates(p, LastModifiedStamper.instance.stampOf(p));
     
    }
  }
  
  @Override
  public void setState(State state) {
    for (BuildUnit<Out> result : cyclicResults) {
      result.setState(state);
    }
  }

  @Override
  protected Path persistentPath() {
    if (this.input.size() == 1) {
      return this.singletonPersistencePath(this.input.get(0));
    } else {
      throw new AssertionError("Should not occur");
    }
  }

  //@Override
  protected boolean canBuildCycle(List<BuildRequirement<?>> cycle) {
    for (BuildRequirement<?> req : cycle) {
      if (req.req.factory != this.factory) {
        System.out.println("Not the same factory");
        return false;
      }
      if (!(req.req.input instanceof ArrayList<?>)) {
        System.out.println("No array list input");
        return false;
      }
    }
    return true;
  }

  //@Override
  protected void buildCycle(List<BuildRequirement<?>> cycle) throws Throwable {
   ArrayList<BuildUnit<Out>> cyclicResults = new ArrayList<>();
   ArrayList<T> inputs = new ArrayList<>();
   
    for (BuildRequirement<?> req : cycle) {
      cyclicResults.add((BuildUnit<Out>) req.unit);
      inputs.addAll((ArrayList<T>) req.req.input);
    }
    
{'fraction_non_alphanumeric': 0.12014254199898185, 'fraction_numerical': 0.0006787714237230613, 'mean_word_length': 4.089810017271157, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6539408', 'n_tokens_mistral': 1821, 'n_tokens_neox': 1794, 'n_words': 377}