/*
 * Copyright 2016, Google Inc.
 * 
 * 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 kiwi.core;

import java.util.ArrayDeque;

/** PropagQueue */
public class PropagQueue {

  private final ArrayDeque<Propagator> queue = new ArrayDeque<Propagator>();

  /** Enqueues the propagator for propagation if needed. 
   * 
   *  Side effect: this method sets the inQueue boolean of propagator to true. 
   */
  public void enqueue(Propagator propagator) {
    if (!propagator.enqueued) {
      queue.addLast(propagator);
      propagator.enqueued = true;
    }
  }

  // Propagate all the propagator in the queue. Note that propagation is
  // likely to enqueue additional propagator while it is running. The method
  // return true if the propagation succeeded and false otherwise. The enqueued
  // boolean of all the propagator in the queue is set to false no matter if
  // the propagation succeeded or not.
  public boolean propagate() {
    boolean feasible = true;
    while (!queue.isEmpty()) {
      Propagator propagator = queue.removeFirst();
      // Propagate only if unfailed.
      feasible = feasible && propagator.propagate();
      // Dequeue the propagator after propagation to prevent
      // it from enqueuing itself.
      propagator.enqueued = false;
    }
    return feasible;
  }
}

/*
 * Copyright 2016, Google Inc.
 * 
 * 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 kiwi.core;

import java.util.ArrayDeque;

/** PropagQueue */
public class PropagQueue {
  // The propagation queue that contains all the propagators waiting for
  // propagation. All the contained propagator should have their enqueued
  // boolean set to true.
  private final ArrayDeque<Propagator> queue = new ArrayDeque<Propagator>();

  /** 
   * Enqueues the propagator for propagation if needed. 
   * 
   * <p>
   * Side effect: this method sets the enqueued boolean of propagator to true. 
   * </p>
   */
  public void enqueue(Propagator propagator) {
    if (!propagator.enqueued) {
      queue.addLast(propagator);
      propagator.enqueued = true;
    }
  }

  /**
   * Propagates the pending propagators.
   * 
   * <p>
   * Propagate all the propagators contained in the propagation queue.
   * Propagation is likely to enqueue additional propagators while it is
   * running. The propagation stops either when the queue is empty, or if
   * a propagator failed (meaning that the problem is infeasible).
   * </p>
   * 
   * <p>
   * The method returns true if the propagation succeded or false if a
   * propagator failed. The queue is empty at the end of the propagation and all
   * propagators contained in the queue have their enqueued boolean set to
   * false.
   * </p>
   * 
   * @return true if propagation succeeded, false otherwise.
   */
  public boolean propagate() {
    boolean feasible = true;
    while (!queue.isEmpty()) {
      Propagator propagator = queue.removeFirst();
      // Propagate only if the problem is still feasible.
      feasible = feasible && propagator.propagate();
      // Dequeue the propagator after propagation to prevent it from enqueuing 
      // itself.
      propagator.enqueued = false;
    }
    return feasible;
  }
}

Add Java doc to PropagQueue.