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

oracle.nosql.driver.values.StringValue Maven / Gradle / Ivy

/*-
 * Copyright (c) 2011, 2020 Oracle and/or its affiliates.  All rights reserved.
 *
 * Licensed under the Universal Permissive License v 1.0 as shown at
 *  https://oss.oracle.com/licenses/upl/
 */

package oracle.nosql.driver.values;

import static oracle.nosql.driver.util.CheckNull.requireNonNull;

import com.fasterxml.jackson.core.io.CharTypes;

import oracle.nosql.driver.util.SizeOf;

/**
 * A {@link FieldValue} instance representing a string value.
 */
public class StringValue extends FieldValue {

    private final String value;

    /**
     * Creates a new instance
     *
     * @param value the value to use
     */
    public StringValue(String value) {
        super();
        requireNonNull(value, "StringValue: value must be non-null");
        this.value = value;
    }

    @Override
    public Type getType() {
        return Type.STRING;
    }

    /**
     * Returns the String value
     *
     * @return the String value of this object
     */
    public String getValue() {
        return value;
    }

    @Override
    public boolean equals(Object other) {
        if (other instanceof StringValue) {
            return value.equals(((StringValue)other).value);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return value.hashCode();
    }

    @Override
    public int compareTo(FieldValue other) {
        if (other instanceof StringValue) {
            return value.compareTo(other.asString().getValue());
        }
        throw new ClassCastException("Object is not an StringValue");
    }

    @Override
    public String toJson(JsonOptions options) {
        StringBuilder sb = new StringBuilder();
        sb.append("\"");
        CharTypes.appendQuoted(sb, value);
        sb.append("\"");
        return sb.toString();
    }

    /**
     * @hidden
     */
    @Override
    public long sizeof() {
        return (SizeOf.OBJECT_OVERHEAD +
                SizeOf.OBJECT_REF_OVERHEAD +
                SizeOf.stringSize(value));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy