com.radeonsys.data.querystore.support.AbstractHashMapQueryStore.java Source code

Java tutorial

Introduction

Here is the source code for com.radeonsys.data.querystore.support.AbstractHashMapQueryStore.java

Source

/*
 * @(#)AbstractHashMapQueryStore.java            Jun 7, 2012 10:00:51 PM
 *
 * Copyright 2012 Radeon Systems
 *
 *    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 com.radeonsys.data.querystore.support;

import java.util.HashMap;
import java.util.Map;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.radeonsys.data.querystore.QueryNotFoundException;
import com.radeonsys.data.querystore.QueryStore;

/**
 * An abstract implementation of {@link QueryStore} which uses a {@link HashMap} to 
 * store query definitions.
 * 
 * <p>This class implements the {@link #getQueryInternal(String)}
 * method which looks up the hash-map for a query with the specified name and
 * returns the associated query string.</p>
 * 
 * <p>Subclasses who implement {@link QueryStore#getQuery(String)} method must use 
 * the {@link #getQueryInternal(String)} to retrieve a query from the hash map.
 * They may choose to perform any processing operations before or after
 * calling the method to implement specialized behavior.</p>
 *
 * <p>Concrete subclasses must call the {@link #addQuery(String, String)} to
 * add queries into the hash-map.</p>
 * 
 * @see QueryStore
 * @see HashMap
 * 
 * @author oddjobsman
 * @since 1.0
 */
public abstract class AbstractHashMapQueryStore implements QueryStore {

    /**
     * The initial map size that will be used
     */
    private static final int INITIAL_MAP_SIZE = 30;

    /**
     * The hash map that will hold the query definitions
     */
    private final Map<String, String> queryMap;

    /**
     * Creates a new instance of {@code AbstractHashMapQueryStore}
     */
    public AbstractHashMapQueryStore() {
        queryMap = Maps.newHashMapWithExpectedSize(INITIAL_MAP_SIZE);
    }

    /**
     * Returns the query string from a query in this query store
     * with the specified name
     * 
     * @param nameOfQuery   the name of the query which is required
     * 
     * @return   the query string associated with the query that has the
     *          specified query name
     * 
     * @throws IllegalArgumentException
     *          if the query name was not specified
     * @throws QueryNotFoundException
     *          if there was no query found with the specified name
     */
    protected final String getQueryInternal(String nameOfQuery) {
        if (!containsQuery(nameOfQuery))
            throw new QueryNotFoundException(nameOfQuery);
        return queryMap.get(nameOfQuery);
    }

    /**
     * Adds a query into this query store with the specified name and with the
     * specified query string
     *  
     * @param queryName      the name of the query to ad
     * @param queryString   the query string to be associated with the query
     * 
     * @throws IllegalArgumentException
     *          if query name or query string was not specified
     */
    protected final void addQuery(String queryName, String queryString) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(queryName), "Must specify valid name for query");
        Preconditions.checkArgument(!Strings.isNullOrEmpty(queryString), "Must specify valid query string");

        queryMap.put(queryName, queryString);
    }

    /**
     * Returns whether or not a query with the specified name is
     * present in this query store
     * 
     * @param queryName      the name of the query 
     * 
     * @return {@code true} if a query with the specified name is
     *          present in this query store; {@code false} if not.
     * 
     * @throws IllegalArgumentException
     *          if the query name was not specified
     */
    public boolean containsQuery(String queryName) {
        Preconditions.checkArgument(!Strings.isNullOrEmpty(queryName));

        return queryMap.containsKey(queryName);
    }
}