001/*
002 * (C) Copyright 2006-2007 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id: LiteralImpl.java 20796 2007-06-19 09:52:03Z sfermigier $
020 */
021
022package org.nuxeo.ecm.platform.relations.api.impl;
023
024import org.nuxeo.ecm.platform.relations.api.Literal;
025import org.nuxeo.ecm.platform.relations.api.NodeType;
026import org.nuxeo.ecm.platform.relations.api.exceptions.InvalidLiteralException;
027
028/**
029 * Literal nodes.
030 *
031 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
032 */
033public class LiteralImpl extends AbstractNode implements Literal {
034
035    private static final long serialVersionUID = 1L;
036
037    protected String value;
038
039    protected String language;
040
041    protected String type;
042
043    public LiteralImpl(String value) {
044        // TODO: maybe handle encoding problems here
045        this.value = value;
046    }
047
048    @Override
049    public NodeType getNodeType() {
050        return NodeType.LITERAL;
051    }
052
053    @Override
054    public boolean isLiteral() {
055        return true;
056    }
057
058    @Override
059    public String getLanguage() {
060        return language;
061    }
062
063    @Override
064    public void setLanguage(String language) {
065        if (type != null) {
066            throw new InvalidLiteralException("Cannot set language, type already set");
067        }
068        this.language = language;
069    }
070
071    @Override
072    public String getType() {
073        return type;
074    }
075
076    @Override
077    public void setType(String type) {
078        if (language != null) {
079            throw new InvalidLiteralException("Cannot set type, language already set");
080        }
081        this.type = type;
082    }
083
084    @Override
085    public String getValue() {
086        return value;
087    }
088
089    @Override
090    public void setValue(String value) {
091        this.value = value;
092    }
093
094    @Override
095    public String toString() {
096        String str;
097        if (type != null) {
098            str = String.format("%s('%s^^%s')", getClass().getSimpleName(), value, type);
099        } else if (language != null) {
100            str = String.format("%s('%s@%s')", getClass().getSimpleName(), value, language);
101        } else {
102            str = String.format("%s('%s')", getClass().getSimpleName(), value);
103        }
104        return str;
105    }
106
107    @Override
108    public boolean equals(Object other) {
109        if (this == other) {
110            return true;
111        }
112        if (!(other instanceof LiteralImpl)) {
113            return false;
114        }
115        LiteralImpl otherLiteral = (LiteralImpl) other;
116        // XXX AT: will fail on different lit/language
117        // boolean res = ((getLanguage() == otherLiteral.getLanguage())
118        // && (getType() == otherLiteral.getType()) && (getValue()
119        // .equals(otherLiteral.getValue())));
120        boolean sameLanguage = language == null ? otherLiteral.language == null
121                : language.equals(otherLiteral.language);
122        boolean sameType = type == null ? otherLiteral.type == null : type.equals(otherLiteral.type);
123        boolean sameValue = value == null ? otherLiteral.value == null : value.equals(otherLiteral.value);
124        return sameLanguage && sameType && sameValue;
125    }
126
127    @Override
128    public int hashCode() {
129        int result = 17;
130        result = 37 * result + (language == null ? 0 : language.hashCode());
131        result = 37 * result + (type == null ? 0 : type.hashCode());
132        result = 37 * result + (value == null ? 0 : value.hashCode());
133        return result;
134    }
135
136}