/electric-8.09/com/sun/electric/database/text/ImmutableArrayList.java

# · Java · 109 lines · 49 code · 7 blank · 53 comment · 20 complexity · 17da483a7f58a01644e91779220ee427 MD5 · raw file

  1. /* -*- tab-width: 4 -*-
  2. *
  3. * Electric(tm) VLSI Design System
  4. *
  5. * File: ImmutableArrayList.java
  6. * Written by: Dmitry Nadezhin, Sun Microsystems.
  7. *
  8. * Copyright (c) 2005 Sun Microsystems and Static Free Software
  9. *
  10. * Electric(tm) is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * Electric(tm) is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with Electric(tm); see the file COPYING. If not, write to
  22. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  23. * Boston, Mass 02111-1307, USA.
  24. */
  25. package com.sun.electric.database.text;
  26. import java.util.ArrayList;
  27. import java.util.Collection;
  28. import java.util.ConcurrentModificationException;
  29. /**
  30. * Constant-array implementation of the <tt>List</tt> interface.
  31. */
  32. public class ImmutableArrayList<E> extends ArrayList<E> {
  33. /**
  34. * Constructs a list containing the elements of the specified
  35. * collection, in the order they are returned by the collection's
  36. * iterator. The <tt>ImmutableArrayList</tt> instance has capacity of
  37. * the size of the specified collection.
  38. *
  39. * @param c the collection whose elements are to be placed into this list.
  40. * @throws NullPointerException if the specified collection is null.
  41. */
  42. public ImmutableArrayList(Collection<? extends E> c) {
  43. super(c.size());
  44. super.addAll(c);
  45. if (size() != c.size()) throw new ConcurrentModificationException();
  46. }
  47. /**
  48. * Constructs a list containing the elements of the specified array.
  49. * The <tt>ImmutableArrayList</tt> instance has capacity of
  50. * the length of the specified array.
  51. *
  52. * @param a the array whose elements are to be placed into this list.
  53. * @throws NullPointerException if the specified array is null.
  54. */
  55. public ImmutableArrayList(E[] a) {
  56. super(a.length);
  57. for (E e: a)
  58. super.add(e);
  59. assert size() == a.length;
  60. }
  61. /**
  62. * Constructs a list containing the range of elements of the specified array.
  63. * The <tt>ImutableArrayList</tt> instance has capacity of the range length.
  64. *
  65. * @param a the array whose elements are to be placed into this list.
  66. * @param fromIndex
  67. * @param toIndex
  68. * @throws NullPointerException if the specified array is null.
  69. */
  70. public ImmutableArrayList(E[] a, int fromIndex, int toIndex) {
  71. super(toIndex - fromIndex);
  72. if (fromIndex < 0)
  73. throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
  74. if (toIndex > a.length)
  75. throw new IndexOutOfBoundsException("toIndex = " + toIndex);
  76. for (int i = fromIndex; i < toIndex; i++)
  77. super.add(a[i]);
  78. assert size() == toIndex - fromIndex;
  79. }
  80. public ImmutableArrayList<E> with(E[] a) {
  81. if (a == null) return this;
  82. int l;
  83. for (l = a.length; l > 0 && a[l - 1] == null; l--);
  84. if (l == size()) {
  85. int i = 0;
  86. while (i < size() && a[i] == get(i)) i++;
  87. if (i == l) return this;
  88. }
  89. return new ImmutableArrayList<E>(a, 0, l);
  90. }
  91. public void trimToSize() {}
  92. public void ensureCapacity(int minCapacity) {}
  93. public E set(int index, E element) { throw new UnsupportedOperationException(); }
  94. public boolean add(E o) { throw new UnsupportedOperationException(); }
  95. public void add(int index, E element) { throw new UnsupportedOperationException(); }
  96. public E remove(int index) { throw new UnsupportedOperationException(); }
  97. public boolean remove(Object o) { throw new UnsupportedOperationException(); }
  98. public void clear() { throw new UnsupportedOperationException(); }
  99. public boolean addAll(Collection<? extends E> c) { throw new UnsupportedOperationException(); }
  100. public boolean addAll(int index, Collection<? extends E> c) { throw new UnsupportedOperationException(); }
  101. protected void removeRange(int fromIndex, int toIndex) { throw new UnsupportedOperationException(); }
  102. }