IntervalDomainIterator.java

  1. /* Copyright 2015 Laurent COCAULT
  2.  * Licensed to Laurent COCAULT under one or more contributor license agreements.
  3.  * See the NOTICE file distributed with this work for additional information
  4.  * regarding copyright ownership. Laurent COCAULT licenses this file to You
  5.  * under the Apache License, Version 2.0 (the "License"); you may not use this
  6.  * file except in compliance with the License.  You may obtain a copy of the
  7.  * License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.csp.constraint.model;

  18. import java.util.Iterator;
  19. import java.util.NoSuchElementException;

  20. /**
  21.  * Iterator on the values of an interval domain.
  22.  */
  23. class IntervalDomainIterator<T extends Value<T>> implements Iterator<T> {

  24.     /**
  25.      * Domain of values to iterate.
  26.      */
  27.     private IntervalDomain<T> domain_;

  28.     /**
  29.      * Current value.
  30.      */
  31.     private T value_;

  32.     /**
  33.      * Next value.
  34.      */
  35.     private T next_;

  36.     /**
  37.      * Constructor of the iterator.
  38.      * @param domain
  39.      *            Domain of values to iterate
  40.      */
  41.     public IntervalDomainIterator(final IntervalDomain<T> domain) {
  42.         domain_ = domain;
  43.         value_ = null;
  44.         next_ = domain.getFirstValue();
  45.     }

  46.     /**
  47.      * {@inheritDoc}
  48.      */
  49.     @Override
  50.     public boolean hasNext() {
  51.         return next_ != null;
  52.     }

  53.     /**
  54.      * {@inheritDoc}
  55.      */
  56.     @Override
  57.     public T next() {

  58.         // Value is the next value
  59.         if (next_ == null) {
  60.             throw new NoSuchElementException();
  61.         }
  62.         value_ = next_;

  63.         // Compute the next one
  64.         boolean finished = false;
  65.         final Iterator<Interval<T>> index = domain_.getValues().iterator();
  66.         while (index.hasNext() && !finished) {
  67.             final Interval<T> interval = index.next();
  68.             if (interval.isInInterval(value_)) {
  69.                 // Current value found : global iteration is over
  70.                 finished = true;

  71.                 if (interval.isInInterval(value_.nextValue())) {
  72.                     // The next value is in the interval
  73.                     next_ = value_.nextValue();
  74.                 } else if (index.hasNext()) {
  75.                     // Get the next internal minimum
  76.                     next_ = index.next().getMinValue();
  77.                 } else {
  78.                     // No next value
  79.                     next_ = null;
  80.                 }
  81.             }
  82.         }

  83.         return value_;
  84.     }

  85.     /**
  86.      * {@inheritDoc}
  87.      */
  88.     @Override
  89.     public void remove() {
  90.         if (value_ == null) {
  91.             // Next has not been called yet
  92.             throw new IllegalStateException();
  93.         } else {
  94.             // Remove the value
  95.             domain_.removeValue(value_);
  96.         }
  97.     }

  98. }