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

io.citrine.jpif.obj.common.Source Maven / Gradle / Ivy

Go to download

This package includes java objects for all items in the Physical Information File (PIF), http://www.citrine.io/pif. Files formatted in the PIF schema can be serialized and deserialized using included methods.

There is a newer version: 2.7.2
Show newest version
package io.citrine.jpif.obj.common;

import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import io.citrine.jpif.util.PifObjectMapper;
import io.citrine.jpif.util.PifSerializationUtil;
import org.apache.commons.validator.routines.UrlValidator;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;

/**
 * Information about the source of a system.
 *
 * 

Supported fields: *

    *
  • producer - Name of the producer. *
  • url - URL at which the information about the system can be accessed. *
  • tags - List of tags that apply to the source. *
* * @author Kyle Michel */ public class Source extends Pio implements Serializable { /** * Set the producer of this system. * * @param producer String with the name of the producer of the system. * @return This object. */ @JsonSetter(value = "producer") public Source setProducer(final String producer) { this.producer = producer; return this; } /** * Get the producer for this system. * * @return String with the name of the producer for this system. */ @JsonGetter(value = "producer") public String getProducer() { return this.producer; } /** * Set the URL of this system. * * @param url String with the URL of the system. * @return This object. */ @JsonSetter(value = "url") public Source setUrl(final String url) { this.url = url; return this; } /** * Get the URL for this system. * * @return String with the URL for this system. */ @JsonGetter(value = "url") public String getUrl() { return this.url; } @Override public Source addTag(final String tag) { super.addTag(tag); return this; } @Override public Source addTag(final int index, final String tag) { super.addTag(index, tag); return this; } @Override @JsonAnySetter public Source addUnsupportedField(final String key, final Object value) { super.addUnsupportedField(key, value); return this; } /** * Generate a new {@link Source} object from the input string. If the string contains a valid URL, then it is * assigned to the resulting object. Otherwise the string is assumed to be a producer. * * @param source String with the value to convert. * @return {@link Source} object. */ public static Source valueOf(final String source) { return isUrl(source) ? new Source().setUrl(source) : new Source().setProducer(source); } /** * Determine whether the input string is a URL. * * @param source String to check as a URL. * @return True if the string is a URL. */ private static boolean isUrl(final String source) { return (source != null) && UrlValidator.getInstance().isValid(source.trim()); } /** * Write this object to the output output stream. * * @param out {@link ObjectOutputStream} to write to. * @throws IOException if this object cannot be written. */ private void writeObject(ObjectOutputStream out) throws IOException { PifSerializationUtil.write(out, this); } /** * Read into this object from the input stream. * * @param in {@link ObjectInputStream} to read from. * @throws IOException if thrown while reading the stream. * @throws ClassNotFoundException if thrown while reading the stream. */ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { PifSerializationUtil.read(in, this); } /** * Read an object with no data. * * @throws ObjectStreamException if thrown while reading the stream. */ private void readObjectNoData() throws ObjectStreamException {} private static final long serialVersionUID = 451087929374693919L; /** Producer of the system. */ private String producer; /** Url for the system. */ private String url; /** * Class used to deserialize a JSON value into a {@link Source} object. If the input token is a string then an * attempt is made to determine the information that it contains. If the input token is an object, then it is * converted directly to a {@link Source} object. * * @author Kyle Michel */ public static class Deserializer extends JsonDeserializer { @Override public Source deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { final JsonToken jsonToken = jsonParser.getCurrentToken(); switch (jsonToken) { case VALUE_STRING: return Source.valueOf(jsonParser.getValueAsString()); case START_OBJECT: return PifObjectMapper.getInstance().readValue(jsonParser, Source.class); default: throw deserializationContext.mappingException(Source.class, jsonToken); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy