001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id: LiteralImpl.java 20796 2007-06-19 09:52:03Z sfermigier $
018 */
019
020package org.nuxeo.ecm.platform.relations.api.impl;
021
022import org.nuxeo.ecm.platform.relations.api.Literal;
023import org.nuxeo.ecm.platform.relations.api.NodeType;
024import org.nuxeo.ecm.platform.relations.api.exceptions.InvalidLiteralException;
025
026/**
027 * Literal nodes.
028 *
029 * @author <a href="mailto:at@nuxeo.com">Anahide Tchertchian</a>
030 */
031public class LiteralImpl extends AbstractNode implements Literal {
032
033    private static final long serialVersionUID = 1L;
034
035    protected String value;
036
037    protected String language;
038
039    protected String type;
040
041    public LiteralImpl(String value) {
042        // TODO: maybe handle encoding problems here
043        this.value = value;
044    }
045
046    public NodeType getNodeType() {
047        return NodeType.LITERAL;
048    }
049
050    @Override
051    public boolean isLiteral() {
052        return true;
053    }
054
055    public String getLanguage() {
056        return language;
057    }
058
059    public void setLanguage(String language) {
060        if (type != null) {
061            throw new InvalidLiteralException("Cannot set language, type already set");
062        }
063        this.language = language;
064    }
065
066    public String getType() {
067        return type;
068    }
069
070    public void setType(String type) {
071        if (language != null) {
072            throw new InvalidLiteralException("Cannot set type, language already set");
073        }
074        this.type = type;
075    }
076
077    public String getValue() {
078        return value;
079    }
080
081    public void setValue(String value) {
082        this.value = value;
083    }
084
085    @Override
086    public String toString() {
087        String str;
088        if (type != null) {
089            str = String.format("%s('%s^^%s')", getClass().getSimpleName(), value, type);
090        } else if (language != null) {
091            str = String.format("%s('%s@%s')", getClass().getSimpleName(), value, language);
092        } else {
093            str = String.format("%s('%s')", getClass().getSimpleName(), value);
094        }
095        return str;
096    }
097
098    @Override
099    public boolean equals(Object other) {
100        if (this == other) {
101            return true;
102        }
103        if (!(other instanceof LiteralImpl)) {
104            return false;
105        }
106        LiteralImpl otherLiteral = (LiteralImpl) other;
107        // XXX AT: will fail on different lit/language
108        // boolean res = ((getLanguage() == otherLiteral.getLanguage())
109        // && (getType() == otherLiteral.getType()) && (getValue()
110        // .equals(otherLiteral.getValue())));
111        boolean sameLanguage = language == null ? otherLiteral.language == null
112                : language.equals(otherLiteral.language);
113        boolean sameType = type == null ? otherLiteral.type == null : type.equals(otherLiteral.type);
114        boolean sameValue = value == null ? otherLiteral.value == null : value.equals(otherLiteral.value);
115        return sameLanguage && sameType && sameValue;
116    }
117
118    @Override
119    public int hashCode() {
120        int result = 17;
121        result = 37 * result + (language == null ? 0 : language.hashCode());
122        result = 37 * result + (type == null ? 0 : type.hashCode());
123        result = 37 * result + (value == null ? 0 : value.hashCode());
124        return result;
125    }
126
127}