001package jmri.managers;
002
003import javax.annotation.CheckForNull;
004import javax.annotation.Nonnull;
005
006import jmri.*;
007import jmri.implementation.DefaultRoute;
008import jmri.jmrix.internal.InternalSystemConnectionMemo;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Basic Implementation of a RouteManager.
014 * <p>
015 * Note that this does not enforce any particular system naming convention
016 *
017 * @author Dave Duchamp Copyright (C) 2004
018 */
019public class DefaultRouteManager extends AbstractManager<Route> implements RouteManager {
020
021    public DefaultRouteManager(InternalSystemConnectionMemo memo) {
022        super(memo);
023        addListeners();
024    }
025    
026    final void addListeners(){
027        InstanceManager.getDefault(TurnoutManager.class).addVetoableChangeListener(this);
028        InstanceManager.getDefault(SensorManager.class).addVetoableChangeListener(this);
029    }
030
031    @Override
032    public int getXMLOrder() {
033        return Manager.ROUTES;
034    }
035
036    @Override
037    public char typeLetter() {
038        return 'O';
039    }
040
041    /**
042     * {@inheritDoc}
043     */
044    @Override
045    @Nonnull
046    public Route provideRoute(@Nonnull String systemName, @CheckForNull String userName) throws IllegalArgumentException {
047        log.debug("provideRoute({})", systemName);
048        Route r;
049        if (userName!=null){
050            r = getByUserName(userName);
051            if (r != null) {
052                return r;
053            }
054        }
055        r = getBySystemName(systemName);
056        if (r != null) {
057            return r;
058        }
059        // Route does not exist, create a new route
060        r = new DefaultRoute(validateSystemNameFormat(systemName), userName);
061        // save in the maps
062        register(r);
063
064        // Keep track of the last created auto system name
065        updateAutoNumber(systemName);
066
067        return r;
068    }
069
070    /**
071     * {@inheritDoc}
072     * <p>
073     * Calls {@link #provideRoute(String, String)} with the result of
074     * {@link #getAutoSystemName()} as the system name.
075     */
076    @Override
077    @Nonnull
078    public Route newRoute(@Nonnull String userName) throws IllegalArgumentException {
079        return provideRoute(getAutoSystemName(), userName);
080    }
081
082    /**
083     * Remove an existing route. Route must have been deactivated before
084     * invoking this.
085     */
086    @Override
087    public void deleteRoute(@Nonnull Route r) {
088        deregister(r);
089    }
090
091    /**
092     * Method to get an existing Route.
093     * First looks up assuming that name is a User Name.
094     * If this fails looks up assuming that name is a System Name.
095     * @return If both fail, returns null.
096     */
097    @Override
098    @CheckForNull
099    public Route getRoute(@Nonnull String name) {
100        Route r = getByUserName(name);
101        return (r != null ? r : getBySystemName(name) );
102    }
103
104    @Nonnull
105    @Override
106    public String getBeanTypeHandled(boolean plural) {
107        return Bundle.getMessage(plural ? "BeanNameRoutes" : "BeanNameRoute");
108    }
109
110    /**
111     * {@inheritDoc}
112     */
113    @Override
114    public Class<Route> getNamedBeanClass() {
115        return Route.class;
116    }
117
118    /**
119     * Provide Route by System Name.
120     * @param name System Name f Route.
121     * @return new or existing Route with corresponding System Name.
122     */
123    @Override
124    @Nonnull
125    public Route provide(@Nonnull String name) throws IllegalArgumentException {
126        return provideRoute(name, null);
127    }
128    
129    @Override
130    public void dispose(){
131        InstanceManager.getDefault(TurnoutManager.class).removeVetoableChangeListener(this);
132        InstanceManager.getDefault(SensorManager.class).removeVetoableChangeListener(this);
133        super.dispose();
134    }
135
136    private static final Logger log = LoggerFactory.getLogger(DefaultRouteManager.class);
137
138}