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.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    }