title: Is this an approach to create a read-only List in Java?
body: <p>Thinking in java says:</p>
  
  <blockquote>
    <p>To create a read-only List from AbstractList, you must implements get() and size().</p>
  </blockquote>
  
  <p>It confused me, the code is below:</p>
  
  <pre><code> public class CountingIntegerList
  extends AbstractList&lt;Integer&gt; {
    private int size;
    public CountingIntegerList(int size) {
      this.size = size &lt; 0 ? 0 : size;
    }
    public Integer get(int index) {
      return Integer.valueOf(index);
    }
    public int size() { return size; }
    public static void main(String[] args) {
      List list = new CountingIntegerList(30);
    }
  }
  </code></pre>
  
  <p>Is list a read-only List? Why?</p>
  
  <p>Ok, the answer is yes since I extend <code>AbstractList</code> and it throw <code>UnsupportedOperationException</code> if <code>set</code> or <code>and</code> is called. If I want to get a unmodifiableList, <code>Collections.unmodifiableList()</code> is a good choice. But remember, both of them are not deeply immutable:</p>
  
  <pre><code>      List&lt;StringBuilder&gt; list = new ArrayList&lt;StringBuilder&gt;();
        StringBuilder sb = new StringBuilder();
        sb.append("hello");
        list.add(sb);
        System.out.println(list);
        list = Collections.unmodifiableList(list);
        sb.append("world");
        System.out.println(list);
  </code></pre>
  
  <p>There is a <code>flyweight pattern</code> in <code>CountingIntegerList</code>. because everytime <code>get()</code> is called,it get caches from Integer, the source code of <code>valueOf()</code>:</p>
  
  <pre><code>   public static Integer valueOf(int i) {
      final int offset = 128;
      if (i &gt;= -128 &amp;&amp; i &lt;= 127) { // must cache 
          return IntegerCache.cache[i + offset];
      }
          return new Integer(i);
      }
  </code></pre>
  
  <p>Is is right?</p>