Lever switch refactor

This commit is contained in:
Bane Stojkovic
2022-10-09 21:25:20 +02:00
parent fa0d3699ce
commit 2447780585
3 changed files with 51 additions and 240 deletions
@@ -57,7 +57,7 @@ import org.diylc.utils.Constants;
description = "Strat-style lever switch", zOrder = IDIYComponent.COMPONENT,
instanceNamePrefix = "SW", keywordPolicy = KeywordPolicy.SHOW_TAG,
keywordTag = "Guitar Wiring Diagram", transformer = AngledComponentTransformer.class)
public class LeverSwitch extends AbstractAngledComponent<String> implements ISwitch {
public class LeverSwitch extends AbstractAngledComponent<LeverSwitchType> implements ISwitch {
private static final long serialVersionUID = 1L;
@@ -76,8 +76,10 @@ public class LeverSwitch extends AbstractAngledComponent<String> implements ISwi
private static Size TERMINAL_WIDTH = new Size(2d, SizeUnit.mm);
private static Size TERMINAL_LENGTH = new Size(0.1d, SizeUnit.in);
private static Size TERMINAL_SPACING = new Size(0.1d, SizeUnit.in);
private String value = "";
@SuppressWarnings("unused")
@Deprecated
private transient String value = "";
private Point2D[] controlPoints = new Point2D[] {new Point2D.Double(0, 0)};
private LeverSwitchType type = LeverSwitchType.DP3T;
private Boolean highlightCommon;
@@ -365,23 +367,15 @@ public class LeverSwitch extends AbstractAngledComponent<String> implements ISwi
// Invalidate the body
body = null;
}
@Override
public String getValue() {
return value;
}
@Override
public void setValue(String value) {
this.value = value;
}
@EditableProperty
public LeverSwitchType getType() {
@EditableProperty(name = "Type")
public LeverSwitchType getValue() {
return type;
}
public void setType(LeverSwitchType type) {
@Override
public void setValue(LeverSwitchType type) {
this.type = type;
updateControlPoints();
// Invalidate body
@@ -401,21 +395,7 @@ public class LeverSwitch extends AbstractAngledComponent<String> implements ISwi
body = null;
}
public enum LeverSwitchType {
DP3T("DP3T (Standard 3-Position Strat)"), DP3T_5pos("DP3T (Standard 5-Position Strat)"), DP3T_5pos_Import("DP3T (Import 5-Position Strat)"),
_4P5T("4P5T (Super/Mega)"), DP4T("DP4T (4-Position Tele)"), _6_WAY_OG("DP4T (6-Position Oak Grigsby)"), DP5T("DP5T");
private String title;
private LeverSwitchType(String title) {
this.title = title;
}
@Override
public String toString() {
return title;
}
}
// @Override
// public String getControlPointNodeName(int index) {
@@ -0,0 +1,39 @@
/*
DIY Layout Creator (DIYLC).
Copyright (c) 2009-2018 held jointly by the individual authors.
This file is part of DIYLC.
DIYLC is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
DIYLC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.components.guitar;
public enum LeverSwitchType {
DP3T("DP3T (Standard 3-Position Strat)"), DP3T_5pos("DP3T (Standard 5-Position Strat)"), DP3T_5pos_Import("DP3T (Import 5-Position Strat)"),
_4P5T("4P5T (Super/Mega)"), DP4T("DP4T (4-Position Tele)"), _6_WAY_OG("DP4T (6-Position Oak Grigsby)"), DP5T("DP5T");
private String title;
private LeverSwitchType(String title) {
this.title = title;
}
@Override
public String toString() {
return title;
}
}
@@ -1,208 +0,0 @@
package org.diylc.netlist;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.Arrays;
import org.diylc.components.electromechanical.MiniToggleSwitch;
import org.diylc.components.electromechanical.SlideSwitch;
import org.diylc.components.electromechanical.SlideSwitchType;
import org.diylc.components.electromechanical.ToggleSwitchType;
import org.diylc.components.guitar.LPSwitch;
import org.diylc.components.guitar.LeverSwitch;
import org.diylc.components.guitar.LeverSwitch.LeverSwitchType;
import org.diylc.components.guitar.S1Switch;
import org.diylc.components.guitar.SchallerMegaSwitch;
import org.diylc.components.guitar.SchallerMegaSwitch.MegaSwitchType;
import org.diylc.core.IDIYComponent;
import org.diylc.core.ISwitch;
import org.junit.Test;
public class SwitchTests {
@Test
public void testLeverSwitchDP5T() {
LeverSwitch leverSwitch = new LeverSwitch();
leverSwitch.setType(LeverSwitchType.DP5T);
String[] validCombinations = new String[] {"0,0,1", "0,6,11", "1,0,2", "1,7,11", "2,0,3",
"2,8,11", "3,0,4", "3,9,11", "4,0,5", "4,10,11"};
testSwitch(leverSwitch, validCombinations);
}
@Test
public void testLeverSwitch4P5T() {
LeverSwitch leverSwitch = new LeverSwitch();
String[] validCombinations = new String[] {"0,0,1", "0,6,11", "0,12,13", "0,18,23", "1,0,2",
"1,7,11", "1,12,14", "1,19,23", "2,0,3", "2,8,11", "2,12,15", "2,20,23", "3,0,4", "3,9,11",
"3,12,16", "3,21,23", "4,0,5", "4,10,11", "4,12,17", "4,22,23"};
leverSwitch.setType(LeverSwitchType._4P5T);
testSwitch(leverSwitch, validCombinations);
}
@Test
public void testLeverSwitchImport() {
LeverSwitch leverSwitch = new LeverSwitch();
String[] validCombinations = new String[] {"0,0,3", "0,4,5", "1,0,3", "1,4,5", "1,1,3",
"1,4,6", "2,1,3", "2,4,6", "3,1,3", "3,2,3", "3,4,6", "3,4,7", "4,2,3", "4,4,7"};
leverSwitch.setType(LeverSwitchType.DP3T_5pos_Import);
testSwitch(leverSwitch, validCombinations);
}
@Test
public void testLeverSwitch6Way() {
LeverSwitch leverSwitch = new LeverSwitch();
String[] validCombinations = new String[] {"0,0,8", "0,1,3", "0,1,5", "1,0,8", "1,2,8", "1,1,5", "2,2,8", "2,1,5", "2,1,7",
"3,2,8", "3,4,8", "3,1,7", "4,4,8", "4,1,7", "4,1,9", "5,4,8", "5,6,8", "5,1,9"};
leverSwitch.setType(LeverSwitchType._6_WAY_OG);
testSwitch(leverSwitch, validCombinations);
}
@Test
public void testS1Switch() {
S1Switch s1Switch = new S1Switch();
String[] validCombinations = new String[] {"0,1,2", "0,4,5", "0,7,8", "0,10,11", "1,2,3", "1,5,6", "1,8,9", "1,11,12"};
testSwitch(s1Switch, validCombinations);
}
@Test
public void testLPSwitch() {
LPSwitch lpSwitch = new LPSwitch();
String[] validCombinations = new String[] {"0,1,2", "1,1,2", "1,2,3", "1,1,3", "2,2,3"};
testSwitch(lpSwitch, validCombinations);
}
@Test
public void testSlideSwitchSPDT() {
SlideSwitch slideSwitch = new SlideSwitch();
slideSwitch.setValue(SlideSwitchType.SPDT);
String[] validCombinations = new String[] {"0,0,1", "1,1,2"};
testSwitch(slideSwitch, validCombinations);
}
@Test
public void testSlideSwitchDPDT() {
SlideSwitch slideSwitch = new SlideSwitch();
slideSwitch.setValue(SlideSwitchType.DPDT);
String[] validCombinations = new String[] {"0,0,2", "0,1,3", "1,2,4", "1,3,5"};
testSwitch(slideSwitch, validCombinations);
}
@Test
public void testSlideSwitchDP3T() {
SlideSwitch slideSwitch = new SlideSwitch();
slideSwitch.setValue(SlideSwitchType.DP3T);
String[] validCombinations = new String[] {"0,0,2", "0,1,3", "1,2,4", "1,3,5", "2,4,6", "2,5,7"};
testSwitch(slideSwitch, validCombinations);
}
@Test
public void testMiniToggleSwitchDPDTononon1() {
MiniToggleSwitch toggleSwitch = new MiniToggleSwitch();
toggleSwitch.setValue(ToggleSwitchType.DPDT_ononon_1);
String[] validCombinations = new String[] {"0,0,1", "0,3,4", "1,0,1", "1,4,5", "2,1,2", "2,4,5"};
testSwitch(toggleSwitch, validCombinations);
}
@Test
public void testMiniToggleSwitchDPDTononon2() {
MiniToggleSwitch toggleSwitch = new MiniToggleSwitch();
toggleSwitch.setValue(ToggleSwitchType.DPDT_ononon_2);
String[] validCombinations = new String[] {"0,0,1", "0,3,4", "1,1,2", "1,3,4", "2,1,2", "2,4,5"};
testSwitch(toggleSwitch, validCombinations);
}
@Test
public void testMegaSwitchE() {
SchallerMegaSwitch sw = new SchallerMegaSwitch();
sw.setType(MegaSwitchType.E);
String[] validCombinations = new String[] {"0,2,6", "1,3,5", "1,2,6", "1,0,2", "2,3,4",
"2,2,5", "2,1,2", "3,3,4", "3,0,2", "3,1,2", "4,1,2"};
testSwitch(sw, validCombinations);
}
@Test
public void testMegaSwitchEPlus() {
SchallerMegaSwitch sw = new SchallerMegaSwitch();
sw.setType(MegaSwitchType.E_PLUS);
String[] validCombinations = new String[] {"0,2,8", "1,3,6", "1,2,8", "1,0,2", "2,3,5",
"2,2,7", "2,1,2", "3,3,4", "3,0,2", "3,1,2", "4,1,2"};
testSwitch(sw, validCombinations);
}
@Test
public void testMegaSwitchP() {
SchallerMegaSwitch sw = new SchallerMegaSwitch();
sw.setType(MegaSwitchType.P);
String[] validCombinations = new String[] {"0,3,4", "0,1,6", "1,3,4", "1,1,2", "1,0,4", "1,1,5",
"2,0,2", "2,1,6", "3,0,1", "3,3,4", "3,2,4", "3,1,6", "4,1,5"};
testSwitch(sw, validCombinations);
}
@Test
public void testMegaSwitchS() {
SchallerMegaSwitch sw = new SchallerMegaSwitch();
sw.setType(MegaSwitchType.S);
String[] validCombinations = new String[] {"0,6,7", "0,2,3", "1,4,7", "1,6,7", "1,0,3", "1,2,3",
"2,4,7", "2,0,3", "3,4,7", "3,5,7", "3,0,3", "3,1,3", "4,5,7", "4,1,3"};
testSwitch(sw, validCombinations);
}
@Test
public void testMegaSwitchM() {
SchallerMegaSwitch sw = new SchallerMegaSwitch();
sw.setType(MegaSwitchType.M);
String[] validCombinations = new String[] {"0,4,5", "0,10,11", "0,16,17", "0,22,23",
"1,3,5", "1,9,11", "1,15,17", "1,21,23",
"2,2,5", "2,8,11", "2,14,17", "2,20,23",
"3,1,5", "3,7,11", "3,13,17", "3,19,23",
"4,0,5", "4,6,11", "4,12,17", "4,18,23"};
testSwitch(sw, validCombinations);
}
@Test
public void testMegaSwitchT() {
SchallerMegaSwitch sw = new SchallerMegaSwitch();
sw.setType(MegaSwitchType.T);
String[] validCombinations = new String[] {"0,6,7", "0,2,3", "1,4,7", "1,0,3",
"2,5,7", "2,1,3"};
testSwitch(sw, validCombinations);
}
private void testSwitch(ISwitch sw, String[] validCombinations) {
Arrays.sort(validCombinations);
int count = 0;
for (int p = 0; p < sw.getPositionCount(); p++)
for (int i = 0; i < ((IDIYComponent<?>) sw).getControlPointCount(); i++)
for (int j = 0; j < ((IDIYComponent<?>) sw).getControlPointCount(); j++) {
String test = p + "," + i + "," + j;
boolean isConnected = sw.arePointsConnected(i, j, p);
if (isConnected)
count++;
boolean isOk = (isConnected && Arrays.binarySearch(validCombinations, test) >= 0)
|| !isConnected && Arrays.binarySearch(validCombinations, test) < 0;
if (!isOk)
fail("Bad connection for p=" + p + ", i=" + i + ",j=" + j);
}
assertEquals(validCombinations.length, count);
}
}