File: IntArrayList.java

package info (click to toggle)
libglazedlists-java 1.9.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,024 kB
  • ctags: 4,252
  • sloc: java: 22,561; xml: 818; sh: 51; makefile: 5
file content (131 lines) | stat: -rw-r--r-- 3,117 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* Glazed Lists                                                 (c) 2012       */
/* http://glazedlists.com/                                                     */
package ca.odell.glazedlists.impl.adt;

/**
 * Simple implementation of an array list suitable for storage of primitive integers.
 *
 * @author <a href="mailto:rob@starlight-systems.com">Rob Eden</a>
 */
public class IntArrayList {
	private int[] data;
	private int size = 0;


	/**
	 * Create a list with an initial capacity of ten.
	 */
	public IntArrayList() {
		this( 10 );
	}

	/**
	 * Create a list with the given initial capacity.
	 * 
	 * @param initial_capacity  The capacity to initially allow in the list without
	 *                          requiring additional array space to be allocated.   
	 */
	public IntArrayList( int initial_capacity ) {
		data = new int[ initial_capacity ];
	}


	/**
	 * Returns the number of entries in the list.
	 */
	public int size() {
		return size;
	}


	/**
	 * Indicates whether or not the list is empty.
	 */
	public boolean isEmpty() {
		return size == 0;
	}


	/**
	 * Clear the existing data in the list.
	 */
	public void clear() {
		size = 0;
	}


	/**
	 * Return the value at the given index. If the index is outside the current bounds
	 * of the list, an exception will be thrown.
	 *
	 * @param index         The index from which to get the value.
	 */
	public int get( int index ) {
		checkAccess( index );
		
		return data[ index ];
	}


	/**
	 * Add the given value to the end of the list.
	 */
	public void add( int value ) {
		checkGrow( 1 );
		
		data[ size ] = value;
		size++;
	}


	/**
	 * Set the value of the given index. If the index is outside the existing bounds of
	 * the list, an exception will be thrown.
	 *
	 * @param index         The index at which to set the value.
	 * @param value         The new value to be set.
	 */
	public void set( int index, int value ) {
		checkAccess( index );
		
		data[ index ] = value;
	}


	/**
	 * Determine if there is sufficient data in the list to make access to the given index
	 * make sense. If it is outside the current bounds of the list, an exception will
	 * be thrown.
	 *
	 * @param index         The index to be accessed.
	 *
	 * @throws IndexOutOfBoundsException    If the index is outside the bounds of the list.
	 */
	private void checkAccess( int index ) {
		if ( size <= index ) {
			throw new IndexOutOfBoundsException( "Index " + index +
				" is outside list bounds (size=" + size + ")" );
		}
	}


	/**
	 * Determines if the existing array has enough space left to add the given amount of
	 * data. If it doesn't, a new array of sufficient size will be created and existing
	 * data will be copied to it.
	 *
	 * @param amount        The amount of entries we would like to add to the list.
	 */
	private void checkGrow( int amount ) {
		if ( size + amount <= data.length ) return;
		
		int new_length = data.length * 2;
		while( new_length < ( size + amount ) ) {
			new_length = data.length * 2;
		}
		
		int[] new_data = new int[ new_length ];
		System.arraycopy( data, 0, new_data, 0, size );
		data = new_data;
	}
}