common-utils/src/main/java/org/warp/commonutils/type/Bytes.java

69 lines
1.7 KiB
Java

package org.warp.commonutils.type;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jetbrains.annotations.NotNull;
public class Bytes {
public final byte[] data;
public Bytes(byte @NotNull[] data) {
this.data = data;
}
public static Map<? extends Bytes,? extends Bytes> ofMap(Map<byte[], byte[]> oldMap) {
var newMap = new HashMap<Bytes, Bytes>(oldMap.size());
oldMap.forEach((key, value) -> newMap.put(new Bytes(key), new Bytes(value)));
return newMap;
}
public static List<? extends Bytes> ofList(List<byte[]> oldList) {
var newList = new ArrayList<Bytes>(oldList.size());
oldList.forEach((item) -> newList.add(new Bytes(item)));
return newList;
}
public static Set<? extends Bytes> ofSet(Set<byte[]> oldSet) {
var newSet = new ObjectOpenHashSet<Bytes>(oldSet.size());
oldSet.forEach((item) -> newSet.add(new Bytes(item)));
return newSet;
}
public static byte[][] toByteArray(Collection<Bytes> value) {
Bytes[] valueBytesArray = value.toArray(Bytes[]::new);
byte[][] convertedResult = new byte[valueBytesArray.length][];
for (int i = 0; i < valueBytesArray.length; i++) {
convertedResult[i] = valueBytesArray[i].data;
}
return convertedResult;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Bytes that = (Bytes) o;
return Arrays.equals(data, that.data);
}
@Override
public int hashCode() {
return Arrays.hashCode(data);
}
@Override
public String toString() {
return Arrays.toString(data);
}
}