Correctly use a long for the offsets within a generated pack
[egit/charleso.git] / org.spearce.jgit / src / org / spearce / jgit / util / IntList.java
blob0a84793465eb39ead0f84a70fcfac66d31ac28ae
1 /*
2 * Copyright (C) 2008, Google Inc.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.util;
40 /** A more efficient List<Integer> using a primitive integer array. */
41 public class IntList {
42 private int[] entries;
44 private int count;
46 /** Create an empty list with a default capacity. */
47 public IntList() {
48 this(10);
51 /**
52 * Create an empty list with the specified capacity.
54 * @param capacity
55 * number of entries the list can initially hold.
57 public IntList(final int capacity) {
58 entries = new int[capacity];
61 /** @return number of entries in this list */
62 public int size() {
63 return count;
66 /**
67 * @param i
68 * index to read, must be in the range [0, {@link #size()}).
69 * @return the number at the specified index
70 * @throws ArrayIndexOutOfBoundsException
71 * the index outside the valid range
73 public int get(final int i) {
74 if (count <= i)
75 throw new ArrayIndexOutOfBoundsException(i);
76 return entries[i];
79 /** Empty this list */
80 public void clear() {
81 count = 0;
84 /**
85 * Add an entry to the end of the list.
87 * @param n
88 * the number to add.
90 public void add(final int n) {
91 if (count == entries.length)
92 grow();
93 entries[count++] = n;
96 /**
97 * Pad the list with entries.
99 * @param toIndex
100 * index position to stop filling at. 0 inserts no filler. 1
101 * ensures the list has a size of 1, adding <code>val</code> if
102 * the list is currently empty.
103 * @param val
104 * value to insert into padded positions.
106 public void fillTo(int toIndex, final int val) {
107 while (count < toIndex)
108 add(val);
111 private void grow() {
112 final int[] n = new int[(entries.length + 16) * 3 / 2];
113 System.arraycopy(entries, 0, n, 0, count);
114 entries = n;
117 public String toString() {
118 final StringBuilder r = new StringBuilder();
119 r.append('[');
120 for (int i = 0; i < count; i++) {
121 if (i > 0)
122 r.append(", ");
123 r.append(entries[i]);
125 r.append(']');
126 return r.toString();