001
014
015 package com.liferay.portal.kernel.template;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
018 import com.liferay.portal.kernel.util.Validator;
019
020 import java.io.IOException;
021 import java.io.ObjectInput;
022 import java.io.ObjectOutput;
023 import java.io.Reader;
024
025
028 public class StringTemplateResource implements TemplateResource {
029
030
034 public StringTemplateResource() {
035 }
036
037 public StringTemplateResource(String templateId, String templateContent) {
038 if (Validator.isNull(templateId)) {
039 throw new IllegalArgumentException("Template ID is null");
040 }
041
042 if (Validator.isNull(templateContent)) {
043 throw new IllegalArgumentException("Template content is null");
044 }
045
046 _templateId = templateId;
047 _templateContent = templateContent;
048 }
049
050 @Override
051 public boolean equals(Object obj) {
052 if (this == obj) {
053 return true;
054 }
055
056 if (!(obj instanceof StringTemplateResource)) {
057 return false;
058 }
059
060 StringTemplateResource stringTemplateResource =
061 (StringTemplateResource)obj;
062
063 if (_templateId.equals(stringTemplateResource._templateId) &&
064 _templateContent.equals(stringTemplateResource._templateContent)) {
065
066 return true;
067 }
068
069 return false;
070 }
071
072 public String getContent() {
073 return _templateContent;
074 }
075
076 public long getLastModified() {
077 return _lastModified;
078 }
079
080 public Reader getReader() {
081 return new UnsyncStringReader(_templateContent);
082 }
083
084 public String getTemplateId() {
085 return _templateId;
086 }
087
088 @Override
089 public int hashCode() {
090 return _templateId.hashCode() * 11 + _templateContent.hashCode();
091 }
092
093 public void readExternal(ObjectInput objectInput) throws IOException {
094 _lastModified = objectInput.readLong();
095 _templateContent = objectInput.readUTF();
096 _templateId = objectInput.readUTF();
097 }
098
099 public void writeExternal(ObjectOutput objectOutput) throws IOException {
100 objectOutput.writeLong(_lastModified);
101 objectOutput.writeUTF(_templateContent);
102 objectOutput.writeUTF(_templateId);
103 }
104
105 private long _lastModified = System.currentTimeMillis();
106 private String _templateContent;
107 private String _templateId;
108
109 }