Is there a standard technique to "extend" a class with private constructors, as with most Singleton classes? Specifcally, I'm trying to extend the java.lang.management.ThreadInfo
class because I'm adding a LOT of them to a HashSet
to control uniqueness. However, the way I determine if two threads are equal is different and not identical to the default implementation of the equals()
method.
Extending the class is obviously not an option in this case.
Would it be reasonable to make something like a wrapper class which accepts a ThreadInfo
in the constructor and then manually populates all relevant fields with the values, then overrides equals()
and hashCode()
, or is there a better way to do this?
Something like this is what I'm starting to write, but a better implementation would be ideal:
class ThreadInfoWrapper {
private ThreadInfo info;
ThreadInfoWrapper(ThreadInfo info) {
this.info = info;
}
//Populate instance variables with Thread.State, thread ID, etc.. with
//Getters/setters and all that other stuff
public boolean equals(Object o) { //Unique implementation
}
public int hashCode() { //Whatever implementation
}
}
But this feels like a very roundabout way to achieve some basic functionality. I looked into it, and implementations of Sets with custom Comparators do not exist in the Java standard library. I suppose I could write my own hash set implementation but that's too much work for a simple situation. Any insights would be helpful.