home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (AbstractElementFactory.java)

This example Java source code file (AbstractElementFactory.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

abstractelementfactory, class, direct, ioexception, logger, objectmapper, override, runtimeexception, sequenceelement, sequenceelementfactory, string

The AbstractElementFactory.java Java example source code

package org.deeplearning4j.models.sequencevectors.serialization;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.NonNull;
import org.deeplearning4j.models.sequencevectors.interfaces.SequenceElementFactory;
import org.deeplearning4j.models.sequencevectors.sequence.SequenceElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * This is universal serialization/deserialization factor for SequenceVectors serialization.
 * It will work for any <T extends SequenceElement> that doesn't breaks simple POJO rules.
 *
 * @author raver119@gmail.com
 */
public class AbstractElementFactory<T extends SequenceElement> implements SequenceElementFactory {
    private final Class targetClass;

    protected static final Logger log = LoggerFactory.getLogger(AbstractElementFactory.class);

    /**
     * This is the only constructor available for AbstractElementFactory
     * @param cls class that going to be serialized/deserialized using this instance. I.e.: VocabWord.class
     */
    public AbstractElementFactory(@NonNull Class<? extends SequenceElement> cls) {
        targetClass = cls;
    }

    /**
     * This method builds object from provided JSON
     *
     * @param json JSON for restored object
     * @return restored object
     */
    @Override
    public T deserialize(String json) {
        ObjectMapper mapper = SequenceElement.mapper();
        try {
            T ret = (T)  mapper.readValue(json, targetClass);
            return ret;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * This method serializaes object  into JSON string
     *
     * @param element
     * @return
     */
    @Override
    public String serialize(T element) {
        String json = null;
        try {
            json = element.toJSON();
        } catch (Exception e) {
            log.error("Direct serialization failed, falling back to jackson");
        }

        if (json == null || json.isEmpty()) {
            ObjectMapper mapper = SequenceElement.mapper();
            try {
                json = mapper.writeValueAsString(element);
            } catch (com.fasterxml.jackson.core.JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }

        return json;
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java AbstractElementFactory.java source code file:



my book on functional programming

 

new blog posts

 

Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.