All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.ehcache.search.query.QueryManagerBuilder Maven / Gradle / Ivy

Go to download

Ehcache is an open source, standards-based cache used to boost performance, offload the database and simplify scalability. Ehcache is robust, proven and full-featured and this has made it the most widely-used Java-based cache.

There is a newer version: 2.10.9.2
Show newest version
/**
 *  Copyright Terracotta, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package net.sf.ehcache.search.query;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Collection;

import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Collection;

/**
 * A {@link net.sf.ehcache.search.query.QueryManager Query Manager} builder providing methods to add caches which
 * can be queried.
 * The Query Manager instance returned by this builder can be used to execute
 * search queries expressed as Big Memory Structured Query Language (BMSQL) statements.
 *
 * @author dkumar
 */
public final class QueryManagerBuilder {

    private final Collection caches = new ArrayList();
    private final Class defaultClass;


    private QueryManagerBuilder() {
        this(getImplementationClass());
    }

    /**
     * Package-local constructor for testing purposes.
     *
     * @param implementationClass a concrete implementation of the {@link net.sf.ehcache.search.query.QueryManager Query Manager} interface
     */
    QueryManagerBuilder(Class implementationClass) {
        this.defaultClass = implementationClass;
    }

    private static Class getImplementationClass() {
        try {
            return (Class)Class.forName("net.sf.ehcache.search.parser.QueryManagerImpl");
        } catch (ClassNotFoundException e) {
            throw new CacheException(e);
        }
    }

    /**
     * Creates a new {@link net.sf.ehcache.search.query.QueryManager Query Manager} builder.
     *
     * @return this for the builder pattern
     */
    public static QueryManagerBuilder newQueryManagerBuilder() {
        return new QueryManagerBuilder();
    }

    /**
     * Adds a {@link net.sf.ehcache.Ehcache Cache} to the internal state and allows it to be used as a target in a
     * BMSQL statement's FROM clause.
     *
     * @param cache a {@link net.sf.ehcache.Ehcache cache} instance to be added
     * @return this for the builder pattern
     */
    public QueryManagerBuilder addCache(Ehcache cache) {
        this.caches.add(cache);
        return this;
    }

    /**
     * Adds all {@link net.sf.ehcache.Ehcache Caches} present in a {@link net.sf.ehcache.CacheManager Cache Manager} to
     * the internal state of the query manager.
     * The added caches can be used in a BMSQL statement's FROM clause.
     *
     * @param cacheManager a {@link net.sf.ehcache.CacheManager Cache Manager} whose
     *                     {@link net.sf.ehcache.Ehcache caches} need to be added
     * @return this for the builder pattern
     */
    public QueryManagerBuilder addAllCachesCurrentlyIn(CacheManager cacheManager) {
        for (String s : cacheManager.getCacheNames()) {
            final Ehcache cache = cacheManager.getEhcache(s);
            if (cache != null) {
                this.caches.add(cache);
            }
        }
        return this;
    }

    /**
     * Returns a new {@link net.sf.ehcache.search.query.QueryManager Query Manager} using the options supplied during the building
     * process.
     *
     * @return the fully constructed {@link net.sf.ehcache.search.query.QueryManager Query Manager}
     */
    public QueryManager build() {
        try {
            final Constructor constructor = defaultClass.getConstructor(Collection.class);
            return constructor.newInstance(this.caches);
        } catch (Exception e) {
            throw new CacheException(e);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy