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:[email protected]">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 public NodeType getNodeType() { 049 return NodeType.LITERAL; 050 } 051 052 @Override 053 public boolean isLiteral() { 054 return true; 055 } 056 057 public String getLanguage() { 058 return language; 059 } 060 061 public void setLanguage(String language) { 062 if (type != null) { 063 throw new InvalidLiteralException("Cannot set language, type already set"); 064 } 065 this.language = language; 066 } 067 068 public String getType() { 069 return type; 070 } 071 072 public void setType(String type) { 073 if (language != null) { 074 throw new InvalidLiteralException("Cannot set type, language already set"); 075 } 076 this.type = type; 077 } 078 079 public String getValue() { 080 return value; 081 } 082 083 public void setValue(String value) { 084 this.value = value; 085 } 086 087 @Override 088 public String toString() { 089 String str; 090 if (type != null) { 091 str = String.format("%s('%s^^%s')", getClass().getSimpleName(), value, type); 092 } else if (language != null) { 093 str = String.format("%s('%s@%s')", getClass().getSimpleName(), value, language); 094 } else { 095 str = String.format("%s('%s')", getClass().getSimpleName(), value); 096 } 097 return str; 098 } 099 100 @Override 101 public boolean equals(Object other) { 102 if (this == other) { 103 return true; 104 } 105 if (!(other instanceof LiteralImpl)) { 106 return false; 107 } 108 LiteralImpl otherLiteral = (LiteralImpl) other; 109 // XXX AT: will fail on different lit/language 110 // boolean res = ((getLanguage() == otherLiteral.getLanguage()) 111 // && (getType() == otherLiteral.getType()) && (getValue() 112 // .equals(otherLiteral.getValue()))); 113 boolean sameLanguage = language == null ? otherLiteral.language == null 114 : language.equals(otherLiteral.language); 115 boolean sameType = type == null ? otherLiteral.type == null : type.equals(otherLiteral.type); 116 boolean sameValue = value == null ? otherLiteral.value == null : value.equals(otherLiteral.value); 117 return sameLanguage && sameType && sameValue; 118 } 119 120 @Override 121 public int hashCode() { 122 int result = 17; 123 result = 37 * result + (language == null ? 0 : language.hashCode()); 124 result = 37 * result + (type == null ? 0 : type.hashCode()); 125 result = 37 * result + (value == null ? 0 : value.hashCode()); 126 return result; 127 } 128 129}