package org.yamcs.xtce;


import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

import org.yamcs.xtce.xml.XtceAliasSet;


/**
 * Keeps a list of parameters with corresponding indexes to be able to retrieve them in any namespace.
 * 
 * Currently the name is case sensitive while aliases are not. Is this the correct behaviour???
 * @author nm
 *
 */
public class NamedDescriptionIndex<T extends NameDescription> implements Serializable{
    private static final long serialVersionUID = 3L;
    
    private LinkedHashMap<String, LinkedHashMap<String,T>> aliasIndex =new LinkedHashMap<String, LinkedHashMap<String,T>>();
    private LinkedHashMap<String,T> index =new LinkedHashMap<String,T>();
    
    
    public void add(T o) {
        XtceAliasSet aliases=o.getAliasSet();
        if(aliases!=null) {
            for(String ns:aliases.getNamespaces()) {
                LinkedHashMap<String, T> m=aliasIndex.get(ns);
                if(m==null) {
                    m=new LinkedHashMap<String, T>();
                    aliasIndex.put(ns, m);
                }
                m.put(aliases.getAlias(ns).toUpperCase(), o);
            }
        }
        
        if (o.getQualifiedName() != null) {
            index.put(o.getQualifiedName(), o);
        } else {
            index.put(o.getName(), o); // Happens for Derived Values
        }
    }
   
    /**
     * returns the object based on its qualified name
     */
    public T get(String qualifiedName) {
        return index.get(qualifiedName);
    }
    
    /**
     * returns the object in namespace
     * @param name
     * @param nameSpace
     * @return
     */
    public T get(String nameSpace, String name) {
        Map<String, T>m=aliasIndex.get(nameSpace);
        if (m!=null) {
            return m.get(name.toUpperCase());
        } else {
            return null;
        }
    }
    /**
     *  returns a collection of all the objects (parameters) in the index
     * @return
     */
    public Collection<T> getObjects() {
        return index.values();
    }

    /**
     * 
     * @return number of objects in index
     */
    public int size() {
        return index.size();
    }
}
@@ -77,16 +77,43 @@
 ection;%0A
+import java.util.Iterator;%0A
 import j
@@ -402,17 +402,16 @@
  behavio
-u
 r???%0A * 
@@ -513,16 +513,29 @@
 alizable
+, Iterable%3CT%3E
 %7B%0A    pr
@@ -2261,10 +2261,109 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public Iterator%3CT%3E iterator() %7B%0A        return index.values().iterator();%0A    %7D
 %0A%7D

make the index iterable