LongList.java

  1. /*
  2.  * Copyright (C) 2009, Christian Halstrick <christian.halstrick@sap.com>
  3.  * Copyright (C) 2009, Google Inc. and others
  4.  *
  5.  * This program and the accompanying materials are made available under the
  6.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  7.  * https://www.eclipse.org/org/documents/edl-v10.php.
  8.  *
  9.  * SPDX-License-Identifier: BSD-3-Clause
  10.  */

  11. package org.eclipse.jgit.util;

  12. import java.util.Arrays;

  13. /**
  14.  * A more efficient List&lt;Long&gt; using a primitive long array.
  15.  */
  16. public class LongList {
  17.     private long[] entries;

  18.     private int count;

  19.     /**
  20.      * Create an empty list with a default capacity.
  21.      */
  22.     public LongList() {
  23.         this(10);
  24.     }

  25.     /**
  26.      * Create an empty list with the specified capacity.
  27.      *
  28.      * @param capacity
  29.      *            number of entries the list can initially hold.
  30.      */
  31.     public LongList(int capacity) {
  32.         entries = new long[capacity];
  33.     }

  34.     /**
  35.      * Get number of entries in this list
  36.      *
  37.      * @return number of entries in this list
  38.      */
  39.     public int size() {
  40.         return count;
  41.     }

  42.     /**
  43.      * Get the value at the specified index
  44.      *
  45.      * @param i
  46.      *            index to read, must be in the range [0, {@link #size()}).
  47.      * @return the number at the specified index
  48.      * @throws java.lang.ArrayIndexOutOfBoundsException
  49.      *             the index outside the valid range
  50.      */
  51.     public long get(int i) {
  52.         if (count <= i)
  53.             throw new ArrayIndexOutOfBoundsException(i);
  54.         return entries[i];
  55.     }

  56.     /**
  57.      * Determine if an entry appears in this collection.
  58.      *
  59.      * @param value
  60.      *            the value to search for.
  61.      * @return true of {@code value} appears in this list.
  62.      */
  63.     public boolean contains(long value) {
  64.         for (int i = 0; i < count; i++)
  65.             if (entries[i] == value)
  66.                 return true;
  67.         return false;
  68.     }

  69.     /**
  70.      * Clear this list
  71.      */
  72.     public void clear() {
  73.         count = 0;
  74.     }

  75.     /**
  76.      * Add an entry to the end of the list.
  77.      *
  78.      * @param n
  79.      *            the number to add.
  80.      */
  81.     public void add(long n) {
  82.         if (count == entries.length)
  83.             grow();
  84.         entries[count++] = n;
  85.     }

  86.     /**
  87.      * Assign an entry in the list.
  88.      *
  89.      * @param index
  90.      *            index to set, must be in the range [0, {@link #size()}).
  91.      * @param n
  92.      *            value to store at the position.
  93.      */
  94.     public void set(int index, long n) {
  95.         if (count < index)
  96.             throw new ArrayIndexOutOfBoundsException(index);
  97.         else if (count == index)
  98.             add(n);
  99.         else
  100.             entries[index] = n;
  101.     }

  102.     /**
  103.      * Pad the list with entries.
  104.      *
  105.      * @param toIndex
  106.      *            index position to stop filling at. 0 inserts no filler. 1
  107.      *            ensures the list has a size of 1, adding <code>val</code> if
  108.      *            the list is currently empty.
  109.      * @param val
  110.      *            value to insert into padded positions.
  111.      */
  112.     public void fillTo(int toIndex, long val) {
  113.         while (count < toIndex)
  114.             add(val);
  115.     }

  116.     /**
  117.      * Sort the list of longs according to their natural ordering.
  118.      */
  119.     public void sort() {
  120.         Arrays.sort(entries, 0, count);
  121.     }

  122.     private void grow() {
  123.         final long[] n = new long[(entries.length + 16) * 3 / 2];
  124.         System.arraycopy(entries, 0, n, 0, count);
  125.         entries = n;
  126.     }

  127.     /** {@inheritDoc} */
  128.     @Override
  129.     public String toString() {
  130.         final StringBuilder r = new StringBuilder();
  131.         r.append('[');
  132.         for (int i = 0; i < count; i++) {
  133.             if (i > 0)
  134.                 r.append(", "); //$NON-NLS-1$
  135.             r.append(entries[i]);
  136.         }
  137.         r.append(']');
  138.         return r.toString();
  139.     }
  140. }