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.search;
016    
017    import com.liferay.portal.kernel.util.Validator;
018    
019    import java.util.Arrays;
020    import java.util.Collections;
021    import java.util.List;
022    import java.util.regex.Pattern;
023    
024    /**
025     * @author Josef Sustacek
026     */
027    public class KeywordsSuggestionHolder {
028    
029            public KeywordsSuggestionHolder(
030                    String suggestedKeywords, String originalKeywords) {
031    
032                    this(suggestedKeywords, originalKeywords, _KEYWORDS_DELIMETER_REGEXP);
033            }
034    
035            public KeywordsSuggestionHolder(
036                    String suggestedKeywords, String originalKeywords,
037                    String keywordsDelimiterRegexp) {
038    
039                    Pattern keywordsDelimiterRegexpPattern = Pattern.compile(
040                            keywordsDelimiterRegexp);
041    
042                    if (Validator.isNull(suggestedKeywords)) {
043                            _suggestedKeywords = Collections.emptyList();
044                    }
045                    else {
046                            _suggestedKeywords = Arrays.asList(
047                                    keywordsDelimiterRegexpPattern.split(suggestedKeywords));
048                    }
049    
050                    if (Validator.isNull(originalKeywords)) {
051                            _originalKeywords = Collections.emptyList();
052                    }
053                    else {
054                            _originalKeywords = Arrays.asList(
055                                    keywordsDelimiterRegexpPattern.split(originalKeywords));
056                    }
057            }
058    
059            public List<String> getSuggestedKeywords() {
060                    return _suggestedKeywords;
061            }
062    
063            public boolean hasChanged(String suggestedKeyword) {
064                    return !_originalKeywords.contains(suggestedKeyword);
065            }
066    
067            private static final String _KEYWORDS_DELIMETER_REGEXP = "[ ]+";
068    
069            private List<String> _originalKeywords;
070            private List<String> _suggestedKeywords;
071    
072    }