IntList.java

  1. /*
  2.  * Copyright (C) 2008, Google Inc.
  3.  * Copyright (C) 2009, Johannes Schindelin <johannes.schindelin@gmx.de> 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. /**
  13.  * A more efficient List&lt;Integer&gt; using a primitive integer array.
  14.  */
  15. public class IntList {
  16.     private int[] entries;

  17.     private int count;

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

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

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

  41.     /**
  42.      * Check if an entry appears in this collection.
  43.      *
  44.      * @param value
  45.      *            the value to search for.
  46.      * @return true of {@code value} appears in this list.
  47.      * @since 4.9
  48.      */
  49.     public boolean contains(int value) {
  50.         for (int i = 0; i < count; i++)
  51.             if (entries[i] == value)
  52.                 return true;
  53.         return false;
  54.     }

  55.     /**
  56.      * Get the value at the specified index
  57.      *
  58.      * @param i
  59.      *            index to read, must be in the range [0, {@link #size()}).
  60.      * @return the number at the specified index
  61.      * @throws java.lang.ArrayIndexOutOfBoundsException
  62.      *             the index outside the valid range
  63.      */
  64.     public int get(int i) {
  65.         if (count <= i)
  66.             throw new ArrayIndexOutOfBoundsException(i);
  67.         return entries[i];
  68.     }

  69.     /**
  70.      * Empty 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(int 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, int 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, int val) {
  113.         while (count < toIndex)
  114.             add(val);
  115.     }

  116.     private void grow() {
  117.         final int[] n = new int[(entries.length + 16) * 3 / 2];
  118.         System.arraycopy(entries, 0, n, 0, count);
  119.         entries = n;
  120.     }

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