001/*
002 * ModeShape (http://www.modeshape.org)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *       http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.modeshape.schematic.internal.document;
017
018import java.util.UUID;
019import java.util.regex.Pattern;
020import org.modeshape.schematic.annotation.Immutable;
021import org.modeshape.schematic.document.Binary;
022import org.modeshape.schematic.document.Document;
023import org.modeshape.schematic.document.Json;
024
025@Immutable
026public class ImmutableField implements Document.Field {
027
028    private final String name;
029    private final Object value;
030
031    public ImmutableField( String name,
032                           Object value ) {
033        this.name = name;
034        this.value = value;
035    }
036
037    @Override
038    public int compareTo( Document.Field that ) {
039        return this == that ? 0 : this.name.compareTo(that.getName());
040    }
041
042    @Override
043    public String getName() {
044        return name;
045    }
046
047    @Override
048    public Object getValue() {
049        return value;
050    }
051
052    @Override
053    public int hashCode() {
054        return name.hashCode();
055    }
056
057    @Override
058    public boolean equals( Object obj ) {
059        if (obj == this) return true;
060        if (obj instanceof Document.Field) {
061            Document.Field that = (Document.Field)obj;
062            if (!this.getName().equals(that.getName())) return true;
063            return this.getValue() != null ? this.getValue().equals(that.getValue()) : that.getValue() == null;
064        }
065        return false;
066    }
067
068    @Override
069    public String toString() {
070        return Json.write(this);
071    }
072
073    @Override
074    public String getValueAsString() {
075        Object value = getValue();
076        return (value != null && value instanceof String) ? (String)value : null;
077    }
078
079    @Override
080    public Integer getValueAsInt() {
081        Object value = getValue();
082        return (value != null && value instanceof Integer) ? (Integer)value : null;
083    }
084
085    @Override
086    public boolean getValueAsBoolean() {
087        Object value = getValue();
088        return (value != null && value instanceof Boolean) ? ((Boolean)value).booleanValue() : false;
089    }
090
091    @Override
092    public Binary getValueAsBinary() {
093        Object value = getValue();
094        return (value != null && value instanceof Binary) ? (Binary)value : null;
095    }
096
097    @Override
098    public Document getValueAsDocument() {
099        Object value = getValue();
100        return (value != null && value instanceof Document) ? (Document)value : null;
101    }
102
103    @Override
104    public Number getValueAsNumber() {
105        Object value = getValue();
106        return (value != null && value instanceof Number) ? (Number)value : null;
107    }
108
109    @Override
110    public Pattern getValueAsPattern() {
111        Object value = getValue();
112        return (value != null && value instanceof Pattern) ? (Pattern)value : null;
113    }
114
115    @Override
116    public Double getValueAsDouble() {
117        Object value = getValue();
118        return (value != null && value instanceof Double) ? (Double)value : null;
119    }
120
121    @Override
122    public UUID getValueAsUuid() {
123        Object value = getValue();
124        if (value instanceof UUID) return (UUID)value;
125        if (value instanceof String) return UUID.fromString((String)value);
126        return null;
127    }
128
129}