how i brand permanent objects java
in code, i am formulating collection objects accessed several threads heed wholly stable objects immutable. an try finished insert new vigilant collection, i wish exam permanent (if not, i'll pitch an exception).
one thing i check few obvious permanent types:
private stationary final set<class> knownimmutables = new hashset<class>(arrays.aslist(
string.class, byte.class, short.class, integer.class, long.class,
float.class, double.class, boolean.class, biginteger.class, bigdecimal.class
));
...
public stationary boolean isimmutable(object o) {
relapse knownimmutables.contains(o.getclass());
}
this indeed gets me 90% way, nonetheless infrequently users wish emanate rudimentary permanent forms own:
public category immutablerectangle {
private final int width;
private final int height;
open immutablerectangle(int width, int height) {
this.width = width;
this.height = height;
}
open int getwidth() { relapse width; }
open int getheight() { relapse height; }
}
is there proceed (perhaps controlling reflection) i reliably detect presumably category immutable? fake positives (thinking it's permanent isn't) tolerable nonetheless fake negatives (thinking it's changeable isn't) are.
edited add: interjection fitting useful answers. answers separate out, i neglected interpretation confidence objectives. hazard here clueless developers -- square horizon formula used infinite numbers know next-to-nothing threading won't reading documentation. i need provoke opposing antagonistic developers -- anyone cunning adequate perform shenanigans also intelligent adequate know it's stable case. stationary research codebase an option, enlarged automated, nonetheless formula reviews can't counted since there pledge each hearing have threading-savvy reviewers.
Comments
Post a Comment