package com.inari.firefly.control.maintenance;

import java.util.Iterator;

import com.inari.commons.lang.list.DynArray;
import com.inari.firefly.component.Component;
import com.inari.firefly.component.ComponentId;

public abstract class ComponentsInit extends Init {
    
    protected DynArray<ComponentId> ids;

    protected ComponentsInit( int index ) {
        super( index );
        ids = DynArray.create( ComponentId.class );
    }
    
    public final Iterator<ComponentId> getAllIds() {
        return ids.iterator();
    }
    
    public final ComponentId getComponentId( final Index index ) {
        return getComponentId( index.index() );
    }
    
    public final ComponentId getComponentId( int index ) {
        if ( !ids.contains( index ) ) {
            return null;
        }
        
        return ids.get( index );
    }
    
    public final <C extends Component> C getComponent( final Index index ) {
        return getComponent( index.index() );
    }

    public final <C extends Component> C getComponent( int index ) {
        final ComponentId id = getComponentId( index );
        if ( id == null ) {
            return null;
        }
        
        return context.getComponent( id );
    }

    protected void cleanup() {
        for ( ComponentId id : ids ) {
            context.deleteComponent( id );
        }
        
        ids.clear();
    }
    
    public interface Index {
        int index();
    }

}

@@ -69,16 +69,117 @@
 rator;%0A%0A
+import com.inari.commons.lang.indexed.Indexed;%0Aimport com.inari.commons.lang.indexed.IndexedTypeKey;%0A
 import c
@@ -215,16 +215,16 @@
 nArray;%0A
-
 import c
@@ -622,36 +622,32 @@
 erator();%0A    %7D%0A
-    
 %0A    public fina
@@ -679,32 +679,34 @@
 tId( final Index
+ed
  index ) %7B%0A     
@@ -1012,16 +1012,18 @@
 al Index
+ed
  index )
@@ -1319,24 +1319,333 @@
  id );%0A    %7D
+%0A    %0A    protected void set( final Indexed index, IndexedTypeKey typeKey, int componentId ) %7B%0A        set( index.index(), typeKey, componentId );%0A    %7D%0A    %0A    protected void set( int index, IndexedTypeKey typeKey, int componentId ) %7B%0A        ids.set( index, new ComponentId( typeKey, componentId ) );%0A    %7D
 %0A%0A    protec
@@ -1797,68 +1797,12 @@
   %7D%0A
+
     
-%0A    public interface Index %7B%0A        int index();%0A    %7D
 %0A%0A%7D%0A

use Indexed instead of Index