001package jmri.managers;
002
003import javax.annotation.CheckForNull;
004import javax.annotation.Nonnull;
005
006import jmri.Manager;
007import jmri.Reporter;
008import jmri.ReporterManager;
009
010/**
011 * Implementation of a ReporterManager that can serve as a proxy for multiple
012 * system-specific implementations.
013 *
014 * @author Bob Jacobsen Copyright (C) 2003, 2010
015 */
016public class ProxyReporterManager extends AbstractProvidingProxyManager<Reporter> implements ReporterManager {
017
018    public ProxyReporterManager() {
019        super();
020    }
021
022    @Override
023    protected AbstractManager<Reporter> makeInternalManager() {
024        return jmri.InstanceManager.getDefault(jmri.jmrix.internal.InternalSystemConnectionMemo.class).getReporterManager();
025    }
026
027    @Override
028    public int getXMLOrder() {
029        return jmri.Manager.REPORTERS;
030    }
031
032    /**
033     * Locate via user name, then system name if needed.
034     *
035     * @return Null if nothing by that name exists
036     */
037    @Override
038    @CheckForNull
039    public Reporter getReporter(@Nonnull String name) {
040        return super.getNamedBean(name);
041    }
042
043    @Override
044    @Nonnull
045    protected Reporter makeBean(Manager<Reporter> manager, String systemName, String userName) throws IllegalArgumentException {
046        return ((ReporterManager) manager).newReporter(systemName, userName);
047    }
048
049    @Override
050    @Nonnull
051    public Reporter provideReporter(@Nonnull String sName) throws IllegalArgumentException {
052        return super.provideNamedBean(sName);
053    }
054
055    /** {@inheritDoc} */
056    @Override
057    @Nonnull
058    public Reporter provide(@Nonnull String name) throws IllegalArgumentException { return provideReporter(name); }
059
060    @Override
061    @CheckForNull
062    public Reporter getByDisplayName(@Nonnull String key) {
063        // First try to find it in the user list.
064        // If that fails, look it up in the system list
065        Reporter retv = this.getByUserName(key);
066        if (retv == null) {
067            retv = this.getBySystemName(key);
068        }
069        // If it's not in the system list, go ahead and return null
070        return (retv);
071    }
072
073    /**
074     * Get an instance with the specified system and user names. Note that
075     * two calls with the same arguments will get the same instance; there is
076     * only one Reporter object representing a given physical Reporter and
077     * therefore only one with a specific system or user name.
078     * <p>
079     * This will always return a valid object reference for a valid request; a
080     * new object will be created if necessary. In that case:
081     * <ul>
082     * <li>If a null reference is given for user name, no user name will be
083     * associated with the Reporter object created; a valid system name must be
084     * provided
085     * <li>If a null reference is given for the system name, a system name will
086     * _somehow_ be inferred from the user name. How this is done is system
087     * specific.
088     * <li>If both names are provided, the system name defines the hardware
089     * access of the desired Reporter, and the user address is associated with
090     * it.
091     * </ul>
092     * Note that it is possible to make an inconsistent request if both
093     * addresses are provided, but the given values are associated with
094     * different objects. This is a problem, and we don't have a good solution
095     * except to issue warnings. This will mostly happen if you're creating
096     * Reporters when you should be looking them up.
097     *
098     * @return requested Reporter object (never null)
099     */
100    @Override
101    @Nonnull
102    public Reporter newReporter(@Nonnull String systemName, String userName) throws IllegalArgumentException {
103        return newNamedBean(systemName, userName);
104    }
105
106    @Override
107    public boolean allowMultipleAdditions(@Nonnull String systemName) {
108        return ((ReporterManager) getManagerOrDefault(systemName)).allowMultipleAdditions(systemName);
109    }
110
111    /**
112     * {@inheritDoc}
113     */
114    @Override
115    @Nonnull
116    public String getBeanTypeHandled(boolean plural) {
117        return Bundle.getMessage(plural ? "BeanNameReporters" : "BeanNameReporter"); // NOI18N
118    }
119
120    /**
121     * {@inheritDoc}
122     */
123    @Override
124    public Class<Reporter> getNamedBeanClass() {
125        return Reporter.class;
126    }
127
128}