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.servlet.taglib.util;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.Mergeable;
019    import com.liferay.portal.kernel.util.StringBundler;
020    
021    import java.io.Serializable;
022    
023    import java.util.HashMap;
024    import java.util.HashSet;
025    import java.util.Map;
026    import java.util.Set;
027    
028    /**
029     * @author Shuyang Zhou
030     */
031    public class OutputData implements Mergeable<OutputData>, Serializable {
032    
033            public void addData(String outputKey, String webKey, StringBundler sb) {
034                    DataKey dataKey = new DataKey(outputKey, webKey);
035    
036                    StringBundler mergedSB = _dataMap.get(dataKey);
037    
038                    if (mergedSB == null) {
039                            _dataMap.put(dataKey, sb);
040                    }
041                    else {
042                            mergedSB.append(sb);
043                    }
044            }
045    
046            public boolean addOutputKey(String outputKey) {
047                    return _outputKeys.add(outputKey);
048            }
049    
050            public StringBundler getData(String outputKey, String webKey) {
051                    DataKey dataKey = new DataKey(outputKey, webKey);
052    
053                    return _dataMap.get(dataKey);
054            }
055    
056            public StringBundler getMergedData(String webKey) {
057                    StringBundler mergedSB = null;
058    
059                    for (Map.Entry<DataKey, StringBundler> entry : _dataMap.entrySet()) {
060                            DataKey dataKey = entry.getKey();
061    
062                            if (dataKey._webKey.equals(webKey)) {
063                                    if (mergedSB == null) {
064                                            mergedSB = entry.getValue();
065                                    }
066                                    else {
067                                            mergedSB.append(entry.getValue());
068                                    }
069                            }
070                    }
071    
072                    return mergedSB;
073            }
074    
075            public Set<String> getOutputKeys() {
076                    return _outputKeys;
077            }
078    
079            @Override
080            public OutputData merge(OutputData outputData) {
081                    if ((outputData == null) || (outputData == this)) {
082                            return this;
083                    }
084    
085                    for (Map.Entry<DataKey, StringBundler> entry :
086                                    outputData._dataMap.entrySet()) {
087    
088                            DataKey dataKey = entry.getKey();
089    
090                            String outputKey = dataKey._outputKey;
091    
092                            StringBundler sb = entry.getValue();
093    
094                            if (!_outputKeys.contains(outputKey)) {
095                                    StringBundler mergedSB = _dataMap.get(dataKey);
096    
097                                    if (mergedSB == null) {
098                                            _dataMap.put(dataKey, sb);
099                                    }
100                                    else {
101                                            mergedSB.append(sb);
102                                    }
103    
104                                    if (outputData._outputKeys.contains(outputKey)) {
105                                            _outputKeys.add(outputKey);
106                                    }
107                            }
108                    }
109    
110                    return this;
111            }
112    
113            public void setData(String outputKey, String webKey, StringBundler sb) {
114                    DataKey dataKey = new DataKey(outputKey, webKey);
115    
116                    _dataMap.put(dataKey, sb);
117            }
118    
119            private static final long serialVersionUID = 1L;
120    
121            private Map<DataKey, StringBundler> _dataMap =
122                    new HashMap<DataKey, StringBundler>();
123            private Set<String> _outputKeys = new HashSet<String>();
124    
125            private class DataKey implements Serializable {
126    
127                    public DataKey(String outputKey, String webKey) {
128                            _outputKey = GetterUtil.getString(outputKey);
129                            _webKey = webKey;
130                    }
131    
132                    @Override
133                    public boolean equals(Object obj) {
134                            DataKey dataKey = (DataKey)obj;
135    
136                            if (_outputKey.equals(dataKey._outputKey) &&
137                                    _webKey.equals(dataKey._webKey)) {
138    
139                                    return true;
140                            }
141    
142                            return false;
143                    }
144    
145                    @Override
146                    public int hashCode() {
147                            return _outputKey.hashCode() * 11 + _webKey.hashCode();
148                    }
149    
150                    private static final long serialVersionUID = 1L;
151    
152                    private String _outputKey;
153                    private String _webKey;
154    
155            }
156    
157    }