001/** 002 * Copyright 2014 Tampere University of Technology, Pori Department 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package core.tut.pori.datatypes; 017 018import java.util.ArrayList; 019import java.util.Iterator; 020 021/** 022 * class for storing primitive integers 023 * 024 */ 025public final class IntPrimitiveList { 026 private static final int ARRAY_SIZE = 50; 027 private ArrayList<int[]> _arrays = null; 028 private int[] _currentArray = null; 029 private int _currentIndex = ARRAY_SIZE; // set to maximum so that new array is created on first call 030 private int _valueCount = 0; 031 032 /** 033 * 034 * @param value 035 */ 036 public void add(int value){ 037 if(_currentIndex >= ARRAY_SIZE){ 038 _currentArray = new int[ARRAY_SIZE]; 039 _currentIndex = 0; 040 _arrays.add(_currentArray); 041 } 042 _currentArray[_currentIndex++] = value; 043 ++_valueCount; 044 } 045 046 /** 047 * 048 */ 049 public IntPrimitiveList(){ 050 _arrays = new ArrayList<>(); 051 } 052 053 /** 054 * 055 * @return the list as an array 056 */ 057 public int[] toArray(){ 058 if(_arrays.isEmpty()){ 059 return null; 060 } 061 int[] array = new int[_valueCount]; 062 int index = 0; 063 for(Iterator<int[]> iter = _arrays.iterator();iter.hasNext();){ 064 int[] temp = iter.next(); 065 for(int i=0;i<temp.length && index < _valueCount;++i){ 066 array[index++] = temp[i]; 067 } 068 } 069 return array; 070 } 071}