Is there any Java library offering an ImmutableBitSet? I didn’t find any, neither Guava nor using Google.
Is there any Java library offering an ImmutableBitSet ? I didn’t find any, neither
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I decided to make a summary of all the answers:
I see no way to get everything perfect, i.e., to get an immutable subclass of
BitSet, so thatequalsworks in an thread-safe manner. I admit that I didn’t state all my requirements in the question.Inheriting from
BitSetand letting all the mutator methods throw an exception is easy and works. The only problem is thatequalscalled fromBitSetitself is not thread-safe since it accesses the non-final inherited fields directly. All other methods can be made thread-safe by a trick described below.Delegating to
BitSetis also easy and works, and its only problem is that aBitSetcan’t be equal to anImmutableBitSet. Note that for thread safety the delegate must be stored in a final field.Combining inheritance and delegation looks promising:
It looks strange, but works nearly perfect. Call to
bitSet.equals(immutableBitSet)are not thread-safe, because of them accessing the non-final fields directly. So it was just a fruitless exercise.Using a
BitIntegeris quite a lot of work if one wants to implement all the methods and conversion to and from the mutable BitSet. So I’d recommend either delegation or inheritance, depending on the desired behavior ofequalsand on the need for thread safety.