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 ofMap(Map oldMap) { var newMap = new HashMap(oldMap.size()); oldMap.forEach((key, value) -> newMap.put(new Bytes(key), new Bytes(value))); return newMap; } public static UnmodifiableMap ofMap(UnmodifiableIterableMap oldMap) { Bytes[] keys = new Bytes[oldMap.size()]; Bytes[] values = new Bytes[oldMap.size()]; IntWrapper i = new IntWrapper(0); oldMap.forEach((key, value) -> { keys[i.var] = new Bytes(key); values[i.var] = new Bytes(value); i.var++; }); return UnmodifiableMap.of(keys, values); } public static List ofList(List oldList) { var newList = new ArrayList(oldList.size()); oldList.forEach((item) -> newList.add(new Bytes(item))); return newList; } public static Set ofSet(Set oldSet) { var newSet = new ObjectOpenHashSet(oldSet.size()); oldSet.forEach((item) -> newSet.add(new Bytes(item))); return newSet; } public static UnmodifiableIterableSet toIterableSet(UnmodifiableSet set) { byte[][] resultItems = new byte[set.size()][]; var it = set.iterator(); int i = 0; while (it.hasNext()) { var item = it.next(); resultItems[i] = item.data; i++; } return UnmodifiableIterableSet.of(resultItems); } public static byte[][] toByteArray(Collection 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); } }