001    /**
002     * Copyright (c) 2000-present 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.suggest;
016    
017    import com.liferay.portal.kernel.search.SearchException;
018    import com.liferay.registry.Registry;
019    import com.liferay.registry.RegistryUtil;
020    import com.liferay.registry.ServiceTracker;
021    
022    /**
023     * @author Michael C. Han
024     */
025    public class NGramHolderBuilderUtil {
026    
027            public static NGramHolder buildNGramHolder(String input)
028                    throws SearchException {
029    
030                    return getNGramHolderBuilder().buildNGramHolder(input);
031            }
032    
033            public static NGramHolder buildNGramHolder(String input, int maxNGramLength)
034                    throws SearchException {
035    
036                    return getNGramHolderBuilder().buildNGramHolder(input, maxNGramLength);
037            }
038    
039            public static NGramHolder buildNGramHolder(
040                            String input, int nGramMinLength, int nGramMaxLength)
041                    throws SearchException {
042    
043                    return getNGramHolderBuilder().buildNGramHolder(
044                            input, nGramMinLength, nGramMaxLength);
045            }
046    
047            public static NGramHolderBuilder getNGramHolderBuilder() {
048                    if (_instance._serviceTracker.getService() == null) {
049                            return _defaultNGramHolderBuilder;
050                    }
051    
052                    return _instance._serviceTracker.getService();
053            }
054    
055            private NGramHolderBuilderUtil() {
056                    Registry registry = RegistryUtil.getRegistry();
057    
058                    _serviceTracker = registry.trackServices(NGramHolderBuilder.class);
059            }
060    
061            private static final NGramHolderBuilderUtil _instance =
062                    new NGramHolderBuilderUtil();
063    
064            private static final NGramHolderBuilder _defaultNGramHolderBuilder =
065                    new NullNGramHolderBuilder();
066    
067            private final ServiceTracker<NGramHolderBuilder, NGramHolderBuilder>
068                    _serviceTracker;
069    
070    }