package org.subalternproductions.seepResource.dsl.style { import org.subalternproductions.util.collections.Hash; import org.subalternproductions.util.collections.IIterator; /** * Manages hash & array of style, assigns (runtime) unique indies to tokens */ public class StyleMap { public function StyleMap() { } private var _tokens:Array = []; private var _styleHash:Hash = new Hash(ISStyle); /** * Adds the token (unless it alread exists) and returns the token ID */ public function add(style:ISStyle):int { var name:String = style.name; if (_styleHash.getItem(name)) { var index:int = (_styleHash.getItem(name) as ISStyle).index; // overwrite existing style _styleHash.addItem(name, style); style.index = index; return index; } _tokens.push(style); _styleHash.addItem(name, style); style.index = _tokens.length -1 return style.index; } // setToken public function styleByName(name:String):ISStyle { return _styleHash.getItem(name) as ISStyle; } /** * Returns the token id, returning */ public function index(name:String):int { return _styleHash.getItem(name) ? (_styleHash.getItem(name) as ISStyle).index : -1; } public function name(index:int):String { return (index >= 0 && index < _tokens.length) ? (_tokens[index] as ISStyle).name : null; } public function style(index:int):ISStyle { return (index >= 0 && index < _tokens.length) ? (_tokens[index] as ISStyle) : null; } public function iterator():IIterator { return _styleHash.iterator(); } } // class } // package