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 @Override
077 public long getLastModified() {
078 return _lastModified;
079 }
080
081 @Override
082 public Reader getReader() {
083 return new UnsyncStringReader(_templateContent);
084 }
085
086 @Override
087 public String getTemplateId() {
088 return _templateId;
089 }
090
091 @Override
092 public int hashCode() {
093 return _templateId.hashCode() * 11 + _templateContent.hashCode();
094 }
095
096 @Override
097 public void readExternal(ObjectInput objectInput) throws IOException {
098 _lastModified = objectInput.readLong();
099 _templateContent = objectInput.readUTF();
100 _templateId = objectInput.readUTF();
101 }
102
103 @Override
104 public void writeExternal(ObjectOutput objectOutput) throws IOException {
105 objectOutput.writeLong(_lastModified);
106 objectOutput.writeUTF(_templateContent);
107 objectOutput.writeUTF(_templateId);
108 }
109
110 private long _lastModified = System.currentTimeMillis();
111 private String _templateContent;
112 private String _templateId;
113
114 }