Class ProviderStack<T>


  • public final class ProviderStack<T>
    extends Object
    This class stores a list of providers in a specific order (determined by insertion order and the OrderedService settings).

    Should be thread safe.

    Author:
    Aaron Zeckoski (azeckoski @ gmail.com)
    • Constructor Detail

      • ProviderStack

        public ProviderStack()
        Default empty constructor.

        This is mostly only useful if you are planning to add new providers later. You should probably use the other contructors.

      • ProviderStack

        public ProviderStack​(ServiceManager serviceManager,
                             Class<T> providerType)
        Construct a provider holder with all currently known providers of the given type.
        Parameters:
        serviceManager - the system service manager
        providerType - the interface type of the providers we care about
      • ProviderStack

        public ProviderStack​(T[] currentProviders)
        Construct a provider holder with a given set of providers. This will maintain the current order as long as it does not violate the rules for OrderedServices.
        Parameters:
        currentProviders - the current set of providers to register in this stack
    • Method Detail

      • addProvider

        public int addProvider​(T provider)
        Add a provider to the stack of providers. This will be placed at the bottom if the order is less than or equal to 0 or this provider is not ordered.
        Parameters:
        provider - the provider to add to the stack
        Returns:
        the position in the stack that this provider was added
      • removeProvider

        public boolean removeProvider​(int position)
        Remove a provider based on the position in the stack (starting at 0 and ending at size-1).
        Parameters:
        position - the position to remove the provider from
        Returns:
        true if the provider position was found and removed OR false if not found
      • removeProvider

        public boolean removeProvider​(T provider)
        Remove a provider by the object equality.
        Parameters:
        provider - the provider to remove from the stack
        Returns:
        true if the provider was found and removed OR false if not found
      • getProviders

        public List<T> getProviders()
        Get the list of all providers currently in the stack.

        WARNING: this should not be held onto; it should be only used for iteration and then discarded.

        Returns:
        a read-only list of all providers in the stack in the correct order, highest priority first
      • getProviderHolders

        public List<ProviderHolder<T>> getProviderHolders()
        This allows access to the provider holders.
        Returns:
        a read only list of the provider holders in the correct order
      • getIterator

        public Iterator<T> getIterator()
        Get the iterator of all providers currently in the stack. This will load up lazy so it can be held onto safely but should not be used more than once (i.e. only iterate over this completely once: stopping and continuing later is likely to produce failures).

        NOTE: This will attempt to iterate over all the valid providers in the stack, but if the provider has been garbage collected during the iteration then a NoSuchElementException will be thrown if the provider was the last one in the stack and next() is called. You should probably handle the exception by assuming this indicates all items were iterated over.

        Returns:
        an iterator over all the providers in the stack in order of priority
      • getProvider

        public T getProvider​(int position)
        Get a provider based on the position in the stack (starting at 0 and ending at size-1).
        Parameters:
        position - the position to check for the provider
        Returns:
        the provider from the position OR null if there is no provider at that position
      • getProviderHolder

        public ProviderHolder<T> getProviderHolder​(int position)
        Get the provider holder from the position in the stack if there is one.
        Parameters:
        position - the position to check for the provider
        Returns:
        the holder from the position OR null if there is no provider at that position
      • size

        public int size()
        Check the number of current providers which are available in this stack.
        Returns:
        the total number of viable providers
      • clear

        public void clear()
        Delete all providers from the current provider stack.
      • refresh

        protected List<T> refresh()
        Check to make sure all providers are refreshed and any that are no longer valid are flushed out of the list.
        Returns:
        list of valid providers