Skip to content

Commit

Permalink
Update SignalGroupManager
Browse files Browse the repository at this point in the history
provideSignalGroup use getByUserName(userName) not getByUserName(systemName)
renames #newSignaGroup to #newSignalGroup
  • Loading branch information
icklesteve committed Jul 19, 2021
1 parent faabf2b commit 54ff7bf
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 40 deletions.
46 changes: 29 additions & 17 deletions java/src/jmri/SignalGroupManager.java
Expand Up @@ -18,55 +18,67 @@
public interface SignalGroupManager extends Manager<SignalGroup> {

/**
* Locate via user name, then system name if needed. Does not create a new
* one if nothing found
* Locate via user name, then system name if needed.
* <p>
* Does not create a new one if nothing found
*
* @param name User Name or System Name to match
* @return null if no match found
*/
@CheckForNull
public SignalGroup getSignalGroup(@Nonnull String name);

@CheckForNull public SignalGroup getBySystemName(@Nonnull String name);
/** {@inheritDoc} */
@CheckForNull
@Override
public SignalGroup getBySystemName(@Nonnull String name);

@CheckForNull public SignalGroup getByUserName(@Nonnull String name);
/** {@inheritDoc} */
@CheckForNull
@Override
public SignalGroup getByUserName(@Nonnull String name);

/**
* Create a new Signal group if the group does not exist. Intended for use with
* Create a new Signal group if the group does not exist.
* <p>
* Intended for use with
* User GUI, to allow the auto generation of systemNames, where the user can
* optionally supply a username.
*
* @deprecated since 4.25.2, use #newSignalGroupWithUserName(@Nonnull String userName)
* @param userName User name for the new group
* @return null if a Group with the same userName already exists or if there
* is trouble creating a new Group
* @return a Signal Group with the same userName if already exists
* @throws IllegalArgumentException if there is trouble creating a new Group
*/
@Deprecated // 4.25.2
@Nonnull
public SignalGroup newSignaGroupWithUserName(@Nonnull String userName);
public SignalGroup newSignaGroupWithUserName(@Nonnull String userName) throws IllegalArgumentException;

/**
* Create a new Signal group if the group does not exist. Intended for use with
* Create a new Signal group if the group does not exist.
* <p>
* Intended for use with
* User GUI, to allow the auto generation of systemNames, where the user can
* optionally supply a username.
*
* @param userName User name for the new group
* @return null if a Group with the same userName already exists or if there
* is trouble creating a new Group
* @deprecated 4.15.2 use newSignaGroupWithUserName
* @return a Signal Group with the same userName if already exists
* @throws IllegalArgumentException if there is trouble creating a new Group
*/
@Nonnull
@Deprecated // 4.15.2 use newSignaGroupWithUserName
public SignalGroup newSignalGroup(@Nonnull String userName);
public SignalGroup newSignalGroupWithUserName(@Nonnull String userName) throws IllegalArgumentException;

/**
* Create a new SignalGroup if the group does not exist.
*
* @param systemName the system name for the group
* @param userName the user name for the group
* @return null if a Signal Group with the same systemName or userName already
* exists or if there is trouble creating a new Group
* @return a Signal Group with the same systemName or userName if
* already exists or
* @throws IllegalArgumentException if there is trouble creating a new Group.
*/
@Nonnull
public SignalGroup provideSignalGroup(@Nonnull String systemName, String userName);
public SignalGroup provideSignalGroup(@Nonnull String systemName, String userName) throws IllegalArgumentException;

/**
* Delete Group by removing it from the manager. The Group must first be
Expand Down
47 changes: 25 additions & 22 deletions java/src/jmri/managers/DefaultSignalGroupManager.java
Expand Up @@ -4,14 +4,17 @@
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import jmri.InstanceManager;

import jmri.Manager;
import jmri.SignalGroup;
import jmri.SignalGroupManager;
import jmri.implementation.DefaultSignalGroup;
import jmri.jmrix.internal.InternalSystemConnectionMemo;
import jmri.util.FileUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -43,6 +46,8 @@ public char typeLetter() {
return 'G'; // according to JMRI: Names and Naming
}

/** {@inheritDoc} */
@CheckForNull
@Override
public SignalGroup getSignalGroup(@Nonnull String name) {
SignalGroup t = getByUserName(name);
Expand All @@ -53,11 +58,15 @@ public SignalGroup getSignalGroup(@Nonnull String name) {
return getBySystemName(name);
}

/** {@inheritDoc} */
@CheckForNull
@Override
public SignalGroup getBySystemName(@Nonnull String key) {
return _tsys.get(key);
}

/** {@inheritDoc} */
@CheckForNull
@Override
public SignalGroup getByUserName(@Nonnull String key) {
return _tuser.get(key);
Expand All @@ -71,12 +80,14 @@ public SignalGroup getByUserName(@Nonnull String key) {
*/
@Override
@Nonnull
public SignalGroup provideSignalGroup(@Nonnull String systemName, String userName) {
public SignalGroup provideSignalGroup(@Nonnull String systemName, String userName) throws IllegalArgumentException {
log.debug("provideGroup({})", systemName);
SignalGroup r;
r = getByUserName(systemName);
if (r != null) {
return r;
if ( userName!=null ) {
r = getByUserName(userName);
if (r != null) {
return r;
}
}
r = getBySystemName(systemName);
if (r != null) {
Expand All @@ -95,14 +106,16 @@ public SignalGroup provideSignalGroup(@Nonnull String systemName, String userNam

/**
* {@inheritDoc}
* @deprecated 4.15.2 use newSignaGroupWithUserName
*
* Keep autostring in line with {@link #provideSignalGroup(String, String)},
* {@link #getSystemPrefix()} and {@link #typeLetter()}
*/
@Deprecated // 4.25.2
@Nonnull
@Override
@Deprecated // 4.15.2 use newSignaGroupWithUserName
public SignalGroup newSignalGroup(@Nonnull String userName) {
jmri.util.LoggingUtil.deprecationWarning(log, "newSignalGroup");
return newSignaGroupWithUserName(userName);
public SignalGroup newSignaGroupWithUserName(@Nonnull String userName) {
jmri.util.LoggingUtil.deprecationWarning(log, "newSignaGroupWithUserName");
return newSignalGroupWithUserName(userName);
}

/**
Expand All @@ -113,12 +126,12 @@ public SignalGroup newSignalGroup(@Nonnull String userName) {
*/
@Nonnull
@Override
public SignalGroup newSignaGroupWithUserName(@Nonnull String userName) {
public SignalGroup newSignalGroupWithUserName(@Nonnull String userName) {
return provideSignalGroup(getAutoSystemName(), userName);
}

List<String> getListOfNames() {
List<String> retval = new ArrayList<String>();
List<String> retval = new ArrayList<>();
// first locate the signal system directory
// and get names of systems
File signalDir;
Expand All @@ -145,16 +158,6 @@ List<String> getListOfNames() {
return retval;
}

/**
*
* @return the default instance of DefaultSignalGroupManager
* @deprecated since 4.17.3; use {@link jmri.InstanceManager#getDefault(java.lang.Class)} instead
*/
@Deprecated
static public DefaultSignalGroupManager instance() {
return InstanceManager.getDefault(DefaultSignalGroupManager.class);
}

@Override
public void deleteSignalGroup(SignalGroup s) {
deregister(s);
Expand Down
14 changes: 13 additions & 1 deletion java/test/jmri/managers/DefaultSignalGroupManagerTest.java
Expand Up @@ -2,6 +2,8 @@

import jmri.InstanceManager;
import jmri.jmrix.internal.InternalSystemConnectionMemo;
import jmri.SignalGroup;
import jmri.SignalGroupManager;
import jmri.util.JUnitUtil;

import org.junit.Assert;
Expand All @@ -11,7 +13,7 @@
*
* @author Paul Bender Copyright (C) 2017
*/
public class DefaultSignalGroupManagerTest extends AbstractManagerTestBase<jmri.SignalGroupManager,jmri.SignalGroup> {
public class DefaultSignalGroupManagerTest extends AbstractManagerTestBase<SignalGroupManager,SignalGroup> {

@Test
public void testCTor() {
Expand All @@ -27,6 +29,16 @@ public void testMakeSystemNameWithNoPrefixNotASystemName() {}
@Test
@Override
public void testMakeSystemNameWithPrefixNotASystemName() {}

@Test
public void testProvideByUserName(){
SignalGroup sg = l.newSignalGroupWithUserName("Sig Group UserName");
Assert.assertNotNull(sg);
Assert.assertEquals("username returned ok",sg.getUserName(), l.provideSignalGroup("", "Sig Group UserName").getUserName());
Assert.assertEquals("systemname created ok","IG:AUTO:0001",sg.getSystemName());
Assert.assertEquals("systemname returned ok",sg,l.provideSignalGroup("IG:AUTO:0001", null));

}

@BeforeEach
public void setUp() {
Expand Down

0 comments on commit 54ff7bf

Please sign in to comment.