001package jmri.util; 002 003import javax.annotation.CheckForNull; 004import javax.annotation.Nonnull; 005import jmri.InstanceManager; 006import jmri.SystemConnectionMemo; 007 008/** 009 * Common utility method for returning the System Connection Name from the 010 * System Name Prefix 011 * 012 * @author Kevin Dickerson Copyright 2010 013 */ 014public class ConnectionNameFromSystemName { 015 016 /** 017 * Locates the connected systems name from a given prefix. 018 * 019 * @param prefix the system prefix 020 * @return The Connection System Name or null if no connection has the given 021 * prefix 022 */ 023 @CheckForNull 024 static public String getConnectionName(@Nonnull String prefix) { 025 SystemConnectionMemo memo = getSystemConnectionMemoFromSystemPrefix(prefix); 026 if (memo != null) { 027 return memo.getUserName(); 028 } 029 return null; 030 } 031 032 /** 033 * Locates the connected systems prefix from a given System name. 034 * 035 * @param name The user name 036 * @return The system prefix or null if no connection has the given name 037 */ 038 @CheckForNull 039 static public String getPrefixFromName(@Nonnull String name) { 040 SystemConnectionMemo memo = getSystemConnectionMemoFromUserName(name); 041 if (memo != null) { 042 return memo.getSystemPrefix(); 043 } 044 return null; 045 } 046 047 /** 048 * Get the {@link SystemConnectionMemo} for a given system 049 * prefix. 050 * 051 * @param systemPrefix the system prefix 052 * @return the SystemConnectionMemo or null if no memo exists 053 */ 054 @CheckForNull 055 static public SystemConnectionMemo getSystemConnectionMemoFromSystemPrefix(@Nonnull String systemPrefix) { 056 for (SystemConnectionMemo memo : InstanceManager.getList(SystemConnectionMemo.class)) { 057 if (memo.getSystemPrefix().equals(systemPrefix)) { 058 return memo; 059 } 060 } 061 return null; 062 } 063 064 /** 065 * Get the {@link SystemConnectionMemo} for a given user name. 066 * 067 * @param userName the user name 068 * @return the SystemConnectionMemo or null if no memo exists 069 */ 070 @CheckForNull 071 static public SystemConnectionMemo getSystemConnectionMemoFromUserName(@Nonnull String userName) { 072 for (SystemConnectionMemo memo : InstanceManager.getList(SystemConnectionMemo.class)) { 073 if (memo.getUserName().equals(userName)) { 074 return memo; 075 } 076 } 077 return null; 078 } 079 080}