<div>>I have no opinion of the usability of this proposal for Maps or Sets (it could make sense, but it could make none).</div><div>>But I am very sure, that for Lists it is very useful.</div><div><br></div><div>You can create a constant-sized mutable List like this:</div>
<div><br></div><div>public static List<T> constantSizeList(T[] array) {</div><div>  return Arrays.asList(array);</div><div>}</div><div><br></div><div>Thus what you describe as the most useful case is already covered.</div>
<div><br></div><div>Regards,</div><div>Dimitris</div><div><br><div class="gmail_quote">2010/4/17 assembling signals <span dir="ltr"><<a href="mailto:assembling.signals@yandex.ru">assembling.signals@yandex.ru</a>></span><br>
<blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
Hello, everyone!<br><br>My next proposal for the "Collections" utility class is a bunch of methods (similar to unmodifiable[...])<br>which would return not an unmodifiable List|Map|Set, but a constant-sized one.<br>
That is, all methods which get|set|... elements would work as earlier, but methods which<br>would alter the size of the underlying collection (add|put|remove|retain|...) would throw UnsupportedOperationException.<br><br>So the new logic hoerarchy would be:<br>
1) Unmodifiable[something] -- very strict<br>2) ConstantSized[something] -- less strict<br>3) [Usual collections]<br><br>A constant-sized-list would be nearly same as an unmodifiable-list, with the exception that set(int index)<br>
would be allowed.<br>A constant-sized-map would be nearly same as an unmodifiable-map, with the exception that put(K key, V value) on EXISTING<br>keys would be allowed.<br><br>I have no opinion of the usability of this proposal for Maps or Sets (it could make sense, but it could make none).<br>
But I am very sure, that for Lists it is very useful. </blockquote><blockquote class="gmail_quote" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0.8ex; border-left-width: 1px; border-left-color: rgb(204, 204, 204); border-left-style: solid; padding-left: 1ex; ">
<br>What do you think of such a proposal? Not too specific? I, for myself, need such constant-sized collections often.<br><br>( I don't know about the name. ConstantSized[...]? NonResizable[...]? Sealed[...]? )<br><br>
Please take a look at a proposal for a ConstantSizedList:<br><br>import java.util.*;<br><br>/**<br> * Wrapps around an ArrayList and prevents all resizing methods by throwing<br> * an UnsupportedOperationException. The underlying ArrayList still may be changed.<br>
 *<br> * The underlying ArrayList is sealed by following means:<br> * - methods which return an iterator use Collections.unmodifiableList(...);<br> * - adding, clearing, removing, retaining methods throw UnsupportedOperationException;<br>
 * - all other methods are delegated to the underlying ArrayList.<br> */<br>public final class ConstantSizeArrayList<T><br>               implements List<T>, RandomAccess, Cloneable {<br><br>       private final static RuntimeException ex =<br>
                       new UnsupportedOperationException(ConstantSizeArrayList.class<br>                       + " doesn't allow methods which resize the underlying collection");<br><br>       private final ArrayList<T> underlying;<br>
<br>       public ConstantSizeArrayList(ArrayList<T> underlying) {<br>               this.underlying = underlying;<br>               underlying.trimToSize();<br>       }<br><br>       @Override<br>       public String toString() {<br>
               return underlying.toString();<br>       }<br><br>       @Override<br>       public boolean containsAll(Collection<?> c) {<br>               return underlying.containsAll(c);<br>       }<br><br>       @Override<br>
       public int hashCode() {<br>               return underlying.hashCode();<br>       }<br><br>       @Override<br>       public boolean equals(Object o) {<br>               return underlying.equals(o);<br>       }<br>
<br>       @Override<br>       public <T> T[] toArray(T[] a) {<br>               return underlying.toArray(a);<br>       }<br><br>       @Override<br>       public Object[] toArray() {<br>               return underlying.toArray();<br>
       }<br><br>       @Override<br>       public List<T> subList(int fromIndex, int toIndex) {<br>               return underlying.subList(fromIndex, toIndex);<br>       }<br><br>       @Override<br>       public int size() {<br>
               return underlying.size();<br>       }<br><br>       @Override<br>       public T set(int index, T element) {<br>               return underlying.set(index, element);<br>       }<br><br>       @Override<br>       public Iterator<T> iterator() {<br>
               return Collections.unmodifiableList(underlying).iterator();<br>       }<br><br>       @Override<br>       public ListIterator<T> listIterator() {<br>               return Collections.unmodifiableList(underlying).listIterator();<br>
       }<br><br>       @Override<br>       public ListIterator<T> listIterator(int index) {<br>               return Collections.unmodifiableList(underlying).listIterator(index);<br>       }<br><br>       @Override<br>
       public int lastIndexOf(Object o) {<br>               return underlying.lastIndexOf(o);<br>       }<br><br>       @Override<br>       public boolean isEmpty() {<br>               return underlying.isEmpty();<br>       }<br>
<br>       @Override<br>       public int indexOf(Object o) {<br>               return underlying.indexOf(o);<br>       }<br><br>       @Override<br>       public T get(int index) {<br>               return underlying.get(index);<br>
       }<br><br>       @Override<br>       @SuppressWarnings("element-type-mismatch")<br>       public boolean contains(Object o) {<br>               return underlying.contains(o);<br>       }<br><br>       // TODO delegating clone: is that OK?<br>
       @Override<br>       public Object clone() {<br>               return underlying.clone();<br>       }<br><br>       @Override<br>       public void clear() {<br>               throw ex;<br>       }<br><br>       @Override<br>
       public boolean addAll(int index, Collection<? extends T> c) {<br>               throw ex;<br>       }<br><br>       @Override<br>       public boolean addAll(Collection<? extends T> c) {<br>               throw ex;<br>
       }<br><br>       @Override<br>       public void add(int index, T element) {<br>               throw ex;<br>       }<br><br>       @Override<br>       public boolean add(T e) {<br>               throw ex;<br>       }<br>
<br>       @Override<br>       public boolean retainAll(Collection<?> c) {<br>               throw ex;<br>       }<br><br>       @Override<br>       public boolean removeAll(Collection<?> c) {<br>               throw ex;<br>
       }<br><br>       @Override<br>       public boolean remove(Object o) {<br>               throw ex;<br>       }<br><br>       @Override<br>       public T remove(int index) {<br>               throw ex;<br>       }<br>
<br>}<br><br><br><br>Best regards,<br>Ivan G Shevchenko<br></blockquote><div><br></div></div></div>