Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: demonstrate refactoring duplicate code using generics
- Loading branch information
Showing
4 changed files
with
78 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
java/src/jmri/jmrix/swing/SystemConnectionNamedPaneAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package jmri.jmrix.swing; | ||
|
||
import javax.swing.Icon; | ||
import jmri.jmrix.SystemConnectionMemo; | ||
import jmri.util.swing.JmriNamedPaneAction; | ||
import jmri.util.swing.WindowInterface; | ||
|
||
/** | ||
* {@link JmriNamedPaneAction} that implements {@link SystemConnectionAction}. | ||
* | ||
* @author Randall Wood Copyright 2020 | ||
* @param <M> the supported subclass of {@link SystemConnectionMemo} | ||
*/ | ||
public abstract class SystemConnectionNamedPaneAction<M extends SystemConnectionMemo> extends JmriNamedPaneAction implements SystemConnectionAction<M> { | ||
|
||
protected M memo; | ||
|
||
public SystemConnectionNamedPaneAction(String s, String paneClass, M memo) { | ||
super(s, paneClass); | ||
this.memo = memo; | ||
} | ||
|
||
public SystemConnectionNamedPaneAction(String s, Icon i, WindowInterface wi, String paneClass, M memo) { | ||
super(s, i, wi, paneClass); | ||
this.memo = memo; | ||
} | ||
|
||
public SystemConnectionNamedPaneAction(String s, WindowInterface wi, String paneClass, M memo) { | ||
super(s, wi, paneClass); | ||
this.memo = memo; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public M getSystemConnectionMemo() { | ||
return memo; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void setSystemConnectionMemo(M memo) { | ||
if (getSystemConnectionMemoClasses().stream().anyMatch(memo.getClass()::isAssignableFrom)) { | ||
this.memo = memo; | ||
} else { | ||
throw new IllegalArgumentException(memo.getClass() + " is not valid for " + this.getClass()); | ||
} | ||
} | ||
} |