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

de.lessvoid.nifty.slick2d.loaders.AbstractSlickLoaders Maven / Gradle / Ivy

There is a newer version: 1.4.3
Show newest version
package de.lessvoid.nifty.slick2d.loaders;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * The abstract implementation of a resource loader.
 *
 * @param  The type of loader used by this loader list
 * @author Martin Karing <[email protected]>
 */
public abstract class AbstractSlickLoaders implements SlickLoaders {
  /**
   * The list of font loaders that are expected to be queried.
   */
  @Nonnull
  private final List loaders;

  /**
   * Create and prepare this class.
   */
  protected AbstractSlickLoaders() {
    loaders = new ArrayList(1);
  }

  /**
   * Add a loader to the list of loaders that get queried when loading a new resource.
   *
   * @param newLoader the new font loader
   * @param order     the loader where the place the new loader on the list
   */
  @Override
  public final void addLoader(@Nonnull final T newLoader, @Nonnull final SlickAddLoaderLocation order) {
    if (checkAlreadyLoaded(newLoader)) {
      return;
    }

    switch (order) {
      case first:
        loaders.add(0, newLoader);
        break;
      case last:
      case doNotCare:
        loaders.add(newLoader);
        break;
    }
  }

  /**
   * Check if the resource loader that is about to be added already a part of the loaders list. This is done be
   * comparing the classes of the loaders.
   *
   * @param newLoader the loader that is to be added
   * @return {@code true} in case the loader is already added to the loaders list
   */
  private boolean checkAlreadyLoaded(@Nonnull final T newLoader) {
    final Class newLoaderClass = newLoader.getClass();
    for (final T currentLoader : loaders) {
      final Class currentLoaderClass = currentLoader.getClass();
      if (newLoaderClass.equals(currentLoaderClass)) {
        return true;
      }
    }
    return false;
  }

  /**
   * Get the iterator that can be used to access all the loaders.
   *
   * @return the loader iterator
   */
  @Nonnull
  protected final Iterator getLoaderIterator() {
    if (loaders.isEmpty()) {
      loadDefaultLoaders(SlickAddLoaderLocation.first);
    }

    return loaders.iterator();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy