Skip to content

Code Examples

Anastasia Braginsky edited this page Aug 9, 2018 · 2 revisions

Integer to Integer build example

    KeySerializer<Integer> keySerializer = new KeySerializer<Integer>() {

      @Override
      public void serialize(Integer key, ByteBuffer targetBuffer) {
        targetBuffer.putInt(targetBuffer.position(), key);
      }

      @Override
      public Integer deserialize(ByteBuffer serializedKey) {
        return serializedKey.getInt(serializedKey.position());
      }

      @Override
      public int calculateSize(Integer object) {
        return Integer.BYTES;
      }
    };

    ValueSerializer<Integer, Integer> valueSerializer = new ValueSerializer<Integer, Integer>() {
      @Override
      public void serialize(Integer key, Integer value, ByteBuffer targetBuffer) {
        targetBuffer.putInt(targetBuffer.position(), value);
      }

      @Override
      public Integer deserialize(ByteBuffer serializedKey, ByteBuffer serializedValue) {
        return serializedValue.getInt(serializedValue.position());
      }

      @Override
      public int calculateSize(Integer object) {
        return Integer.BYTES;
      }
    };

    OakComparator<Integer> keysComparator = new OakComparator<Integer>() {

      @Override
      public int compareKeys(Integer int1, Integer int2) {
        return intsCompare(int1, int2);
      }

      @Override
      public int compareSerializedKeys(ByteBuffer buff1, ByteBuffer buff2) {
        int int1 = buff1.getInt(buff1.position());
        int int2 = buff2.getInt(buff2.position());
        return intsCompare(int1, int2);
      }

      @Override
      public int compareSerializedKeyAndKey(ByteBuffer buff1, Integer int2) {
        int int1 = buff1.getInt(buff1.position());
        return intsCompare(int1, int2);
      }
     };

     OakMapBuilder<Integer, Integer> builder = new OakMapBuilder<Integer, Integer>()
                 .setKeySerializer(keySerializer)
                 .setValueSerializer(valueSerializer)
                 .setMinKey(new Integer(Integer.MIN_VALUE))
                 .setKeysComparator(keysComparator)
                 .setMemoryCapacity(1048576); // 1MB in bytes

     OakMap<Integer,Integer> oak = (OakMap<Integer, Integer>) builder.build();
Clone this wiki locally