java - Is it possible to repair this class to make it Immutable -


i'm trying repair following class become immutable:

public class immutablelist<e> implements iterable<e> {     private list<e> internal;     public immutablelist(list<e> l) {         this.internal = l;     }     public int size() {         return this.internal.size();     }     public e get(int i) {         return this.internal.get(i);     }     public iterator<e> iterator() {         return this.internal.iterator();     } } 

but @ moment i'm not successful, problem don't know type of e don't know how deep copy it. wanted know if possible make class immutable?

thanks in advance

your class nearly "as immutable possible." there's no general way deal arbitrary generic types, should not worry it.

two remaining issues:

  • you need copy l -- shallow copy suffice. this.internal = new arraylist<e>(l) do.
  • if internal.iterator() supports remove(), consumers of list type can remove elements.
  • make class final cannot subclassed in mutable way.

Comments

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -