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 the event ID in dotted hex 024 */ 025 public String getEventName(EventID eventID) { 026 var tag = tagmgr.getBySystemName(OlcbConstants.tagPrefix+eventID.toShortString()); 027 if (tag == null) return eventID.toShortString(); 028 var name = tag.getUserName(); 029 if (name == null || name.isEmpty()) return eventID.toShortString(); 030 return name; 031 } 032 033 /** 034 * @param eventID The EventID being searched for 035 * @return true if there is an associated name 036 */ 037 public boolean hasEventName(EventID eventID) { 038 var tag = tagmgr.getBySystemName(OlcbConstants.tagPrefix + eventID.toShortString()); 039 if (tag == null) return false; 040 var name = tag.getUserName(); 041 if (name == null || name.isEmpty()) return false; 042 return true; 043 } 044 045 /** 046 * @param name The event name being searched for 047 * @return The EventID associated with that name or an event ID constructed from the input 048 */ 049 public EventID getEventID(String name) { 050 var tag = tagmgr.getByUserName(name); 051 if (tag == null) return new EventID(name); 052 053 var eid = tag.getSystemName().substring(OlcbConstants.tagPrefix.length()); 054 return new EventID(eid); 055 } 056 057 /** 058 * @param name The event name being searched for 059 * @return true if an EventID is associated with that name 060 */ 061 public boolean hasEventID(String name) { 062 var tag = tagmgr.getByUserName(name); 063 if (tag == null) return false; 064 065 var eid = tag.getSystemName().substring(OlcbConstants.tagPrefix.length()); 066 return eid != null; 067 } 068 069 /** 070 * Create a new name to/from EventID association 071 * @param eventID associated EventID 072 * @param name associated name 073 */ 074 public void addMatch(EventID eventID, String name) { 075 tagmgr.provideIdTag(OlcbConstants.tagPrefix+eventID.toShortString()) 076 .setUserName(name); 077 } 078 079 public java.util.Set<EventID> getMatches() { 080 var set = new java.util.HashSet<EventID>(); 081 for (var tag: tagmgr.getNamedBeanSet()) { 082 if (tag.getSystemName().startsWith(OlcbConstants.tagPrefix)) { 083 var eid = tag.getSystemName().substring(OlcbConstants.tagPrefix.length()); 084 set.add(new EventID(eid)); 085 } 086 } 087 return set; 088 089 } 090 // private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(OlcbEventNameStore.class); 091 092}