001
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
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 final Map<DataKey, StringBundler> _dataMap = new HashMap<>();
122 private final Set<String> _outputKeys = new HashSet<>();
123
124 private class DataKey implements Serializable {
125
126 public DataKey(String outputKey, String webKey) {
127 _outputKey = GetterUtil.getString(outputKey);
128 _webKey = webKey;
129 }
130
131 @Override
132 public boolean equals(Object obj) {
133 DataKey dataKey = (DataKey)obj;
134
135 if (_outputKey.equals(dataKey._outputKey) &&
136 _webKey.equals(dataKey._webKey)) {
137
138 return true;
139 }
140
141 return false;
142 }
143
144 @Override
145 public int hashCode() {
146 return _outputKey.hashCode() * 11 + _webKey.hashCode();
147 }
148
149 private static final long serialVersionUID = 1L;
150
151 private final String _outputKey;
152 private final String _webKey;
153
154 }
155
156 }