/**
 * Chart2D, a java library for drawing two dimensional charts.
 * Copyright (C) 2001 Jason J. Simas
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


package net.sourceforge.chart2d;


import java.util.Vector;


/**
 * Shared bookkeeping for properties that track per-dependent update state.
 */
final class DependentUpdateTracker {

  private final Vector dependents;
  private final Vector needsUpdateFlags;


  DependentUpdateTracker (int initialCapacity, int capacityIncrement) {

    dependents = new Vector (initialCapacity, capacityIncrement);
    needsUpdateFlags = new Vector (initialCapacity, capacityIncrement);
  }


  final boolean getNeedsUpdate (Object dependent, boolean needsUpdate) {

    if (needsUpdate) return true;
    int index = dependents.indexOf (dependent);
    if (index != -1) {
      return ((Boolean)needsUpdateFlags.get (index)).booleanValue();
    }
    return false;
  }


  final void add (Object dependent) {

    if (!dependents.contains (dependent)) {
      dependents.add (dependent);
      needsUpdateFlags.add (Boolean.TRUE);
    }
  }


  final void remove (Object dependent) {

    int index = dependents.indexOf (dependent);
    if (index != -1) {
      dependents.remove (index);
      needsUpdateFlags.remove (index);
    }
  }


  final void markAllNeedsUpdate() {

    for (int i = 0; i < needsUpdateFlags.size(); ++i) {
      needsUpdateFlags.set (i, Boolean.TRUE);
    }
  }


  final void clearNeedsUpdate (Object dependent) {

    int index = dependents.indexOf (dependent);
    if (index != -1) {
      needsUpdateFlags.set (index, Boolean.FALSE);
    }
  }


  final int size() {

    return dependents.size();
  }


  final Object getDependent (int index) {

    return dependents.get (index);
  }
}
