com.conwet.silbops.model.basic.Value.java Source code

Java tutorial

Introduction

Here is the source code for com.conwet.silbops.model.basic.Value.java

Source

package com.conwet.silbops.model.basic;

/*
 * #%L
 * SilboPS API
 * %%
 * Copyright (C) 2011 - 2014 CoNWeT Lab., Universidad Politcnica de Madrid
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */

import org.json.simple.JSONValue;

import com.conwet.silbops.util.JSONizable;

/**
 * This class represents the value of an attribute.</br>
 * Getters method never returns null.
 * 
 * @author sergio
 */
public abstract class Value implements Comparable<Value>, JSONizable {

    private Type type;
    private Object value;

    private Value(Type type, Object value) {
        // private constructor to enforce object creation
        // parameters can't be null because they are controlled in valueOf()
        this.type = type;
        this.value = value;
    }

    @Override
    public int compareTo(Value other) {

        if (type != other.type) {

            throw new IllegalArgumentException("type mismatch: type1=" + type + ", type2=" + other.type);
        }

        return compareValue(other);
    }

    protected abstract int compareValue(Value other);

    @Override
    public boolean equals(Object obj) {

        if (this == obj) {
            return true;
        }

        if (obj instanceof Value) {

            Value other = (Value) obj;
            return type == other.type && value.equals(other.value);
        }

        return false;
    }

    @Override
    public int hashCode() {

        return 59 * type.hashCode() + value.hashCode();
    }

    public Type getType() {

        return type;
    }

    public Object getValue() {

        return value;
    }

    public Double getDouble() {

        throw new UnsupportedOperationException("Method not supported for this type: " + type);
    }

    public Long getLong() {

        throw new UnsupportedOperationException("Method not supported for this type: " + type);
    }

    public String getString() {

        throw new UnsupportedOperationException("Method not supported for this type: " + type);
    }

    @Override
    public String toString() {

        return value.toString();
    }

    /**
     * Wraps a value in a Value object. This is the only factory method available.
     *
     * @param value either String, Double or Long
     * @return The wrapped value
     * @throws IllegalArgumentException if the given value isn't valid
     */
    public static Value valueOf(Object value) {

        if (value instanceof String) {
            return new StringValue((String) value);
        } else if (value instanceof Long || value instanceof Integer) {
            return new LongValue(((Number) value).longValue());
        } else if (value instanceof Double || value instanceof Float) {
            return new DoubleValue(((Number) value).doubleValue());
        } else if (value instanceof Value) {// cast if necessary
            return (Value) value;
        } else {
            throw new IllegalArgumentException("Invalid value: " + value);
        }
    }

    public static Value fromJSON(Object json) {

        return valueOf(json);
    }

    @Override
    public Object toJSON() {

        return value;
    }

    @Override
    public String toJSONString() {

        return JSONValue.toJSONString(toJSON());
    }

    // real classes returned by the factory method
    private static class DoubleValue extends Value {

        private DoubleValue(Double value) {
            super(Type.DOUBLE, value);
        }

        @Override
        protected int compareValue(Value other) {

            return getDouble().compareTo(other.getDouble());
        }

        @Override
        public Double getDouble() {

            return (Double) getValue();
        }
    }

    private static class LongValue extends Value {

        private LongValue(Long value) {

            super(Type.LONG, value);
        }

        @Override
        protected int compareValue(Value other) {

            return getLong().compareTo(other.getLong());
        }

        @Override
        public Long getLong() {

            return (Long) getValue();
        }
    }

    private static class StringValue extends Value {

        private StringValue(String value) {
            super(Type.STRING, value);
        }

        @Override
        protected int compareValue(Value other) {

            return getString().compareTo(other.getString());
        }

        @Override
        public String getString() {

            return (String) getValue();
        }
    }
}