Java tutorial
/* * @(#)CompositeQueryStore.java Jun 17, 2012 11:07:23 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.Set; import com.google.common.base.Preconditions; import com.google.common.base.Predicate; import com.google.common.collect.Iterables; import com.google.common.collect.Sets; import com.radeonsys.data.querystore.QueryNotFoundException; import com.radeonsys.data.querystore.QueryStore; /** * An implementation of {@link QueryStore} which looks up one or more other query stores * to find a query with a specified name. * * <p>{@link QueryStore}s can be added to this query store using the {@link #addQueryStore(QueryStore)} * method.</p> * * @see QueryStore * * @author oddjobsman * @since 1.0 */ public final class CompositeQueryStore implements QueryStore { /** * Stores the initial size of the stores */ private static final int DEFAULT_INITIAL_SIZE = 10; /** * Stores the query stores to be looked up */ private final Set<QueryStore> stores = Sets.newLinkedHashSetWithExpectedSize(DEFAULT_INITIAL_SIZE); /** * Creates a new instance of {@code CompositeQueryStore} */ public CompositeQueryStore() { // DO NOTHING } /* (non-Javadoc) * @see com.radeonsys.data.querystore.QueryStore#getQuery(java.lang.String) */ @Override public final String getQuery(String nameOfQuery) { for (QueryStore store : stores) if (store.containsQuery(nameOfQuery)) return store.getQuery(nameOfQuery); throw new QueryNotFoundException(nameOfQuery); } /* (non-Javadoc) * @see com.radeonsys.data.querystore.QueryStore#containsQuery(java.lang.String) */ @Override public boolean containsQuery(final String queryName) { return Iterables.any(stores, new Predicate<QueryStore>() { @Override public boolean apply(QueryStore store) { return store.containsQuery(queryName); } }); } /** * Adds the specified query store to this query store * * @param queryStoreToAdd reference to an instance of {@link QueryStore} to be added * * @throws IllegalArgumentException * if the specified query store was {@code null} */ public final void addQueryStore(QueryStore queryStoreToAdd) { Preconditions.checkArgument(queryStoreToAdd != null, "A valid query store must be specified."); stores.add(queryStoreToAdd); } }