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.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    
021    import java.util.List;
022    import java.util.Map;
023    
024    /**
025     * @author Daniela Zapata
026     * @author David Gonzalez
027     */
028    public class DefaultCollatorImpl implements Collator {
029    
030            @Override
031            public String collate(
032                    Map<String, List<String>> suggestionsMap, List<String> tokens) {
033    
034                    StringBundler sb = new StringBundler(tokens.size() * 2);
035    
036                    for (String token : tokens) {
037                            List<String> suggestions = suggestionsMap.get(token);
038    
039                            if ((suggestions != null) && !suggestions.isEmpty()) {
040                                    String suggestion = suggestions.get(0);
041    
042                                    if (Character.isUpperCase(token.charAt(0))) {
043                                            suggestion = suggestion.substring(
044                                                    0, 1).toUpperCase().concat(suggestion.substring(1));
045                                    }
046    
047                                    sb.append(suggestion);
048                                    sb.append(StringPool.SPACE);
049                            }
050                            else {
051                                    sb.append(token);
052                                    sb.append(StringPool.SPACE);
053                            }
054                    }
055    
056                    String collatedValue = sb.toString();
057    
058                    return collatedValue.trim();
059            }
060    
061    }