A list of objects that can grow as required. : List « Date Type « Android






A list of objects that can grow as required.

   
package app.test;

/* 
 * AFreeChart : a free chart library for Android(tm) platform.
 *              (based on JFreeChart and JCommon)
 * 
 *
 * (C) Copyright 2010, by Icom Systech Co., Ltd.
 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
 * 
 * Project Info:
 *    AFreeChart: http://code.google.com/p/afreechart/
 *    JFreeChart: http://www.jfree.org/jfreechart/index.html
 *    JCommon   : http://www.jfree.org/jcommon/index.html
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * [Android is a trademark of Google Inc.]
 * 
 * ---------------
 * ObjectList.java
 * ---------------
 * 
 * (C) Copyright 2010, by Icom Systech Co., Ltd.
 *
 * Original Author:  shiraki  (for Icom Systech Co., Ltd);
 * Contributor(s):   Sato Yoshiaki ;
 *                   Niwano Masayoshi;
 *
 * Changes (from 19-Nov-2010)
 * --------------------------
 * 19-Nov-2010 : port JCommon 1.0.16 to Android as "AFreeChart"
 * 
 * ------------- JFreeChart ---------------------------------------------
 * (C) Copyright 2003, 2004, by Object Refinery Limited and Contributors.
 *
 * Original Author:  David Gilbert (for Object Refinery Limited);
 * Contributor(s):   -;
 *
 *
 * Changes
 * -------
 * 17-Jul-2003 : Version 1 (DG);
 * 13-Aug-2003 : Refactored to extend AbstractObjectList (DG);
 * 21-Oct-2004 : removed duplicate interface declarations and empty methods.
 * 22-Oct-2004 : Restored removed methods - see note in code (DG);
 * 
 */

import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

import android.util.Log;

/**
 * A list of objects that can grow as required.
 * <p>
 * When cloning, the objects in the list are NOT cloned, only the references.
 * 
 * @author Thomas Morgner
 */
public class ObjectList extends AbstractObjectList {

  /**
     * 
     */
  private static final long serialVersionUID = 2683811442645658830L;

  /**
   * Default constructor.
   */
  public ObjectList() {
  }

  /**
   * Creates a new list.
   * 
   * @param initialCapacity
   *            the initial capacity.
   */
  public ObjectList(final int initialCapacity) {
    super(initialCapacity);
  }

  // NOTE: the methods below look redundant, but their purpose is to provide
  // public
  // access to the the get(), set() and indexOf() methods defined in the
  // AbstractObjectList class, for this class only. For other classes
  // (e.g. PaintList, ShapeList etc) we don't want the Object versions of
  // these
  // methods to be visible in the public API.

  /**
   * Returns the object at the specified index, if there is one, or
   * <code>null</code>.
   * 
   * @param index
   *            the object index.
   * 
   * @return The object or <code>null</code>.
   */
  public Object get(final int index) {
    return super.get(index);
  }

  /**
   * Sets an object reference (overwriting any existing object).
   * 
   * @param index
   *            the object index.
   * @param object
   *            the object (<code>null</code> permitted).
   */
  public void set(final int index, final Object object) {
    super.set(index, object);
  }

  /**
   * Returns the index of the specified object, or -1 if the object is not in
   * the list.
   * 
   * @param object
   *            the object.
   * 
   * @return The index or -1.
   */
  public int indexOf(final Object object) {
    return super.indexOf(object);
  }

}

/*
 * 
 * AFreeChart : a free chart library for Android(tm) platform. (based on
 * JFreeChart and JCommon)
 * 
 * 
 * (C) Copyright 2010, by Icom Systech Co., Ltd. (C) Copyright 2000-2005, by
 * Object Refinery Limited and Contributors.
 * 
 * Project Info: AFreeChart: http://code.google.com/p/afreechart/ JFreeChart:
 * http://www.jfree.org/jfreechart/index.html JCommon :
 * http://www.jfree.org/jcommon/index.html
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program 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 General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * [Android is a trademark of Google Inc.]
 * 
 * ----------------------- AbstractObjectList.java -----------------------
 * 
 * (C) Copyright 2010, by Icom Systech Co., Ltd.
 * 
 * Original Author: shiraki (for Icom Systech Co., Ltd); Contributor(s): Sato
 * Yoshiaki ; Niwano Masayoshi;
 * 
 * Changes (from 19-Nov-2010) -------------------------- 19-Nov-2010 : port
 * JCommon 1.0.16 to Android as "AFreeChart"
 * 
 * ------------- JFreeChart --------------------------------------------- (C)
 * Copyright 2003, 2004, by Object Refinery Limited and Contributors.
 * 
 * Original Author: David Gilbert (for Object Refinery Limited); Contributor(s):
 * Bill Kelemen; Nicolas Brodu
 * 
 * 
 * Changes ------- 13-Aug-2003 : Version 1, based on ObjectList (DG);
 * 24-Aug-2003 : Fixed size (BK); 15-Sep-2003 : Fix serialization for subclasses
 * (ShapeList, PaintList) (NB);
 */

/**
 * A list of objects that can grow as required.
 * 
 * @author David Gilbert
 */
class AbstractObjectList implements Cloneable, Serializable {

  /** For serialization. */
  private static final long serialVersionUID = 7789833772597351595L;

  /** The default initial capacity of the list. */
  public static final int DEFAULT_INITIAL_CAPACITY = 8;

  /** Storage for the objects. */
  private transient Object[] objects;

  /** The current list size. */
  private int size = 0;

  /** The default increment. */
  private int increment = DEFAULT_INITIAL_CAPACITY;

  /**
   * Creates a new list with the default initial capacity.
   */
  protected AbstractObjectList() {
    this(DEFAULT_INITIAL_CAPACITY);
  }

  /**
   * Creates a new list.
   * 
   * @param initialCapacity
   *            the initial capacity.
   */
  protected AbstractObjectList(final int initialCapacity) {
    this(initialCapacity, initialCapacity);
  }

  /**
   * Creates a new list.
   * 
   * @param initialCapacity
   *            the initial capacity.
   * @param increment
   *            the increment.
   */
  protected AbstractObjectList(final int initialCapacity, final int increment) {
    this.objects = new Object[initialCapacity];
    this.increment = increment;
  }

  /**
   * Returns the object at the specified index, if there is one, or
   * <code>null</code>.
   * 
   * @param index
   *            the object index.
   * 
   * @return The object or <code>null</code>.
   */
  protected Object get(final int index) {
    Object result = null;
    if (index >= 0 && index < this.size) {
      result = this.objects[index];
    }
    return result;
  }

  /**
   * Sets an object reference (overwriting any existing object).
   * 
   * @param index
   *            the object index.
   * @param object
   *            the object (<code>null</code> permitted).
   */
  protected void set(final int index, final Object object) {
    if (index < 0) {
      throw new IllegalArgumentException("Requires index >= 0.");
    }
    if (index >= this.objects.length) {
      final Object[] enlarged = new Object[index + this.increment];
      System.arraycopy(this.objects, 0, enlarged, 0, this.objects.length);
      this.objects = enlarged;
    }
    this.objects[index] = object;
    this.size = Math.max(this.size, index + 1);
  }

  /**
   * Clears the list.
   */
  public void clear() {
    Arrays.fill(this.objects, null);
    this.size = 0;
  }

  /**
   * Returns the size of the list.
   * 
   * @return The size of the list.
   */
  public int size() {
    return this.size;
  }

  /**
   * Returns the index of the specified object, or -1 if the object is not in
   * the list.
   * 
   * @param object
   *            the object.
   * 
   * @return The index or -1.
   */
  protected int indexOf(final Object object) {
    for (int index = 0; index < this.size; index++) {
      if (this.objects[index] == object) {
        return (index);
      }
    }
    return -1;
  }

  /**
   * Tests this list for equality with another object.
   * 
   * @param obj
   *            the object to test.
   * 
   * @return A boolean.
   */
  public boolean equals(final Object obj) {

    if (obj == null) {
      return false;
    }

    if (obj == this) {
      return true;
    }

    if (!(obj instanceof AbstractObjectList)) {
      return false;
    }

    final AbstractObjectList other = (AbstractObjectList) obj;
    final int listSize = size();
    for (int i = 0; i < listSize; i++) {
      if (!ObjectUtilities.equal(get(i), other.get(i))) {
        return false;
      }
    }
    return true;
  }

  /**
   * Returns a hash code value for the object.
   * 
   * @return the hashcode
   */
  public int hashCode() {
    return super.hashCode();
  }

  /**
   * Clones the list of objects. The objects in the list are not cloned, so
   * this is method makes a 'shallow' copy of the list.
   * 
   * @return A clone.
   * 
   * @throws CloneNotSupportedException
   *             if an item in the list does not support cloning.
   */
  public Object clone() throws CloneNotSupportedException {

    final AbstractObjectList clone = (AbstractObjectList) super.clone();
    if (this.objects != null) {
      clone.objects = new Object[this.objects.length];
      System.arraycopy(this.objects, 0, clone.objects, 0,
          this.objects.length);
    }
    return clone;

  }

}

/**
 * A collection of useful static utility methods for handling classes and object
 * instantiation.
 * 
 * @author Thomas Morgner
 */
final class ObjectUtilities {

  /**
   * Returns <code>true</code> if the two objects are equal OR both
   * <code>null</code>.
   * 
   * @param o1
   *            object 1 (<code>null</code> permitted).
   * @param o2
   *            object 2 (<code>null</code> permitted).
   * @return <code>true</code> or <code>false</code>.
   */
  public static boolean equal(final Object o1, final Object o2) {
    if (o1 == o2) {
      return true;
    }
    if (o1 != null) {
      return o1.equals(o2);
    } else {
      return false;
    }
  }

  /**
   * Returns a hash code for an object, or zero if the object is
   * <code>null</code>.
   * 
   * @param object
   *            the object (<code>null</code> permitted).
   * @return The object's hash code (or zero if the object is
   *         <code>null</code>).
   */
  public static int hashCode(final Object object) {
    int result = 0;
    if (object != null) {
      result = object.hashCode();
    }
    return result;
  }

  /**
   * Returns a clone of the specified object, if it can be cloned, otherwise
   * throws a CloneNotSupportedException.
   * 
   * @param object
   *            the object to clone (<code>null</code> not permitted).
   * @return A clone of the specified object.
   * @throws CloneNotSupportedException
   *             if the object cannot be cloned.
   */
  public static Object clone(final Object object)
      throws CloneNotSupportedException {
    if (object == null) {
      throw new IllegalArgumentException("Null 'object' argument.");
    }
    if (object instanceof PublicCloneable) {
      final PublicCloneable pc = (PublicCloneable) object;
      return pc.clone();
    } else {
      try {
        final Method method = object.getClass().getMethod("clone",
            (Class[]) null);
        if (Modifier.isPublic(method.getModifiers())) {
          return method.invoke(object, (Object[]) null);
        }
      } catch (NoSuchMethodException e) {
        Log.w("ObjectUtilities",
            "Object without clone() method is impossible.");
      } catch (IllegalAccessException e) {
        Log.w("ObjectUtilities",
            "Object.clone(): unable to call method.");
      } catch (InvocationTargetException e) {
        Log.w("ObjectUtilities",
            "Object without clone() method is impossible.");
      }
    }
    throw new CloneNotSupportedException("Failed to clone.");
  }

  /**
   * Returns a new collection containing clones of all the items in the
   * specified collection.
   * 
   * @param collection
   *            the collection (<code>null</code> not permitted).
   * @return A new collection containing clones of all the items in the
   *         specified collection.
   * @throws CloneNotSupportedException
   *             if any of the items in the collection cannot be cloned.
   */
  public static Collection deepClone(final Collection collection)
      throws CloneNotSupportedException {

    if (collection == null) {
      throw new IllegalArgumentException("Null 'collection' argument.");
    }
    // all JDK-Collections are cloneable ...
    // and if the collection is not clonable, then we should throw
    // a CloneNotSupportedException anyway ...
    final Collection result = (Collection) ObjectUtilities
        .clone(collection);
    result.clear();
    final Iterator iterator = collection.iterator();
    while (iterator.hasNext()) {
      final Object item = iterator.next();
      if (item != null) {
        result.add(clone(item));
      } else {
        result.add(null);
      }
    }
    return result;
  }

}

/**
 * An interface that exposes the clone() method.
 * @author David Gilbert
 */
interface PublicCloneable extends Cloneable {
    
    /**
     * Returns a clone of the object.
     * 
     * @return A clone.
     * 
     * @throws CloneNotSupportedException if cloning is not supported for some reason.
     */
    public Object clone() throws CloneNotSupportedException;

}

   
    
    
  








Related examples in the same category

1.Fixed Array List
2.Convert varargv... to List
3.A list of Boolean objects.