Skip to content

Code Examples

Eran Meir edited this page Apr 1, 2019 · 2 revisions

Integer to Integer build example

   OakSerializer<Integer> integerSerializer = new OakSerializer<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;
        }
    };

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

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

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

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

    OakMapBuilder<Integer, Integer> builder = new OakMapBuilder<Integer, Integer>()
            .setKeySerializer(integerSerializer)
            .setValueSerializer(integerSerializer)
            .setMinKey(Integer.MIN_VALUE)
            .setComparator(comparator)
            .setMemoryCapacity(1048576); // 1MB in bytes

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