001
014
015 package com.liferay.portal.kernel.util;
016
017
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 }