001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    /**
018     * @author Michael C. Han
019     */
020    public class PrimitiveIntList {
021    
022            public PrimitiveIntList() {
023                    _elements = new int[10];
024            }
025    
026            public PrimitiveIntList(int capacity) {
027                    _elements = new int[capacity];
028            }
029    
030            public void addAll(int[] values) {
031                    _checkCapacity(_elementsSize + values.length);
032    
033                    System.arraycopy(values, 0, _elements, _elementsSize, values.length);
034    
035                    _elementsSize += values.length;
036            }
037    
038            public void add(int value) {
039                    _checkCapacity(_elementsSize + 1);
040    
041                    _elements[_elementsSize++] = value;
042            }
043    
044            public int[] getArray() {
045                    trim();
046    
047                    return _elements;
048            }
049    
050            public int size() {
051                    return _elementsSize;
052            }
053    
054            private void trim() {
055                    int oldSize = _elements.length;
056    
057                    if (_elementsSize < oldSize) {
058                            int[] previousElements = _elements;
059    
060                            _elements = new int[_elementsSize];
061    
062                            System.arraycopy(previousElements, 0, _elements, 0, _elementsSize);
063                    }
064            }
065    
066            private void _checkCapacity(int minSize) {
067                    int oldSize = _elements.length;
068    
069                    if (minSize > oldSize) {
070                            int[] previousElements = _elements;
071    
072                            int newCapacity = (oldSize * 3) / 2 + 1;
073    
074                            if (newCapacity < minSize) {
075                                    newCapacity = minSize;
076                            }
077    
078                            _elements = new int[newCapacity];
079    
080                            System.arraycopy(previousElements, 0, _elements, 0, _elementsSize);
081                    }
082            }
083    
084            private int[] _elements;
085            private int _elementsSize;
086    
087    }