Simple BloomFilter Class We have some BloomFilters (like a java.util.set without deletion) and we store them in ehcache (you can think it's as a java.util.Map):

For example, Constant.BLOOM_FILTER_CACHE ehcache contains:

key   value
aaa   bloomfilter (of name aaa)
bbb   bloomfilter (of name bbb)
ccc   bloomfilter (of name ccc)


If we call localCacheManager.get(Constant.BLOOM_FILTER_CACHE, aaa), it will return a bloomfilter (of name aaa) or Null (if not initialized).

If we call localCacheManager.put(Constant.BLOOM_FILTER_CACHE, aaa, new BloomFilter(100, 0.001)), it will put a bloomfilte to cache entry aaa.

If we want to add data1 into BloomFilter of name aaa, then we may call add(aaa, data1) and if we want to know if aaa BloomFilter contains data1, we should call contains(aaa, data1) (in that case, it will return true).

If we want to look up or add, we should check the bloomfilter is initialized or not.

package com.xxx.utils.bloomfilter;

import com.clearspring.analytics.stream.membership.BloomFilter;
import com.xxx.utils.cache.ehcache.LocalCacheManager;
import com.xxx.utils.common.Constant;

public class BloomFilterInCache {
  private int bloomFilterElementSize;
  private double bloomFilterErrorRate;
  private LocalCacheManager localCacheManager;

  public BloomFilterInCache(int bloomFilterElementSize,
      double bloomFilterErrorRate, LocalCacheManager localCacheManager) {
    this.bloomFilterElementSize = bloomFilterElementSize;
    this.bloomFilterErrorRate = bloomFilterErrorRate;
    this.localCacheManager = localCacheManager;
  }

  private boolean isBloomFilterExistsByName(String name)
  {
    BloomFilter bf = (BloomFilter) localCacheManager.get(Constant.BLOOM_FILTER_CACHE, name);
    if (bf == null)
      return false;
    return true;
  }

  private void newBloomFilter(String name)
  {
    BloomFilter bf = new BloomFilter(bloomFilterElementSize, bloomFilterErrorRate);
    localCacheManager.put(Constant.BLOOM_FILTER_CACHE, name, bf);
  }

  private void NotExistsThenPut(String name)
  {
    if (!isBloomFilterExistsByName(name))
    {
      newBloomFilter(name);
    }
  }

  public boolean contains(String name, String key)
  {
    NotExistsThenPut(name);
    BloomFilter bf = (BloomFilter) localCacheManager.get(Constant.BLOOM_FILTER_CACHE, name);
    return bf.isPresent(key);
  }

  public void add(String name, String key)
  {
    NotExistsThenPut(name);
    BloomFilter bf = (BloomFilter) localCacheManager.get(Constant.BLOOM_FILTER_CACHE, name);
    bf.add(key);
  }
}


Are there any flaws in this code?
private boolean isBloomFilterExistsByName(String name)
{


The brace belong on the previous line.

isBloomFilterExistsByName(String name)


is...Exists sounds strange.

 private void newBloomFilter(String name)


From the name, I'd expect this method to return a new Bloom Filter instead of putting it somewhere.

 private void NotExistsThenPut(String name)


Java method names starts with lowercase. A better name would be putIfNotExists.

I'd actually prefer a method like getOrCreate.

public boolean contains(String name, String key)


contains and isPresent are wrong names for this. A Bloom Filter is like a Set, but an undependable one. A much better name is mayContain.

This method is an invitation to inefficiency. Imagine a tight loop calling it. On every iteration you look up the Bloom Filter twice (once to make sure that it exists and once to use it). This may easily dominate the cost of the filter's isPresent operation.

I'd suggest to provide methods allowing to use it like

 bloomFilterInCache.getOrCreate("name").mayContain("element");


which allows efficiency via "extract local variable".

 public void add(String name, String key)


Again, an invitation to inefficiency.