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

sirius.biz.i5.Transformable Maven / Gradle / Ivy

There is a newer version: 9.6
Show newest version
/*
 * Made with all the love in the world
 * by scireum in Remshalden, Germany
 *
 * Copyright by scireum GmbH
 * http://www.scireum.de - [email protected]
 */

package sirius.biz.i5;

import com.google.common.collect.Maps;

import javax.annotation.Nonnull;
import java.util.Map;

import static sirius.kernel.health.Exceptions.handle;

/**
 * Base class for all types which can automatically be transformed from and to byte arrays.
 * 

* The {@link Transformer} for the given class will read all {@link Transform} annotations and determine * which parts of the byte array belong to which Java field. It then converts them using the appropriate * AS400... classes. */ public abstract class Transformable { private static Map, Transformer> transformers = Maps.newConcurrentMap(); private static Transformer getTransformer(@Nonnull Class type) { return transformers.computeIfAbsent(type, k -> new Transformer(type)); } /** * Tries for create a new instance of the given type by parsing the given byte array. * * @param type the type to be created * @param the generic type for type * @param data the byte array to parse * @return a new instance of T filled with the data parsed from data */ public static T parse(@Nonnull Class type, @Nonnull byte[] data) { try { Transformer tx = getTransformer(type); T result = type.newInstance(); tx.fromBytes(result, data); return result; } catch (Exception e) { throw handle().to(I5Connector.LOG) .error(e) .withSystemErrorMessage("Cannot load data for '%s': %s (%s)", type.getName()) .handle(); } } /** * Transforms this object into a byte representation which can be sent to an i5. * * @param destination the byte array to fill */ public void toBytes(byte[] destination) { getTransformer(getClass()).toBytes(this, destination); } @Override public String toString() { return getTransformer(getClass()).asString(this); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy