001package de.deepamehta.core.model;
002
003import java.util.HashMap;
004import java.util.Map;
005import java.util.logging.Logger;
006
007
008
009/**
010 * A wrapper for the topic value (atomic, non-null). Supported value types are string, int, long, double, boolean.
011 */
012public class SimpleValue {
013
014    // ---------------------------------------------------------------------------------------------- Instance Variables
015
016    /**
017     * The (auto-boxed) wrapped value.
018     * Either String, Integer, Long, Double, or Boolean.
019     */
020    private Object value;
021
022    private Logger logger = Logger.getLogger(getClass().getName());
023
024    // ---------------------------------------------------------------------------------------------------- Constructors
025
026    /**
027     * Called by JAX-RS container to create a SimpleValue from a @PathParam or @QueryParam
028     */
029    public SimpleValue(String value) {
030        if (value == null) {
031            throw new IllegalArgumentException("Tried to build a SimpleValue from a null String");
032        }
033        this.value = value;
034    }
035
036    public SimpleValue(int value) {
037        this.value = value;
038    }
039
040    public SimpleValue(long value) {
041        this.value = value;
042    }
043
044    public SimpleValue(double value) {
045        this.value = value;
046    }
047
048    public SimpleValue(boolean value) {
049        this.value = value;
050    }
051
052    public SimpleValue(Object value) {
053        // check argument
054        if (value == null) {
055            throw new IllegalArgumentException("Tried to build a SimpleValue from a null Object");
056        }
057        if (!(value instanceof String || value instanceof Integer || value instanceof Long ||
058              value instanceof Double || value instanceof Boolean)) {
059            throw new IllegalArgumentException("Tried to build a SimpleValue from a " + value.getClass().getName() +
060                " (expected are String, Integer, Long, Double, or Boolean)");
061        }
062        //
063        this.value = value;
064    }
065
066    // -------------------------------------------------------------------------------------------------- Public Methods
067
068    @Override
069    public String toString() {
070        return value.toString();
071    }
072
073    public int intValue() {
074        return (Integer) value;
075    }
076
077    public long longValue() {
078        return (Long) value;
079    }
080
081    public double doubleValue() {
082        return (Double) value;
083    }
084
085    public boolean booleanValue() {
086        return (Boolean) value;
087    }
088
089    public Object value() {
090        return value;
091    }
092
093    // ---
094
095    @Override
096    public boolean equals(Object o) {
097        if (!(o instanceof SimpleValue)) {
098            return false;
099        }
100        return ((SimpleValue) o).value.equals(value);
101    }
102
103    @Override
104    public int hashCode() {
105        return value.hashCode();
106    }
107}