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