001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
026     * @author Tina Tian
027     */
028    public class StringTemplateResource implements TemplateResource {
029    
030            /**
031             * The empty constructor is required by {@link java.io.Externalizable}. Do
032             * not use this for any other purpose.
033             */
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    }