dcsimg
Making a vector object to READONLY
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Ashok_Jekaa
Posted On:   Sunday, May 26, 2002 08:48 PM

How to make a vector object to readonly? I mean that the elements in the vector should not be modified using setElementAt() method.. How to write it?

Re: Making a vector object to READONLY

Posted By:   Christian_Ey  
Posted On:   Wednesday, May 29, 2002 04:32 AM

You could subclass java.util.Vector and simply do nothing in the setElementAt() Method if the Vector is readOnly:




public class ReadOnlyVector extends java.util.Vector {

private boolean readOnly = false;

public void setReadOnly( boolean readOnly) {
this.readOnly = readOnly;
}

public boolean isReadOnly() {
return this.readOnly;
}

public void setElementAt( Object obj, int index) {
if (!this.readOnly) {
super.setElementAt( obj, index);
} else {
// do nothing or throw an exception
}
}
}


Remember that multiple other methods are able to modify the vector also, like for example the methods add() or remove().



Hope that helps.

Chris

About | Sitemap | Contact