function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Peter Somogyvari 13Peter Somogyvari 13 

Implementing BitList Collection In Apex

Hiya,


I need a BitList collection, that will be backing up a bloom filter implementation. The bloom filter will store large amounts of Salesforce IDs and need to be able to have add() and test() methods.
The BitList would need to be able to access bits via indices. If it returns elements as Booleans that's fine.

Is there any existing implementation out there that I could leverage?
Could someone point out the pitfalls here, what could be the best way to approach the problem?

I guess another question I need to ask is this: Is there any upside to implementing a BitList, instead of just using a standard Apex collection with Booleans? Note here: The BitList has to be serializable as well, I plan to use the new Platform Cache feature to store instances of BitLists.

Sketch of the interface as I imagine it: 

interface BitList {

  // constructor, initalises collection with given, fixed size and with all items as false initially
  public BitList(final Integer collectionSize);

  // constructor, initailises bit list from an array of integers (each bit of the integers become an item in the bitlist)
  // so an input of 5 integers of 32 bits each would result in a BitList with a size of 160 (5*32)
  public BitList(final Integer[] sourceBits);

  public void set(final Integer anIndex, final Boolean aBit);
  public Boolean get(final Integer anIndex);
  
  // Does this have to be implemented manually? Depends on the way bits are stored I guess.
  public String serialize();
}
James LoghryJames Loghry
Apex in general supports a wide range of collections.  I'm not familiar with "BitLists", but imagine that Apex can handle them as well.  I'll caveat that with depending on the type of object you need to support.

That being said, Lists along with Apex classes and sobjects are serializeable (and deserializable) and utilize JSON to do so.  You would use Apex like the following to serialize, for instance:
 
List<Bit> bitList = (List<Bit>)JSON.serialize(yourListJson,List<Bit>.class);
In the example above, yourListJson is a JSON string of your bit list.

As far as the structure of your Apex goes, Apex does support interfaces, virtual and abstract classes, etc.  However, I've found the use cases are few and far in between for true OOP in Apex.

Hope that helps.