Replace inefficient new Long(long) constructor to silence FindBugs
[egit/imyousuf.git] / org.spearce.jgit / src / org / spearce / jgit / revwalk / RevObjectList.java
blobb95b1275bd9fa237848d21c23d02a4b28a43a20d
1 /*
2 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
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.revwalk;
40 import java.util.AbstractList;
42 /**
43 * An ordered list of {@link RevObject} subclasses.
45 * @param <E>
46 * type of subclass of RevObject the list is storing.
48 public class RevObjectList<E extends RevObject> extends AbstractList<E> {
49 static final int BLOCK_SHIFT = 8;
51 static final int BLOCK_SIZE = 1 << BLOCK_SHIFT;
53 Block contents;
55 int size;
57 /** Create an empty object list. */
58 public RevObjectList() {
59 clear();
62 public void add(final int index, final E element) {
63 if (index != size)
64 throw new UnsupportedOperationException("Not add-at-end: " + index);
65 set(index, element);
66 size++;
69 public E set(int index, E element) {
70 Block s = contents;
71 while (index >> s.shift >= BLOCK_SIZE) {
72 s = new Block(s.shift + BLOCK_SHIFT);
73 s.contents[0] = contents;
74 contents = s;
76 while (s.shift > 0) {
77 final int i = index >> s.shift;
78 index -= i << s.shift;
79 if (s.contents[i] == null)
80 s.contents[i] = new Block(s.shift - BLOCK_SHIFT);
81 s = (Block) s.contents[i];
83 final Object old = s.contents[index];
84 s.contents[index] = element;
85 return (E) old;
88 public E get(int index) {
89 Block s = contents;
90 if (index >> s.shift >= 1024)
91 return null;
92 while (s != null && s.shift > 0) {
93 final int i = index >> s.shift;
94 index -= i << s.shift;
95 s = (Block) s.contents[i];
97 return s != null ? (E) s.contents[index] : null;
100 public int size() {
101 return size;
104 @Override
105 public void clear() {
106 contents = new Block(0);
107 size = 0;
110 static class Block {
111 final Object[] contents = new Object[BLOCK_SIZE];
113 final int shift;
115 Block(final int s) {
116 shift = s;