001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.io.Serializable;
018    
019    /**
020     * @author Jonathan Potter
021     * @author Brian Wing Shun Chan
022     */
023    public class EscapableObject<T> implements Serializable {
024    
025            public EscapableObject(T originalValue) {
026                    this(originalValue, true);
027            }
028    
029            public EscapableObject(T originalValue, boolean escape) {
030                    _originalValue = originalValue;
031                    _escape = escape;
032            }
033    
034            public String getEscapedValue() {
035                    if (_escapedValue == null) {
036                            if (_escape) {
037                                    _escapedValue = escape(_originalValue);
038                            }
039                            else {
040                                    _escapedValue = String.valueOf(_originalValue);
041                            }
042                    }
043    
044                    return _escapedValue;
045            }
046    
047            public T getOriginalValue() {
048                    return _originalValue;
049            }
050    
051            @Override
052            public String toString() {
053                    return _originalValue.toString();
054            }
055    
056            protected String escape(T t) {
057                    return String.valueOf(t);
058            }
059    
060            private boolean _escape;
061            private String _escapedValue;
062            private T _originalValue;
063    
064    }