com.simiacryptus.util.data.SerialArrayList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-util Show documentation
Show all versions of java-util Show documentation
Miscellaneous utilities for Java 8
/*
* Copyright (c) 2019 by Andrew Charneski.
*
* The author licenses this file to you 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.simiacryptus.util.data;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collection;
import java.util.function.Function;
public class SerialArrayList {
public final int unitSize;
private final SerialType factory;
private byte[] buffer;
private int maxByte = 0;
public SerialArrayList(SerialType factory, SerialArrayList... items) {
this.factory = factory;
this.unitSize = factory.getSize();
this.maxByte = Arrays.stream(items).mapToInt(item -> item.maxByte).sum();
this.buffer = new byte[this.maxByte];
int cursor = 0;
for (int i = 0; i < items.length; i++) {
SerialArrayList item = items[i];
System.arraycopy(item.buffer, 0, this.buffer, cursor, item.maxByte);
cursor += item.maxByte;
}
}
public SerialArrayList(SerialType factory, Collection items) {
this.factory = factory;
this.unitSize = factory.getSize();
this.buffer = new byte[items.size() * unitSize];
int i = 0;
for (U x : items) set(i++, x);
}
public SerialArrayList(SerialType factory, U... items) {
this.factory = factory;
this.unitSize = factory.getSize();
this.buffer = new byte[items.length * unitSize];
for (int i = 0; i < items.length; i++) set(i, items[i]);
}
public SerialArrayList(SerialType factory) {
this.factory = factory;
this.unitSize = factory.getSize();
this.buffer = new byte[1024];
}
public SerialArrayList(SerialType factory, int size) {
this.factory = factory;
this.unitSize = factory.getSize();
this.buffer = new byte[this.unitSize * size];
}
public SerialArrayList add(SerialArrayList right) {
return new SerialArrayList(factory, this, right);
}
public synchronized void clear() {
buffer = new byte[]{};
maxByte = 0;
}
public int length() {
return maxByte / unitSize;
}
public U get(int i) {
ByteBuffer view = getView(i);
try {
return factory.read(view);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public synchronized int add(U value) {
int length = length();
set(length, value);
return length;
}
public synchronized U update(int i, Function updater) {
U updated = updater.apply(this.get(i));
set(i, updated);
return updated;
}
public void set(int i, U value) {
ensureCapacity((i + 1) * unitSize);
ByteBuffer view = getView(i);
try {
factory.write(view, value);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private ByteBuffer getView(int i) {
ByteBuffer duplicate = ByteBuffer.wrap(buffer);
duplicate.position(unitSize * i);
return duplicate;
}
private synchronized void ensureCapacity(int bytes) {
if (maxByte < bytes) {
maxByte = bytes;
}
int targetBytes = buffer.length;
while (targetBytes < bytes) targetBytes = Math.max(targetBytes * 2, 1);
if (targetBytes > buffer.length) {
buffer = Arrays.copyOf(buffer, targetBytes);
}
}
public synchronized int addAll(Collection data) {
int startIndex = length();
putAll(data, startIndex);
return startIndex;
}
public synchronized void putAll(Collection data, int startIndex) {
putAll(new SerialArrayList(factory, data), startIndex);
}
public synchronized void putAll(SerialArrayList data, int startIndex) {
ensureCapacity((startIndex * unitSize) + data.maxByte);
System.arraycopy(data.buffer, 0, this.buffer, startIndex * unitSize, data.maxByte);
}
public int getMemorySize() {
return buffer.length;
}
public SerialArrayList copy() {
return new SerialArrayList(factory, this);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SerialArrayList> that = (SerialArrayList>) o;
if (unitSize != that.unitSize) return false;
if (maxByte != that.maxByte) return false;
if (!factory.equals(that.factory)) return false;
return Arrays.equals(buffer, that.buffer);
}
@Override
public int hashCode() {
int result = factory.hashCode();
result = 31 * result + unitSize;
result = 31 * result + Arrays.hashCode(buffer);
result = 31 * result + maxByte;
return result;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy