Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6627783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:03:16+00:00 2026-05-25T22:03:16+00:00

Is there any Java library offering an ImmutableBitSet ? I didn’t find any, neither

  • 0

Is there any Java library offering an ImmutableBitSet? I didn’t find any, neither Guava nor using Google.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:03:17+00:00Added an answer on May 25, 2026 at 10:03 pm

    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 that equals works in an thread-safe manner. I admit that I didn’t state all my requirements in the question.

    Inheriting from BitSet and letting all the mutator methods throw an exception is easy and works. The only problem is that equals called from BitSet itself 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 BitSet is also easy and works, and its only problem is that a BitSet can’t be equal to an ImmutableBitSet. Note that for thread safety the delegate must be stored in a final field.

    Combining inheritance and delegation looks promising:

    public class ImmutableBitSet extends BitSet {
        private final ImmutableBitSet delegate;
    
        public ImmutableBitSet(BitSet original) {
            or(original); // copy original to this
            delegate = this; // initialize a final reference for thread safety
        }
    
        @Override // example mutator method
        public void and(BitSet set) {
            throw new UnsupportedOperationException();
        }
    
        @Override // example non-mutator method
        public boolean get(int bitIndex) {
            return delegate.getPrivate(bitIndex);
        }
    
        // needed in order to avoid endless recursion
        private boolean getPrivate(int bitIndex) {
            super.get(bitIndex);
        }
    
        ...
    }
    

    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 BitInteger is 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 of equals and on the need for thread safety.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any Java or C# library for signal analysis using the sound card
Is there any Java library that supports hierarchical column? For example (the first three
Is there any free java library which I can use to convert string in
Is there any free Java library for extracting text from PDF, that is compatible
Is there any recommended Java application profiling tutorial? I am now using JProfiler and
Is there any java library that is similar to unix's command file ? ie:
Is there any Java library, that can help in building AST from the specified
Is there any java library that allows to export LaTeX document to other formats
Is there any way to detect and handle whether a Java library is correctly
I would like to ask if there's any Java package or library that have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.