I'll start with giving a bit of background as there are many classes involved and I can't attach all of them because that would make this question wildly too long. For the curios they can be found at [my github repo](https://github.com/EmilyBjoerk/lsml).

I have an immutable structure called a `Chassis` which consists of 8 immutable `Component`s. Each `Component` is named by a `Location` such as `Location.LeftArm` or `Location.RightTorso`.

A `Loadout` is a particular configuration of `Item`s that are equipped on a `Chassis`. For each `Component` on the `Chassis`, the `Loadout` has a matching `ConfiguredComponent` that contains the `Item`s equipped on that component of the `Chassis`. In addition to this, each `Component` can have a number of fixed `Item`s and some `Items` are kind of fixed but can be toggled on and off (not my decision, that's the game rules). So the toggle state is a part of the `ConfiguredComponent` which is why `ConfiguredComponent` has a `getItemsFixed()` method which will return the truly fixed items + any items which have their toggle state set to enabled.

In many places in the code base I need to iterate over all items on the `Loadout` (fixed and removable). Even more frequently I'm only interested in certain types of items on the loadout. For example those that are a subclass of `Weapon` which is an `Item` or even those items that implement a particular interface not necessarily inheriting from `Item`. In other words I may want to filter on a class that is not descendant from `Item`. However all items on the loadout have `Item` as a base class.

To this end I decided upon letting `Loadout` have methods to return an `Iterable` of approriate type which I can use to iterate over `Items` of a particular type. Then I implement a form of MultiIterator that can iterate over all the `Item`s on the Loadout.

Relevant excerpt from `Loadout`:

```
public abstract class LoadoutBase<T extends ConfiguredComponentBase> {
   public Iterable<Item> items(){
      return new LoadoutIterable<Item>(this, null);
   }

   public <X> Iterable<X> items(Class<X> aClass){
      return new LoadoutIterable<X>(this, aClass);
   }
}

```

Implementation of glue class `LoadoutIterable`:

```
public class LoadoutIterable<T> implements Iterable<T>{
   private final LoadoutBase<?> loadout;
   private final Class<T> filter;

   public LoadoutIterable(LoadoutBase<?> aLoadout, Class<T> aFilter){
      loadout = aLoadout;
      filter = aFilter;
   }

   @Override
   public Iterator<T> iterator(){
      return new LoadoutItemIterator<T>(loadout, filter);
   }
}

```

And finally the `Iterator` itself (remember, `T` might not extend `Item` at all but there might be some instance of a subclass of `Item` that implements `T`):

```
public class LoadoutItemIterator<T> implements Iterator<T>{
   private static enum IterationState{
      Fixed, Equipped
   }

   private final static Location[] LOCATION_ORDER;
   private List<Item>              items;
   private IterationState          state           = IterationState.Fixed;
   private int                     index           = 0;
   private final LoadoutBase<?>    loadout;
   private final Class<T>          filter;
   private Location                currentLocation = LOCATION_ORDER[0];

   static{
      LOCATION_ORDER = new Location[Location.values().length];
      for(Location location : Location.values()){
         LOCATION_ORDER[location.ordinal()] = location;
      }
   }

   LoadoutItemIterator(LoadoutBase<?> aLoadout, Class<T> aFilter){
      loadout = aLoadout;
      filter = aFilter;
      items = loadout.getComponent(currentLocation).getItemsFixed();
   }

   LoadoutItemIterator(LoadoutBase<?> aLoadout){
      this(aLoadout, null);
   }

   @Override
   public boolean hasNext(){
      return null != getNextNonFiltered();
   }

   @Override
   public T next(){
      T ans = getNextNonFiltered();
      index++;
      return ans;
   }

   @Override
   public void remove(){
      throw new UnsupportedOperationException();
   }

   @SuppressWarnings("unchecked")
   private T getNextNonFiltered(){
      while( true ){
         if( index < items.size() ){
            Item item = items.get(index);
            if( filter == null || filter.isAssignableFrom(item.getClass()) ){
               return (T)item; // This cast is checked
            }
            index++;
         }
         else{
            index = 0;
            if( state == IterationState.Fixed ){
               state = IterationState.Equipped;
               items = loadout.getComponent(currentLocation).getItemsEquipped();
            }
            else{
               if( currentLocation.ordinal() == LOCATION_ORDER.length - 1 ){
                  return null; // End of items
               }
               currentLocation = LOCATION_ORDER[currentLocation.ordinal() + 1];
               state = IterationState.Fixed;
               items = loadout.getComponent(currentLocation).getItemsFixed();
            }
         }
      }
   }
}

```

Example usage:

```
LoadoutBase<?> loadout = ...;
for(Weapon weapon : loadout.items(Weapon.class)){
   ...;
}

```

The code works as is and I have achieved my sought for syntax for iterating over specific types of items on the loadout. However there are a few things nagging me, most prominently the unchecked cast in `LoadoutItemIterator.getNextNonFiltered`. 

I would like help with:

* How can I (if I can) get rid of the unchecked cast?
* Better naming?
* Performance of `LoadoutItemIterator`. I tried implementing using `Iterator`s to each sub list individually but that proved to be slower than this implementation. Without filtering this is about 20% faster than just slamming all the sublists into a new list and returning that.
* Other suggestions for design or solutions to the problem.

And I'm sorry for being unable to provide a runnable example, but I feel like that would be too much work for the reviewer. If you do wish to run the code you can check it out from the github repository at the top and run the gradle setup script to get the dependencies.