package aya;

import java.util.ArrayList;
import java.util.HashMap;

import aya.entities.operations.OpDoc;
import aya.obj.Obj;
import aya.obj.list.GenericList;
import aya.obj.list.List;

public class OperationDocs {
	private static HashMap<String, OpDoc> _docs = new HashMap<String, OpDoc>();
	private static HashMap<Integer, List> _allOpsCache;
	
	public static void add(OpDoc doc) {
		_docs.put(doc.opName(), doc);
	}
	
	public static OpDoc get(String opName) {
		return _docs.get(opName);
	}
	
	/** Given the type of operator, return a list of dicts containing
	 * information about them. Cache the list of dicts
	 * 
	 * @param type
	 * @return
	 */
	public static List asDicts(int type) {
		if (_allOpsCache == null) _allOpsCache = new HashMap<Integer, List>();
		
		if (_allOpsCache.get(type) == null) {
			ArrayList<Obj> dicts  = new ArrayList<Obj>();
			for (HashMap.Entry<String, OpDoc> e : _docs.entrySet()) {
				if (e.getValue().type() == type) {
					dicts.add(e.getValue().toDict());
				}
			}
			List l = new GenericList(dicts);
			_allOpsCache.put(type, l);
			return l;
		} else {
			return _allOpsCache.get(type);
		}
	}

	public static ArrayList<String> getAllOpDescriptions() {
		ArrayList<String> out = new ArrayList<String>();
		for (HashMap.Entry<String, OpDoc> e : _docs.entrySet()) {
			out.add(e.getValue().toString());
		}
		return out;
	}
}

@@ -352,19 +352,69 @@
 Cache;%0D%0A
+%09private static OpDoc NONE = new OpDoc(' ', %22%22);%0D%0A
 %09%0D%0A
-
 %09public 
@@ -528,22 +528,27 @@
 e) %7B%0D%0A%09%09
-return
+OpDoc doc =
  _docs.g
@@ -556,24 +556,96 @@
 t(opName);%0D%0A
+%09%09if (doc == null) %7B%0D%0A%09%09%09return NONE;%0D%0A%09%09%7D else %7B%0D%0A%09%09%09return doc;%0D%0A%09%09%7D%0D%0A
 %09%7D%0D%0A%09%0D%0A%09/** 

fix #63