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

com.github.scr.hashmap.collections.LongBufferCollection Maven / Gradle / Ivy

Go to download

Readonly Set and Map that use a MappedByteBuffer to load quickly and use minimal Java Heap.

There is a newer version: 1.2.0
Show newest version
/*
The MIT License (MIT)

Copyright (c) 2015 Sheridan C Rawlins

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
 */

package com.github.scr.hashmap.collections;

import com.github.scr.hashmap.Constants;
import com.google.common.collect.Iterators;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.DataOutput;
import java.io.IOException;
import java.nio.LongBuffer;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

/**
 * Created by scr on 7/2/15.
 */
public class LongBufferCollection implements IndexedCollection {
    @NotNull
    private final LongBuffer BUFFER;

    @Nullable
    @Override
    public Long get(int index) {
        return BUFFER.get(index);
    }

    @Override
    public void writeOutput(@NotNull DataOutput dataOutput) throws IOException {
        dataOutput.writeInt(Constants.MAGIC);
        dataOutput.writeInt(Constants.VERSION);
        int size = BUFFER.remaining();
        dataOutput.writeInt(size);
        for (int i = 0; i < size; ++i) {
            dataOutput.writeLong(BUFFER.get(i));
        }
    }

    public static class BufferIterator implements Iterator {
        @NotNull
        private final LongBuffer BUFFER;

        public BufferIterator(@NotNull LongBuffer buffer) {
            BUFFER = buffer.duplicate();
        }

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

        @NotNull
        @Override
        public Long next() {
            return BUFFER.get();
        }
    }

    public LongBufferCollection(@NotNull LongBuffer ints) {
        BUFFER = ints;
    }

    @Override
    public int size() {
        return BUFFER.capacity();
    }

    @Override
    public boolean isEmpty() {
        return BUFFER.capacity() > 0;
    }

    @Override
    public boolean contains(Object o) {
        if (!(o instanceof Long)) {
            return false;
        }
        Long oLong = (Long) o;
        return Iterators.contains(iterator(), oLong);
    }

    @NotNull
    @Override
    public Iterator iterator() {
        return new BufferIterator(BUFFER);
    }

    @NotNull
    @Override
    public Object[] toArray() {
        int size = size();
        Object[] ret = new Object[size];
        for (int i = 0; i < size; ++i) {
            ret[i] = BUFFER.get(i);
        }
        return ret;
    }

    @NotNull
    @Override
    public  T[] toArray(@NotNull T[] a) {
        int size = size();
        if (a.length < size) {
            a = Arrays.copyOf(a, size);
        }
        for (int i = 0; i < size; ++i) {
            @SuppressWarnings("unchecked")
            T t = (T) Long.valueOf(BUFFER.get(i));
            a[i] = t;
        }
        return a;
    }

    @Override
    public boolean add(@NotNull Long aLong) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean remove(@NotNull Object o) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean containsAll(@NotNull Collection c) {
        for (Object o : c) {
            if (!contains(o)) {
                return false;
            }
        }
        return true;
    }

    @Override
    public boolean addAll(@NotNull Collection c) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean removeAll(@NotNull Collection c) {
        throw new UnsupportedOperationException();
    }

    @Override
    public boolean retainAll(@NotNull Collection c) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void clear() {
        throw new UnsupportedOperationException();
    }

    @Override
    public long getLong(int index) {
        return BUFFER.get(index);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy