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

com.smartystreets.api.us_street.Batch Maven / Gradle / Ivy

There is a newer version: 3.18.3
Show newest version
package com.smartystreets.api.us_street;

import com.smartystreets.api.exceptions.BatchFullException;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Vector;

/**
 * This class contains a collection of lookups to be sent to the 
* SmartyStreets US Street API all at once. This is more efficient than sending them
* one at a time. Maximum batch size is 100. */ public class Batch { public static final int MAX_BATCH_SIZE = 100; private Map namedLookups; private Vector allLookups; public Batch() { this.namedLookups = new LinkedHashMap<>(); this.allLookups = new Vector<>(); } /** * Adds a lookup to the batch, as long as there are less than 100 lookup in the batch already. * @param newAddress * @throws BatchFullException Batch size cannot exceed 100 */ public void add(Lookup newAddress) throws BatchFullException { if (this.isFull()) throw new BatchFullException("Batch size cannot exceed " + MAX_BATCH_SIZE); this.allLookups.add(newAddress); String key = newAddress.getInputId(); if (key == null) return; this.namedLookups.put(key, newAddress); } /** * Clears the lookups stored in the batch so it can be used again.
* This helps avoid the overhead of building a new Batch object for each group of lookups. */ public void clear() { this.namedLookups.clear(); this.allLookups.clear(); } //region [ Helpers ] /** * @return The number of lookups currently in this batch. */ public int size() { return this.allLookups.size(); } public boolean isFull() { return (this.size() >= MAX_BATCH_SIZE); } public Iterator iterator() { return this.allLookups.iterator(); } //endregion //region [ Getters ] public Map getNamedLookups() { return this.namedLookups; } public Lookup get(String inputId) { return this.namedLookups.get(inputId); } public Lookup get(int inputIndex) { return this.allLookups.get(inputIndex); } public Vector getAllLookups() { return this.allLookups; } //endregion }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy