package com.coherentlogic.fred.client.core.factories;
import java.util.Map;
import org.springframework.web.client.RestTemplate;
import com.coherentlogic.fred.client.core.builders.QueryBuilder;
/**
* A factory which is used for creating instances of QueryBuilder.
* <p>
* Since the QueryBuilder is not thread-safe, it cannot be set as a member
* variable -- instead, use this factory and call {@link #getInstance()}
* whenever you need to query the FRED web services.
* <p>
* This class can be extended, for example, if you need to always return the
* same value for some query parameter -- for example, in this class we always
* return the same API key.
* <p>
* @author <a href="support@coherentlogic.com">Support</a>
*/
public class QueryBuilderFactory
implements Factory<QueryBuilder> {
private final RestTemplate restTemplate;
private final String uri;
private final Map<String, Object> cache;
private final String apiKey;
public QueryBuilderFactory (
RestTemplate restTemplate,
String uri,
String apiKey
) {
this (restTemplate, uri, null, apiKey);
}
public QueryBuilderFactory (
RestTemplate restTemplate,
String uri,
Map<String, Object> cache,
String apiKey
) {
this.restTemplate = restTemplate;
this.uri = uri;
this.cache = cache;
this.apiKey = apiKey;
}
@Override
public QueryBuilder getInstance() {
QueryBuilder result = null;
if (cache == null )
result = new QueryBuilder (restTemplate, uri);
else
result = new QueryBuilder (restTemplate, uri, cache);
result.setApiKey(apiKey);
return result;
}
}