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