001package jmri.jmrix.openlcb;
002
003import jmri.IdTagManager;
004import jmri.InstanceManager;
005
006import org.openlcb.EventID;
007import org.openlcb.EventNameStore;
008
009/**
010 * JMRI's implementation of part of the OpenLcb EventNameStore interface.
011 *
012 * @author Bob Jacobsen Copyright (C) 2024
013 */
014public final class OlcbEventNameStore implements EventNameStore {
015
016    public OlcbEventNameStore() {
017    }
018
019    final IdTagManager tagmgr = InstanceManager.getDefault(IdTagManager.class); // only one of these
020    
021    /**
022     * @param eventID The EventID being searched for
023     * @return The name associated with that EventID or null
024     */
025    public String getEventName(EventID eventID) {
026        var tag = tagmgr.getBySystemName(OlcbConstants.tagPrefix+eventID.toShortString());
027        if (tag == null) return null;
028        var name = tag.getUserName();
029        if (name == null || name.isEmpty()) return null;
030        return name;
031    }
032    
033    /**
034     * @param name The event name being searched for
035     * @return The EventID associated with that name
036     */
037    public EventID getEventID(String name) {
038        var tag = tagmgr.getByUserName(name);
039        if (tag == null) return null;
040        
041        var eid = tag.getSystemName().substring(OlcbConstants.tagPrefix.length());
042        return new EventID(eid);
043    }
044        
045    /**
046     * Create a new name to/from EventID association
047     * @param eventID associated EventID
048     * @param name  associated name
049     */
050    public void addMatch(EventID eventID, String name) {
051        tagmgr.provideIdTag(OlcbConstants.tagPrefix+eventID.toShortString())
052            .setUserName(name);
053    }
054    
055    public java.util.Set<EventID> getMatches() {
056        var set = new java.util.HashSet<EventID>();
057        for (var tag: tagmgr.getNamedBeanSet()) {
058            if (tag.getSystemName().startsWith(OlcbConstants.tagPrefix)) {
059                var eid = tag.getSystemName().substring(OlcbConstants.tagPrefix.length());
060                set.add(new EventID(eid));
061            }
062        }
063        return set;
064        
065    }
066    // private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(OlcbEventNameStore.class);
067
068}