1// Copyright (C) 2014 Google Inc. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15#ifndef I18N_ADDRESSINPUT_ONDEMAND_SUPPLIER_H_ 16#define I18N_ADDRESSINPUT_ONDEMAND_SUPPLIER_H_ 17 18#include <libaddressinput/callback.h> 19#include <libaddressinput/supplier.h> 20#include <libaddressinput/util/basictypes.h> 21#include <libaddressinput/util/scoped_ptr.h> 22 23#include <map> 24#include <string> 25 26namespace i18n { 27namespace addressinput { 28 29class LookupKey; 30class Retriever; 31class Rule; 32class Source; 33class Storage; 34 35// An implementation of the Supplier interface that owns a Retriever object, 36// through which it loads address metadata as needed, creating Rule objects and 37// caching these. 38// 39// When using an OndemandSupplier, address validation will benefit from address 40// metadata server synonym resolution, because the server will be contacted for 41// every new LookupKey (ie. every LookupKey that isn't on canonical form and 42// isn't already cached). 43// 44// The maximum size of this cache is naturally limited to the amount of data 45// available from the data server. (Currently this is less than 12,000 items of 46// in total less than 2 MB of JSON data.) 47class OndemandSupplier : public Supplier { 48 public: 49 // Takes ownership of |source| and |storage|. 50 OndemandSupplier(const Source* source, Storage* storage); 51 virtual ~OndemandSupplier(); 52 53 // Loads the metadata needed for |lookup_key|, then calls |supplied|. 54 virtual void Supply(const LookupKey& lookup_key, const Callback& supplied); 55 56 private: 57 const scoped_ptr<const Retriever> retriever_; 58 std::map<std::string, const Rule*> rule_cache_; 59 60 DISALLOW_COPY_AND_ASSIGN(OndemandSupplier); 61}; 62 63} // namespace addressinput 64} // namespace i18n 65 66#endif // I18N_ADDRESSINPUT_ONDEMAND_SUPPLIER_H_ 67