/*
 * $Id$
 *
 * Copyright (c) 2004 by Rodney Kinney
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License (LGPL) as published by the Free Software Foundation.
 *
 * 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, copies are available
 * at http://www.opensource.org.
 */
package VASSAL.build.module.map;

import VASSAL.counters.GamePiece;
import VASSAL.tools.ArrayUtils;

/** Stores {@link VASSAL.counters.GamePiece}s in a simple array */
public class SimplePieceCollection implements PieceCollection {
  private int capacity = 100, incr = 25;
  private GamePiece pieces[] = new GamePiece[capacity];
  private int pieceCount = 0;

  /**
   * Returns the index of a piece.  When painting the map, pieces
   * are drawn in order of index */
  public int indexOf(GamePiece p) {
    for (int i = 0; i < pieceCount; ++i)
      if (pieces[i] == p)
        return (i);
    return -1;
  }

  public boolean canMerge(GamePiece p1, GamePiece p2) {
    return true;
  }

  public void add(GamePiece p) {
    if (pieceCount >= capacity) {
      capacity += incr;
      GamePiece oldStack[] = pieces;
      pieces = new GamePiece[capacity];
      System.arraycopy(oldStack, 0, pieces, 0, pieceCount);
    }
    pieces[pieceCount++] = p;
  }

  public void clear() {
    pieceCount = 0;
  }

  public void remove(GamePiece p) {
    removePieceAt(indexOf(p));
  }

  public GamePiece[] getPieces() {
    return ArrayUtils.copyOf(pieces, pieceCount);
  }

  public GamePiece[] getAllPieces() {
    return getPieces();
  }

  private void removePieceAt(int gone) {
    if (gone >= 0) {
      for (int i = gone; i < pieceCount - 1; ++i)
        pieces[i] = pieces[i + 1];
      pieceCount--;
    }
  }

  public void reposition(GamePiece s, int pos) {
    int index = indexOf(s);
    if (index >= 0) {
      for (int i = index; i < pieceCount - 1; ++i) {
        pieces[i] = pieces[i + 1];
      }
      for (int i = pieceCount - 1; i > pos; --i) {
        pieces[i] = pieces[i - 1];
      }
      pieces[pos] = s;
    }
  }

  public void moveToBack(GamePiece p) {
    reposition(p, 0);
  }

  public void moveToFront(GamePiece p) {
    reposition(p, pieceCount - 1);
  }
}

@@ -717,65 +717,62 @@
 ort 
-VASSAL.counters.GamePiece;%0Aimport VASSAL.tools.ArrayUtils
+java.util.ArrayList;%0A%0Aimport VASSAL.counters.GamePiece
 ;%0A%0A/
@@ -915,49 +915,24 @@
 ate 
+f
 in
-t capacity = 100, incr = 25;%0A  private 
+al ArrayList%3C
 Game
@@ -940,73 +940,45 @@
 iece
+%3E
  pieces
-%5B%5D
  = new 
-GamePiece%5Bcapacity%5D;%0A  private int pieceCount = 0
+ArrayList%3CGamePiece%3E()
 ;%0A%0A 
@@ -1128,104 +1128,32 @@
     
-for (int i = 0; i %3C pieceCount; ++i)%0A      if (pieces%5Bi%5D == p)%0A        return (i);%0A    return -1
+return pieces.indexOf(p)
 ;%0A  
@@ -1274,233 +1274,21 @@
     
-if (pieceCount %3E= capacity) %7B%0A      capacity += incr;%0A      GamePiece oldStack%5B%5D = pieces;%0A      pieces = new GamePiece%5Bcapacity%5D;%0A      System.arraycopy(oldStack, 0, pieces, 0, pieceCount);%0A    %7D%0A    pieces%5BpieceCount++%5D = p
+pieces.add(p)
 ;%0A  
@@ -1327,17 +1327,17 @@
 iece
-Count = 0
+s.clear()
 ;%0A  
@@ -1461,44 +1461,51 @@
 urn 
-ArrayUtils.copyOf(pieces, pieceCount
+pieces.toArray(new GamePiece%5Bpieces.size()%5D
 );%0A 
@@ -1647,105 +1647,27 @@
     
-for (int i = gone; i %3C pieceCount - 1; ++i)%0A        pieces%5Bi%5D = pieces%5Bi + 1%5D;%0A      pieceCount--
+pieces.remove(gone)
 ;%0A  
@@ -1710,17 +1710,17 @@
 mePiece 
-s
+p
 , int po
@@ -1732,28 +1732,37 @@
     
+f
 in
-t index = 
+al int i = pieces.
 indexOf(
 s);%0A
@@ -1757,17 +1757,17 @@
 indexOf(
-s
+p
 );%0A    i
@@ -1770,20 +1770,16 @@
    if (i
-ndex
  %3E= 0) %7B
@@ -1789,213 +1789,50 @@
     
-for (int i = index; i %3C pieceCount - 1; ++i) %7B%0A        pieces%5Bi%5D = pieces%5Bi + 1%5D;%0A      %7D%0A      for (int i = pieceCount - 1; i %3E pos; --i) %7B%0A        pieces%5Bi%5D = pieces%5Bi - 1%5D;%0A      %7D%0A      pieces%5Bpos%5D = s
+pieces.remove(i);%0A      pieces.add(pos, p)
 ;%0A  
@@ -1956,38 +1956,109 @@
     
-reposition(p, pieceCount - 1);
+final int i = pieces.indexOf(p);%0A    if (i %3E= 0) %7B%0A      pieces.remove(p);%0A      pieces.add(p);%0A    %7D
 %0A  %7D

Replaced a lot of array-management code with an ArrayList.