View Javadoc

1   /*
2    * Copyright (C) 2004 TiongHiang Lee
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not,  write to the Free Software
16   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   *
18   * Email: thlee@onemindsoft.org
19   */
20  
21  package org.onemind.commons.java.datastructure;
22  
23  /***
24   * Represents a list of long
25   * @author TiongHiang Lee (thlee@onemindsoft.org)
26   * @version $Id: LongList.java,v 1.2 2004/08/26 12:33:16 thlee Exp $ $Name:  $
27   */
28  public class LongList
29  {
30  
31      /*** the initial capacity * */
32      private static final int INITIAL_CAPACITY = 10;
33  
34      /*** the growth rate * */
35      private static final int GROW = 10;
36  
37      /*** the count of longs in the list * */
38      private int _count;
39  
40      /*** the list * */
41      private long[] _list = new long[INITIAL_CAPACITY];
42  
43      /***
44       * {@inheritDoc}
45       */
46      public LongList()
47      {
48          this(INITIAL_CAPACITY);
49      }
50  
51      /***
52       * {@inheritDoc}
53       * @param capacity initial capacity
54       */
55      public LongList(int capacity)
56      {
57          _list = new long[INITIAL_CAPACITY];
58      }
59  
60      /***
61       * Add a long to the list
62       * @param l the long
63       */
64      public void add(long l)
65      {
66          ensureCapacity(_count + 1);
67          _list[_count] = l;
68          _count++;
69      }
70  
71      /***
72       * Get the long on index i in the list
73       * @param i the index
74       * @return the long
75       */
76      public long get(int i)
77      {
78          if ((i < 0) || (i >= _count))
79          {
80              throw new IndexOutOfBoundsException("Invalid index " + i);
81          } else
82          {
83              return _list[i];
84          }
85      }
86  
87      /***
88       * Add a long at index i
89       * @param l the long
90       * @param i the index
91       */
92      public void add(long l, int i)
93      {
94          if ((i < 0) || (i > _count))
95          {
96              throw new IndexOutOfBoundsException("Invalid index " + i);
97          } else if (i == _count)
98          {
99              add(l);
100         } else
101         {
102             ensureCapacity(_count + 1);
103             for (int j = _count; j > i; j--)
104             {
105                 _list[j] = _list[j - 1];
106             }
107             _count++;
108             _list[i] = l;
109         }
110     }
111 
112     /***
113      * ensure the capacity of the long
114      * @param size the size
115      */
116     private void ensureCapacity(int size)
117     {
118         if (_list.length < size)
119         {
120             long[] newlist = new long[_list.length + GROW];
121             for (int i = 0; i < _list.length; i++)
122             {
123                 newlist[i] = _list[i];
124             }
125             _list = newlist;
126         }
127     }
128 
129     /***
130      * Remove the long at index i
131      * @param i the index
132      * @return the long at index i
133      */
134     public long remove(int i)
135     {
136         if ((i < 0) || (i >= _count))
137         {
138             throw new IndexOutOfBoundsException("Invalid index " + i);
139         } else
140         {
141             long save = _list[i];
142             for (int j = i; j < (_count - 2); j--)
143             {
144                 _list[j] = _list[j + 1];
145             }
146             _count--;
147             return save;
148         }
149     }
150 
151     /***
152      * Return the first long in the list
153      * @return the first long
154      */
155     public long first()
156     {
157         return _list[0];
158     }
159 
160     /***
161      * Return the last long in the list
162      * @return the last long
163      */
164     public long last()
165     {
166         return _list[_count - 1];
167     }
168 }