org.anvilpowered.anvil.api.model.Mappable Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of anvil-api Show documentation
Show all versions of anvil-api Show documentation
A cross-platform database API / ORM / entity framework with useful services for minecraft plugins
The newest version!
/*
* Anvil - AnvilPowered
* Copyright (C) 2020
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
package org.anvilpowered.anvil.api.model;
import com.google.common.collect.ImmutableList;
import jetbrains.exodus.util.ByteArraySizedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public interface Mappable {
/**
* Writes all data from this object to the provided {@code object}.
*
* Fields that are {@code null} in this object but not {@code null}
* in the target object should not be overwritten.
*
*
* @param object Object to write data to
* @return object with data written to. Same instance as provided object
*/
T writeTo(T object);
/**
* Reads all data from the provided {@code object} to this object
*
* Fields that are {@code null} in the provided object but not {@code null}
* this object should not be overwritten.
*
*
* @param object Object to read data from
*/
void readFrom(T object);
static byte[] serializeUnsafe(Object object) throws IOException {
try (
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutput oos = new ObjectOutputStream(baos)
) {
oos.writeObject(object);
return baos.toByteArray();
}
}
static Optional serialize(Object object) {
try {
return Optional.of(serializeUnsafe(object));
} catch (IOException ignored) {
return Optional.empty();
}
}
@SuppressWarnings("unchecked")
static T deserializeUnsafe(InputStream inputStream) throws IOException, ClassNotFoundException {
try (
ObjectInput objectInputStream = new ObjectInputStream(inputStream)
) {
return (T) objectInputStream.readObject();
}
}
static Optional deserialize(InputStream inputStream) {
try {
return Optional.of(deserializeUnsafe(inputStream));
} catch (IOException | ClassNotFoundException | ClassCastException ignored) {
return Optional.empty();
}
}
static boolean addToCollection(InputStream inputStream, Consumer callback, Collection elements) {
Collection collection;
try {
collection = deserializeUnsafe(inputStream);
} catch (IOException | ClassNotFoundException ignored) {
return false;
}
try {
collection.addAll(elements);
} catch (UnsupportedOperationException ignored) {
Collection temp = new ArrayList<>(collection.size() + elements.size());
temp.addAll(collection);
temp.addAll(elements);
collection = temp;
}
byte[] data;
try {
data = serializeUnsafe(collection);
} catch (IOException ignored) {
return false;
}
callback.accept(new ByteArraySizedInputStream(data));
return true;
}
@SafeVarargs
static boolean addToCollection(InputStream inputStream, Consumer callback, T... elements) {
return addToCollection(inputStream, callback, ImmutableList.copyOf(elements));
}
static boolean removeFromCollection(InputStream inputStream, Consumer callback,
Predicate super T> filter) {
Collection collection;
try {
collection = deserializeUnsafe(inputStream);
} catch (IOException | ClassNotFoundException ignored) {
return false;
}
try {
collection.removeIf(filter);
} catch (UnsupportedOperationException ignored) {
collection = collection.stream().filter(filter.negate()).collect(Collectors.toList());
}
byte[] data;
try {
data = serializeUnsafe(collection);
} catch (IOException ignored) {
return false;
}
callback.accept(new ByteArraySizedInputStream(data));
return true;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy