001package jmri.jmrit.logix;
002
003import javax.annotation.CheckForNull;
004import javax.annotation.Nonnull;
005
006import jmri.InstanceManagerAutoDefault;
007import jmri.ProvidingManager;
008import jmri.managers.AbstractManager;
009
010/**
011 * Basic Implementation of an OBlockManager.
012 * <p>
013 * Note that this does not enforce any particular system naming convention.
014 * <p>
015 * Note this is a concrete class, there are now 2 types of Blocks (LayoutBlocks
016 * use a Block member and inheritance). Perhaps now the proxyManager
017 * strategy of interface/implementation pairs like other Managers should be
018 * implemented.
019 *
020 * @author Bob Jacobsen Copyright (C) 2006
021 * @author Pete Cressman Copyright (C) 2009
022 */
023public class OBlockManager extends AbstractManager<OBlock>
024        implements ProvidingManager<OBlock>, InstanceManagerAutoDefault {
025
026    public OBlockManager() {
027        super(new jmri.jmrix.CaptiveSystemConnectionMemo("O", "OBlocks")); // NOI18N
028    }
029
030    @Override
031    public int getXMLOrder() {
032        return jmri.Manager.OBLOCKS;
033    }
034
035    @Override
036    public char typeLetter() {
037        return 'B';
038    }
039
040    /**
041     * Create a new OBlock if it does not exist.
042     *
043     * @param systemName System name
044     * @param userName   User name
045     * @return newly created OBlock, or null if an OBlock with the same
046     * systemName or userName already exists, or if there
047     * is trouble creating a new OBlock
048     */
049    @CheckForNull
050    public OBlock createNewOBlock(@Nonnull String systemName, @CheckForNull String userName) {
051        // Check that OBlock does not already exist
052        OBlock r;
053        if (userName != null && !userName.equals("")) {
054            r = getByUserName(userName);
055            if (r != null) {
056                return null;
057            }
058        }
059        if (!isValidSystemNameFormat(systemName)) {
060            return null;
061        }
062
063        r = getBySystemName(systemName);
064        if (r != null) {
065            return null;
066        }
067        // OBlock does not exist, create a new OBlock
068        r = new OBlock(systemName, userName);
069
070        // Keep track of the last created auto system name
071        updateAutoNumber(systemName);
072
073        // save in the maps
074        register(r);
075        return r;
076    }
077
078    /**
079     * Create a new OBlock using an automatically incrementing system
080     * name.
081     *
082     * @param userName the user name for the new OBlock
083     * @return null if an OBlock with the same systemName or userName already
084     *         exists, or if there is trouble creating a new OBlock.
085     */
086    @CheckForNull
087    public OBlock createNewOBlock(@Nonnull String userName) {
088        return createNewOBlock(getAutoSystemName(), userName);
089    }
090
091
092    /**
093     * Get an existing OBlock by a given name. First looks up assuming that name
094     * is a User Name. If this fails looks up assuming that name is a System Name.
095     * If both fail, returns null.
096     *
097     * @param name OBlock name
098     * @return the OBlock, oe null if not found
099     */
100    public OBlock getOBlock(@Nonnull String name) {
101        OBlock r = getByUserName(name);
102        if (r != null) {
103            return r;
104        }
105        return getBySystemName(name);
106    }
107
108    @Override
109    public OBlock provide(@Nonnull String name) {
110        return provideOBlock(name);
111    }
112
113    @Nonnull
114    public OBlock provideOBlock(@Nonnull String name) {
115        if (name.trim().length() == 0) {
116            throw new IllegalArgumentException("name \"" + name + "\" invalid");
117        }
118        OBlock ob = getByUserName(name);
119        if (ob == null) {
120            ob = getBySystemName(name);
121        }
122        if (ob == null) {
123            ob = createNewOBlock(name, null);
124            if (ob == null) {
125                throw new IllegalArgumentException("could not create OBlock \"" + name + "\"");
126            }
127        }
128        return ob;
129    }
130
131    @Override
132    @Nonnull
133    public String getBeanTypeHandled(boolean plural) {
134        return Bundle.getMessage(plural ? "BeanNameOBlocks" : "BeanNameOBlock");
135    }
136
137    /**
138     * {@inheritDoc}
139     */
140    @Override
141    public Class<OBlock> getNamedBeanClass() {
142        return OBlock.class;
143    }
144
145}