<commit_before>package jkanvas.groups;

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import jkanvas.animation.AnimationTiming;
import jkanvas.animation.Animator;
import jkanvas.painter.Renderpass;

/**
 * A group linearly ordering the members. Render-passes without bounding boxes
 * are <em>not</em> allowed.
 * 
 * @author Joschi <<EMAIL>>
 */
public class LinearGroup extends RenderGroup {

  /** The animation timing. */
  private AnimationTiming timing;

  /** The space between render-passes. */
  private double space;

  /** Whether the render-passes are laid out in horizontal direction. */
  private boolean horizontal;

  /** The alignment of the group. */
  private double alignmentFactor;

  /**
   * The alignment of a linear group. The alignment is meant relative to the
   * direction of the layout creation.
   * 
   * @author <NAME> <<EMAIL>>
   */
  public static enum Alignment {

    /** Aligns on the left side. */
    LEFT(0),

    /** Aligns in the middle. */
    MIDDLE(0.5),

    /** Aligns on the right side. */
    RIGHT(1),

    ;

    /** The alignment factor of the alignment. */
    public final double alignmentFactor;

    /**
     * Creates an alignment.
     * 
     * @param alignmentFactor The alignment factor.
     */
    private Alignment(final double alignmentFactor) {
      this.alignmentFactor = alignmentFactor;
    }

  }

  /**
   * Creates a linear group.
   * 
   * @param animator The underlying animator.
   * @param isHorizontal Whether render-passes are laid out in horizontal
   *          direction.
   * @param space The space between two render-passes.
   * @param timing The animation timing.
   */
  public LinearGroup(final Animator animator, final boolean isHorizontal,
      final double space, final AnimationTiming timing) {
    super(animator);
    horizontal = isHorizontal;
    alignmentFactor = Alignment.MIDDLE.alignmentFactor;
    this.space = space;
    this.timing = Objects.requireNonNull(timing);
  }

  @Override
  protected void doLayout(final List<RenderpassPosition> members) {
    final List<Rectangle2D> bboxes = new ArrayList<>(members.size());
    double max = 0;
    for(final RenderpassPosition p : members) {
      final Renderpass pass = p.pass;
      final Rectangle2D bbox = pass.getBoundingBox();
      if(bbox == null) throw new IllegalStateException("bbox must not be null");
      bboxes.add(bbox);
      if(!pass.isVisible()) {
        continue;
      }
      final double v = horizontal ? bbox.getHeight() : bbox.getWidth();
      if(v > max) {
        max = v;
      }
    }
    double pos = 0;
    for(int i = 0; i < members.size(); ++i) {
      final RenderpassPosition p = members.get(i);
      final Rectangle2D bbox = bboxes.get(i);
      if(!p.pass.isVisible()) {
        continue;
      }
      final double v = horizontal ? bbox.getHeight() : bbox.getWidth();
      final double opos = (max - v) * alignmentFactor;
      final Point2D dest = new Point2D.Double(
          horizontal ? pos : opos, horizontal ? opos : pos);
      p.startAnimationTo(dest, timing);<commit_msg>consistent use of state during layout<commit_after>package jkanvas.groups;

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import jkanvas.animation.AnimationTiming;
import jkanvas.animation.Animator;
import jkanvas.painter.Renderpass;

/**
 * A group linearly ordering the members. Render-passes without bounding boxes
 * are <em>not</em> allowed.
 * 
 * @author Joschi <<EMAIL>>
 */
public class LinearGroup extends RenderGroup {

  /** The animation timing. */
  private AnimationTiming timing;

  /** The space between render-passes. */
  private double space;

  /** Whether the render-passes are laid out in horizontal direction. */
  private boolean horizontal;

  /** The alignment of the group. */
  private double alignmentFactor;

  /**
   * The alignment of a linear group. The alignment is meant relative to the
   * direction of the layout creation.
   * 
   * @author <NAME> <<EMAIL>>
   */
  public static enum Alignment {

    /** Aligns on the left side. */
    LEFT(0),

    /** Aligns in the middle. */
    MIDDLE(0.5),

    /** Aligns on the right side. */
    RIGHT(1),

    ;

    /** The alignment factor of the alignment. */
    public final double alignmentFactor;

    /**
     * Creates an alignment.
     * 
     * @param alignmentFactor The alignment factor.
     */
    private Alignment(final double alignmentFactor) {
      this.alignmentFactor = alignmentFactor;
    }

  }

  /**
   * Creates a linear group.
   * 
   * @param animator The underlying animator.
   * @param isHorizontal Whether render-passes are laid out in horizontal
   *          direction.
   * @param space The space between two render-passes.
   * @param timing The animation timing.
   */
  public LinearGroup(final Animator animator, final boolean isHorizontal,
      final double space, final AnimationTiming timing) {
    super(animator);
    horizontal = isHorizontal;
    alignmentFactor = Alignment.MIDDLE.alignmentFactor;
    this.space = space;
    this.timing = Objects.requireNonNull(timing);
  }

  @Override
  protected void doLayout(final List<RenderpassPosition> members) {
    final AnimationTiming timing = getTiming();
    final boolean horizontal = isHorizontal();
    final double alignmentFactor = getAlignment();
    final double space = getSpace();
    final List<Rectangle2D> bboxes = new ArrayList<>(members.size());
    double max = 0;
    for(final RenderpassPosition p : members) {
      final Renderpass pass = p.pass;
      final Rectangle2D bbox = pass.getBoundingBox();
      if(bbox == null) throw new IllegalStateException("bbox must not be null");
      bboxes.add(bbox);
      if(!pass.isVisible()) {
        continue;
      }
      final double v = horizontal ? bbox.getHeight() : bbox.getWidth();
      if(v > max) {
        max = v;
      }
    }
    double pos = 0;
    for(int i = 0; i < members.size(); ++i) {
      final RenderpassPosition p = members.get(i);
      final Rectangle2D bbox = bboxes.get(i);
      if(!p.pass.isVisible()) {
        continue;
      }
      final double v = horizontal ? bbox.getHeight() : bbox.getWidth();
      final double opos = (max - v) * alignmentFactor;
      final Point2D dest = new Point2D.Double(
          horizontal ? pos : opos, horizontal ? opos : pos);
      p.startAnimationTo(dest, timing);
{'fraction_non_alphanumeric': 0.10675613847796249, 'fraction_numerical': 0.0042702455391185, 'mean_word_length': 2.844079718640094, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '5619959', 'n_tokens_mistral': 1954, 'n_tokens_neox': 1901, 'n_words': 586}