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    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    }