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

com.fitbur.github.dockerjava.api.model.VolumesFrom Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
package com.fitbur.github.dockerjava.api.model;

import java.io.IOException;

import com.fitbur.apache.com.fitburmons.lang.builder.EqualsBuilder;
import com.fitbur.apache.com.fitburmons.lang.builder.HashCodeBuilder;

import com.fitbur.fasterxml.jackson.core.JsonGenerator;
import com.fitbur.fasterxml.jackson.core.JsonParser;
import com.fitbur.fasterxml.jackson.core.JsonProcessingException;
import com.fitbur.fasterxml.jackson.core.ObjectCodec;
import com.fitbur.fasterxml.jackson.databind.DeserializationContext;
import com.fitbur.fasterxml.jackson.databind.JsonDeserializer;
import com.fitbur.fasterxml.jackson.databind.JsonNode;
import com.fitbur.fasterxml.jackson.databind.JsonSerializer;
import com.fitbur.fasterxml.jackson.databind.SerializerProvider;
import com.fitbur.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fitbur.fasterxml.jackson.databind.annotation.JsonSerialize;

@JsonSerialize(using = VolumesFrom.Serializer.class)
@JsonDeserialize(using = VolumesFrom.Deserializer.class)
public class VolumesFrom {

    private String container;

    private AccessMode accessMode;

    public VolumesFrom(String container) {
        this(container, AccessMode.DEFAULT);
    }

    public VolumesFrom(String container, AccessMode accessMode) {
        this.container = container;
        this.accessMode = accessMode;
    }

    public String getContainer() {
        return container;
    }

    public AccessMode getAccessMode() {
        return accessMode;
    }

    /**
     * Parses a volume from specification to a {@link VolumesFrom}.
     * 
     * @param serialized
     *            the specification, e.g. container:ro
     * @return a {@link VolumesFrom} matching the specification
     * @throws IllegalArgumentException
     *             if the specification cannot be parsed
     */
    public static VolumesFrom parse(String serialized) {
        try {
            String[] parts = serialized.split(":");
            switch (parts.length) {
            case 1: {
                return new VolumesFrom(parts[0]);
            }
            case 2: {
                return new VolumesFrom(parts[0], AccessMode.valueOf(parts[1]));
            }

            com.fitburfault: {
                throw new IllegalArgumentException();
            }
            }
        } catch (Exception e) {
            throw new IllegalArgumentException("Error parsing Bind '" + serialized + "'");
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof VolumesFrom) {
            VolumesFrom other = (VolumesFrom) obj;
            return new EqualsBuilder().append(container, other.getContainer())
                    .append(accessMode, other.getAccessMode()).isEquals();
        } else
            return super.equals(obj);
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(container).append(accessMode).toHashCode();
    }

    /**
     * Returns a string representation of this {@link VolumesFrom} suitable for inclusion in a JSON message. The format
     * is <container>:<access mode>, like the argument in {@link #parse(String)}.
     * 
     * @return a string representation of this {@link VolumesFrom}
     */
    @Override
    public String toString() {
        return container + ":" + accessMode.toString();
    }

    public static class Serializer extends JsonSerializer {

        @Override
        public void serialize(VolumesFrom volumeFrom, JsonGenerator jsonGen, SerializerProvider serProvider)
                throws IOException, JsonProcessingException {

            jsonGen.writeString(volumeFrom.toString());

        }

    }

    public static class Deserializer extends JsonDeserializer {
        @Override
        public VolumesFrom com.fitburserialize(JsonParser jsonParser, DeserializationContext com.fitburserializationContext)
                throws IOException, JsonProcessingException {

            ObjectCodec oc = jsonParser.getCodec();
            JsonNode node = oc.readTree(jsonParser);
            return VolumesFrom.parse(node.asText());

        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy