001    /**
002     * Copyright (c) 2000-2013 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 PrimitiveLongList {
021    
022            public PrimitiveLongList() {
023                    _elements = new long[10];
024            }
025    
026            public PrimitiveLongList(int capacity) {
027                    _elements = new long[capacity];
028            }
029    
030            public void add(long value) {
031                    _checkCapacity(_elementsSize + 1);
032    
033                    _elements[_elementsSize++] = value;
034            }
035    
036            public void addAll(long[] values) {
037                    _checkCapacity(_elementsSize + values.length);
038    
039                    System.arraycopy(values, 0, _elements, _elementsSize, values.length);
040    
041                    _elementsSize += values.length;
042            }
043    
044            public long[] getArray() {
045                    _trim();
046    
047                    return _elements;
048            }
049    
050            public int size() {
051                    return _elementsSize;
052            }
053    
054            private void _checkCapacity(int minSize) {
055                    int oldSize = _elements.length;
056    
057                    if (minSize > oldSize) {
058                            long[] previousElements = _elements;
059    
060                            int newCapacity = (oldSize * 3) / 2 + 1;
061    
062                            if (newCapacity < minSize) {
063                                    newCapacity = minSize;
064                            }
065    
066                            _elements = new long[newCapacity];
067    
068                            System.arraycopy(previousElements, 0, _elements, 0, _elementsSize);
069                    }
070            }
071    
072            private void _trim() {
073                    int oldSize = _elements.length;
074    
075                    if (_elementsSize < oldSize) {
076                            long[] previousElements = _elements;
077    
078                            _elements = new long[_elementsSize];
079    
080                            System.arraycopy(previousElements, 0, _elements, 0, _elementsSize);
081                    }
082            }
083    
084            private long[] _elements;
085            private int _elementsSize;
086    
087    }