1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  /**
18   * <a href="PrimitiveLongList.java.html"><b><i>View Source</i></b></a>
19   *
20   * @author Michael C. Han
21   */
22  public class PrimitiveLongList {
23  
24      public PrimitiveLongList() {
25          _elements = new long[10];
26      }
27  
28      public PrimitiveLongList(int capacity) {
29          _elements = new long[capacity];
30      }
31  
32      public void addAll(long[] values) {
33          _checkCapacity(_elementsSize + values.length);
34  
35          System.arraycopy(values, 0, _elements, _elementsSize, values.length);
36  
37          _elementsSize += values.length;
38      }
39  
40      public void add(long value) {
41          _checkCapacity(_elementsSize + 1);
42  
43          _elements[_elementsSize++] = value;
44      }
45  
46      public long[] getArray() {
47          trim();
48  
49          return _elements;
50      }
51  
52      public int size() {
53          return _elementsSize;
54      }
55  
56      private void trim() {
57          int oldSize = _elements.length;
58  
59          if (_elementsSize < oldSize) {
60              long[] previousElements = _elements;
61  
62              _elements = new long[_elementsSize];
63  
64              System.arraycopy(previousElements, 0, _elements, 0, _elementsSize);
65          }
66      }
67  
68      private void _checkCapacity(int minSize) {
69          int oldSize = _elements.length;
70  
71          if (minSize > oldSize) {
72              long[] previousElements = _elements;
73  
74              int newCapacity = (oldSize * 3) / 2 + 1;
75  
76              if (newCapacity < minSize) {
77                  newCapacity = minSize;
78              }
79  
80              _elements = new long[newCapacity];
81  
82              System.arraycopy(previousElements, 0, _elements, 0, _elementsSize);
83          }
84      }
85  
86      private long[] _elements;
87      private int _elementsSize;
88  
89  }