001    /**
002     * Copyright (c) 2000-2012 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 getMergedData(String webKey) {
051                    StringBundler mergedSB = null;
052    
053                    for (Map.Entry<DataKey, StringBundler> entry : _dataMap.entrySet()) {
054                            DataKey dataKey = entry.getKey();
055    
056                            if (dataKey._webKey.equals(webKey)) {
057                                    if (mergedSB == null) {
058                                            mergedSB = entry.getValue();
059                                    }
060                                    else {
061                                            mergedSB.append(entry.getValue());
062                                    }
063                            }
064                    }
065    
066                    return mergedSB;
067            }
068    
069            public OutputData merge(OutputData outputData) {
070                    if ((outputData == null) || (outputData == this)) {
071                            return this;
072                    }
073    
074                    for (Map.Entry<DataKey, StringBundler> entry :
075                                    outputData._dataMap.entrySet()) {
076    
077                            DataKey dataKey = entry.getKey();
078    
079                            String outputKey = dataKey._outputKey;
080    
081                            StringBundler sb = entry.getValue();
082    
083                            if (!_outputKeys.contains(outputKey)) {
084                                    StringBundler mergedSB = _dataMap.get(dataKey);
085    
086                                    if (mergedSB == null) {
087                                            _dataMap.put(dataKey, sb);
088                                    }
089                                    else {
090                                            mergedSB.append(sb);
091                                    }
092    
093                                    if (outputData._outputKeys.contains(outputKey)) {
094                                            _outputKeys.add(outputKey);
095                                    }
096                            }
097                    }
098    
099                    return this;
100            }
101    
102            private static final long serialVersionUID = 1L;
103    
104            private Map<DataKey, StringBundler> _dataMap =
105                    new HashMap<DataKey, StringBundler>();
106            private Set<String> _outputKeys = new HashSet<String>();
107    
108            private class DataKey implements Serializable {
109    
110                    public DataKey(String outputKey, String webKey) {
111                            _outputKey = GetterUtil.getString(outputKey);
112                            _webKey = webKey;
113                    }
114    
115                    @Override
116                    public boolean equals(Object obj) {
117                            DataKey dataKey = (DataKey)obj;
118    
119                            if (_outputKey.equals(dataKey._outputKey) &&
120                                    _webKey.equals(dataKey._webKey)) {
121    
122                                    return true;
123                            }
124    
125                            return false;
126                    }
127    
128                    @Override
129                    public int hashCode() {
130                            return _outputKey.hashCode() * 11 + _webKey.hashCode();
131                    }
132    
133                    private static final long serialVersionUID = 1L;
134    
135                    private String _outputKey;
136                    private String _webKey;
137    
138            }
139    
140    }