001/*-------------------------------------------------------------------------+ 002| | 003| Copyright 2005-2011 The ConQAT Project | 004| | 005| Licensed under the Apache License, Version 2.0 (the "License"); | 006| you may not use this file except in compliance with the License. | 007| You may obtain a copy of the License at | 008| | 009| http://www.apache.org/licenses/LICENSE-2.0 | 010| | 011| Unless required by applicable law or agreed to in writing, software | 012| distributed under the License is distributed on an "AS IS" BASIS, | 013| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 014| See the License for the specific language governing permissions and | 015| limitations under the License. | 016+-------------------------------------------------------------------------*/ 017package org.conqat.lib.commons.collections; 018 019import java.util.ArrayList; 020import java.util.List; 021 022/** 023 * A really simple list for storing ints. This exists as it is both more 024 * efficient and uses way less memory than a List of Integers. 025 */ 026public class IntList extends ManagedIntArray { 027 028 private static final long serialVersionUID = 1L; 029 030 /** Returns the size of the list. */ 031 public int getSize() { 032 return size; 033 } 034 035 /** 036 * Returns the element at the given index. No range checking is performed, thus 037 * you might sometimes get an {@link ArrayIndexOutOfBoundsException}, and 038 * sometimes just 0 returned, depending on if you reached existing memory by 039 * chance. 040 */ 041 public int get(int index) { 042 return array[index]; 043 } 044 045 /** 046 * Set the element at the given index. No range checking is performed, thus you 047 * might sometimes get an {@link ArrayIndexOutOfBoundsException}, and sometimes 048 * not for illegal indexes, depending on whether you hit memory allocated by the 049 * exponential growth strategy by chance. 050 */ 051 public void set(int index, int value) { 052 array[index] = value; 053 } 054 055 /** Adds an element to the end of the list. */ 056 public void add(int value) { 057 int index = size; 058 addArrayElement(); 059 array[index] = value; 060 } 061 062 /** Adds all elements of the given list to the end of the list. */ 063 public void addAll(IntList values) { 064 for (int i = 0; i < values.getSize(); i++) { 065 add(values.get(i)); 066 } 067 } 068 069 /** Returns the int list as an actual list. This is only used for testing. */ 070 public List<Integer> getAsList() { 071 List<Integer> result = new ArrayList<>(); 072 for (int i = 0; i < getSize(); i++) { 073 result.add(get(i)); 074 } 075 return result; 076 } 077}