001package jmri.managers;
002
003import java.util.Arrays;
004import java.util.Set;
005
006import jmri.*;
007import jmri.implementation.AbstractInstanceInitializer;
008import jmri.implementation.DefaultClockControl;
009import jmri.jmrit.audio.DefaultAudioManager;
010import jmri.jmrit.audio.DefaultAudioSourceManager;
011import jmri.jmrit.simpleclock.SimpleTimebase;
012import jmri.jmrit.vsdecoder.VSDecoderManager;
013import jmri.jmrix.internal.InternalSystemConnectionMemo;
014
015import org.openide.util.lookup.ServiceProvider;
016
017/**
018 * Provide the usual default implementations for the
019 * {@link jmri.InstanceManager}.
020 * <p>
021 * Not all {@link jmri.InstanceManager} related classes are provided by this
022 * class. See the discussion in {@link jmri.InstanceManager} of initialization
023 * methods.
024 * <hr>
025 * This file is part of JMRI.
026 * <p>
027 * JMRI is free software; you can redistribute it and/or modify it under the
028 * terms of version 2 of the GNU General Public License as published by the Free
029 * Software Foundation. See the "COPYING" file for a copy of this license.
030 * <p>
031 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
032 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
033 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
034 *
035 * @author Bob Jacobsen Copyright (C) 2001, 2008, 2014
036 * @since 2.9.4
037 */
038@ServiceProvider(service = InstanceInitializer.class)
039public class DefaultInstanceInitializer extends AbstractInstanceInitializer {
040
041    @Override
042    public <T> Object getDefault(Class<T> type) {
043
044        InternalSystemConnectionMemo memo = InstanceManager.getDefault(InternalSystemConnectionMemo.class);
045        // In order for getDefault() to create a new object, the manager also
046        // needs to be added to the method getInitalizes() below.
047
048        if (type == AnalogIOManager.class) {
049            return new ProxyAnalogIOManager().init();
050        }
051
052        if (type == AudioManager.class) {
053            return new DefaultAudioManager(memo);
054        }
055
056        if (type == AudioSourceManager.class) {
057            return new DefaultAudioSourceManager();
058        }
059
060        if (type == ClockControl.class) {
061            return new DefaultClockControl();
062        }
063
064        if (type == ConditionalManager.class) {
065            return new DefaultConditionalManager(memo);
066        }
067
068        if (type == LightManager.class) {
069            return new ProxyLightManager();
070        }
071
072        if (type == VariableLightManager.class) {
073            return new DefaultVariableLightManager(memo).init();
074        }
075
076        if (type == LogixManager.class) {
077            return new DefaultLogixManager(memo);
078        }
079
080        if (type == MemoryManager.class) {
081            return new DefaultMemoryManager(memo);
082        }
083
084        if (type == MeterManager.class) {
085            return new ProxyMeterManager();
086        }
087
088        if (type == RailComManager.class) {
089            return new DefaultRailComManager();
090        }
091
092        if (type == ReporterManager.class) {
093            return new ProxyReporterManager();
094        }
095
096        if (type == RouteManager.class) {
097            return new DefaultRouteManager(memo);
098        }
099
100        if (type == SectionManager.class) {
101            return new DefaultSectionManager();
102        }
103
104        if (type == SensorManager.class) {
105            return new ProxySensorManager();
106        }
107
108        if (type == SignalGroupManager.class) {
109            // ensure signal mast manager exists first
110            InstanceManager.getDefault(SignalMastManager.class);
111            return new DefaultSignalGroupManager(memo);
112        }
113
114        if (type == SignalHeadManager.class) {
115            return new AbstractSignalHeadManager(memo);
116        }
117
118        if (type == SignalMastLogicManager.class) {
119            return new DefaultSignalMastLogicManager(memo);
120        }
121
122        if (type == SignalMastManager.class) {
123            // ensure signal head manager exists first
124            InstanceManager.getDefault(SignalHeadManager.class);
125            return new DefaultSignalMastManager(memo);
126        }
127
128        if (type == SignalSystemManager.class) {
129            return new DefaultSignalSystemManager(memo);
130        }
131
132        if (type == StringIOManager.class) {
133            return new ProxyStringIOManager();
134        }
135
136        if (type == Timebase.class) {
137            Timebase timebase = new SimpleTimebase(memo);
138            InstanceManager.getOptionalDefault(ConfigureManager.class).ifPresent(cm -> cm.registerConfig(timebase, Manager.TIMEBASE));
139            return timebase;
140        }
141
142        if (type == TurnoutManager.class) {
143            return new ProxyTurnoutManager();
144        }
145
146        if (type == TransitManager.class) {
147            return new DefaultTransitManager();
148        }
149
150        if (type == VSDecoderManager.class) {
151            return VSDecoderManager.instance();
152        }
153
154        if (type == IdTagManager.class) {
155            return new ProxyIdTagManager();
156        }
157
158        return super.getDefault(type);
159    }
160
161    @Override
162    public Set<Class<?>> getInitalizes() {
163        Set<Class<?>> set = super.getInitalizes();
164        set.addAll(Arrays.asList(
165                AnalogIOManager.class,
166                AudioManager.class,
167                AudioSourceManager.class,
168                ClockControl.class,
169                ConditionalManager.class,
170                IdTagManager.class,
171                LightManager.class,
172                LogixManager.class,
173                MemoryManager.class,
174                MeterManager.class,
175                RailComManager.class,
176                ReporterManager.class,
177                RouteManager.class,
178                SectionManager.class,
179                SensorManager.class,
180                SignalGroupManager.class,
181                SignalHeadManager.class,
182                SignalMastLogicManager.class,
183                SignalMastManager.class,
184                SignalSystemManager.class,
185                StringIOManager.class,
186                Timebase.class,
187                TransitManager.class,
188                TurnoutManager.class,
189                VariableLightManager.class,
190                VSDecoderManager.class
191        ));
192        return set;
193    }
194
195}