001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.search;
016    
017    import com.liferay.portal.kernel.search.Collator;
018    import com.liferay.portal.kernel.util.StringBundler;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    
022    import java.util.List;
023    import java.util.Map;
024    
025    /**
026     * @author Daniela Zapata
027     * @author David Gonzalez
028     */
029    public class DefaultCollatorImpl implements Collator {
030    
031            @Override
032            public String collate(
033                    Map<String, List<String>> suggestionsMap, List<String> tokens) {
034    
035                    StringBundler sb = new StringBundler(tokens.size() * 2);
036    
037                    for (String token : tokens) {
038                            List<String> suggestions = suggestionsMap.get(token);
039    
040                            if ((suggestions != null) && !suggestions.isEmpty()) {
041                                    String suggestion = suggestions.get(0);
042    
043                                    if (Character.isUpperCase(token.charAt(0))) {
044                                            suggestion = StringUtil.toUpperCase(
045                                                    suggestion.substring(0, 1)).concat(
046                                                            suggestion.substring(1));
047                                    }
048    
049                                    sb.append(suggestion);
050                                    sb.append(StringPool.SPACE);
051                            }
052                            else {
053                                    sb.append(token);
054                                    sb.append(StringPool.SPACE);
055                            }
056                    }
057    
058                    String collatedValue = sb.toString();
059    
060                    return collatedValue.trim();
061            }
062    
063    }