All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.metamx.collections.bitmap.ConciseBitmapFactory Maven / Gradle / Ivy

/*
 * Copyright 2011 - 2015 Metamarkets Group Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.metamx.collections.bitmap;

import it.uniroma3.mat.extendedset.intset.ImmutableConciseSet;

import java.nio.ByteBuffer;
import java.util.Iterator;

/**
 * As the name suggests, this class instantiates bitmaps of the types
 * WrappedConciseBitmap and WrappedImmutableConciseBitmap.
 */
public class ConciseBitmapFactory implements BitmapFactory
{
  private static final ImmutableConciseSet EMPTY_IMMUTABLE_BITMAP = new ImmutableConciseSet();
  private static final WrappedImmutableConciseBitmap WRAPPED_IMMUTABLE_CONCISE_BITMAP =
      new WrappedImmutableConciseBitmap(EMPTY_IMMUTABLE_BITMAP);

  @Override
  public MutableBitmap makeEmptyMutableBitmap()
  {
    return new WrappedConciseBitmap();
  }

  @Override
  public ImmutableBitmap makeEmptyImmutableBitmap()
  {
    return WRAPPED_IMMUTABLE_CONCISE_BITMAP;
  }

  @Override
  public ImmutableBitmap makeImmutableBitmap(MutableBitmap mutableBitmap)
  {
    if (!(mutableBitmap instanceof WrappedConciseBitmap)) {
      throw new IllegalStateException(String.format("Cannot convert [%s]", mutableBitmap.getClass()));
    }
    return new WrappedImmutableConciseBitmap(
        ImmutableConciseSet.newImmutableFromMutable(
            ((WrappedConciseBitmap) mutableBitmap).getBitmap()
        )
    );
  }

  @Override
  public ImmutableBitmap mapImmutableBitmap(ByteBuffer b)
  {
    return new WrappedImmutableConciseBitmap(b);
  }

  @Override
  public ImmutableBitmap union(Iterable b)
      throws ClassCastException
  {
    return new WrappedImmutableConciseBitmap(ImmutableConciseSet.union(unwrap(b)));
  }

  @Override
  public ImmutableBitmap intersection(Iterable b)
      throws ClassCastException
  {
    return new WrappedImmutableConciseBitmap(ImmutableConciseSet.intersection(unwrap(b)));
  }

  @Override
  public ImmutableBitmap complement(ImmutableBitmap b)
  {
    return new WrappedImmutableConciseBitmap(ImmutableConciseSet.complement(((WrappedImmutableConciseBitmap) b).getBitmap()));
  }

  @Override
  public ImmutableBitmap complement(ImmutableBitmap b, int length)
  {
    return new WrappedImmutableConciseBitmap(
        ImmutableConciseSet.complement(
            ((WrappedImmutableConciseBitmap) b).getBitmap(),
            length
        )
    );
  }

  private static Iterable unwrap(
      final Iterable b
  )
  {
    return new Iterable()
    {
      @Override
      public Iterator iterator()
      {
        final Iterator i = b.iterator();
        return new Iterator()
        {
          @Override
          public void remove()
          {
            throw new UnsupportedOperationException();
          }

          @Override
          public boolean hasNext()
          {
            return i.hasNext();
          }

          @Override
          public ImmutableConciseSet next()
          {
            final WrappedImmutableConciseBitmap wrappedBitmap = (WrappedImmutableConciseBitmap) i.next();

            if (wrappedBitmap == null) {
              return EMPTY_IMMUTABLE_BITMAP;
            }

            return wrappedBitmap.getBitmap();
          }
        };
      }
    };
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy