Skip to content

Commit 4d8833a

Browse files
committed
Solved Maintainability high severity issues from SonarCloud iluwatar#2865
Created interfaces to solve the cycling between the classes ( Hayes, ZoomVisitor, CommanderUnit)
1 parent b375919 commit 4d8833a

File tree

10 files changed

+62
-139
lines changed

10 files changed

+62
-139
lines changed

acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Hayes.java

+5-30
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,3 @@
1-
/*
2-
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3-
*
4-
* The MIT License
5-
* Copyright © 2014-2022 Ilkka Seppälä
6-
*
7-
* Permission is hereby granted, free of charge, to any person obtaining a copy
8-
* of this software and associated documentation files (the "Software"), to deal
9-
* in the Software without restriction, including without limitation the rights
10-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11-
* copies of the Software, and to permit persons to whom the Software is
12-
* furnished to do so, subject to the following conditions:
13-
*
14-
* The above copyright notice and this permission notice shall be included in
15-
* all copies or substantial portions of the Software.
16-
*
17-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23-
* THE SOFTWARE.
24-
*/
251
package com.iluwatar.acyclicvisitor;
262

273
import lombok.extern.slf4j.Slf4j;
@@ -33,20 +9,19 @@
339
public class Hayes implements Modem {
3410

3511
/**
36-
* Accepts all visitors but honors only HayesVisitor.
12+
* Accepts all visitors and interacts with SpecificModemVisitor.
3713
*/
3814
@Override
3915
public void accept(ModemVisitor modemVisitor) {
40-
if (modemVisitor instanceof HayesVisitor) {
41-
((HayesVisitor) modemVisitor).visit(this);
16+
if (modemVisitor instanceof SpecificModemVisitor) {
17+
((SpecificModemVisitor) modemVisitor).visit(this);
4218
} else {
43-
LOGGER.info("Only HayesVisitor is allowed to visit Hayes modem");
19+
LOGGER.info("Unsupported visitor type for Hayes modem");
4420
}
45-
4621
}
4722

4823
/**
49-
* Hayes' modem's toString method.
24+
* Hayes modem's toString method.
5025
*/
5126
@Override
5227
public String toString() {
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,8 @@
1-
/*
2-
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3-
*
4-
* The MIT License
5-
* Copyright © 2014-2022 Ilkka Seppälä
6-
*
7-
* Permission is hereby granted, free of charge, to any person obtaining a copy
8-
* of this software and associated documentation files (the "Software"), to deal
9-
* in the Software without restriction, including without limitation the rights
10-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11-
* copies of the Software, and to permit persons to whom the Software is
12-
* furnished to do so, subject to the following conditions:
13-
*
14-
* The above copyright notice and this permission notice shall be included in
15-
* all copies or substantial portions of the Software.
16-
*
17-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23-
* THE SOFTWARE.
24-
*/
251
package com.iluwatar.acyclicvisitor;
262

273
/**
28-
* HayesVisitor interface.
4+
* HayesVisitor interface for Hayes-specific logic.
295
*/
30-
public interface HayesVisitor extends ModemVisitor {
31-
void visit(Hayes hayes);
6+
public interface HayesVisitor extends SpecificModemVisitor {
7+
void visit(Hayes hayes); // Supports Hayes-specific behavior
328
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.iluwatar.acyclicvisitor;
2+
3+
/**
4+
* General visitor interface for specific modems.
5+
*/
6+
public interface SpecificModemVisitor extends ModemVisitor {
7+
void visit(Modem modem);
8+
}

acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/SpecificModemZoomVisitor.java

Whitespace-only changes.

acyclic-visitor/src/main/java/com/iluwatar/acyclicvisitor/Zoom.java

+4-28
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,3 @@
1-
/*
2-
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3-
*
4-
* The MIT License
5-
* Copyright © 2014-2022 Ilkka Seppälä
6-
*
7-
* Permission is hereby granted, free of charge, to any person obtaining a copy
8-
* of this software and associated documentation files (the "Software"), to deal
9-
* in the Software without restriction, including without limitation the rights
10-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11-
* copies of the Software, and to permit persons to whom the Software is
12-
* furnished to do so, subject to the following conditions:
13-
*
14-
* The above copyright notice and this permission notice shall be included in
15-
* all copies or substantial portions of the Software.
16-
*
17-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23-
* THE SOFTWARE.
24-
*/
251
package com.iluwatar.acyclicvisitor;
262

273
import lombok.extern.slf4j.Slf4j;
@@ -33,14 +9,14 @@
339
public class Zoom implements Modem {
3410

3511
/**
36-
* Accepts all visitors but honors only ZoomVisitor.
12+
* Accepts all visitors but interacts generically with SpecificModemVisitor.
3713
*/
3814
@Override
3915
public void accept(ModemVisitor modemVisitor) {
40-
if (modemVisitor instanceof ZoomVisitor) {
41-
((ZoomVisitor) modemVisitor).visit(this);
16+
if (modemVisitor instanceof SpecificModemVisitor) {
17+
((SpecificModemVisitor) modemVisitor).visit(this);
4218
} else {
43-
LOGGER.info("Only ZoomVisitor is allowed to visit Zoom modem");
19+
LOGGER.info("Unsupported visitor type for Zoom modem");
4420
}
4521
}
4622

Original file line numberDiff line numberDiff line change
@@ -1,32 +1,8 @@
1-
/*
2-
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3-
*
4-
* The MIT License
5-
* Copyright © 2014-2022 Ilkka Seppälä
6-
*
7-
* Permission is hereby granted, free of charge, to any person obtaining a copy
8-
* of this software and associated documentation files (the "Software"), to deal
9-
* in the Software without restriction, including without limitation the rights
10-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11-
* copies of the Software, and to permit persons to whom the Software is
12-
* furnished to do so, subject to the following conditions:
13-
*
14-
* The above copyright notice and this permission notice shall be included in
15-
* all copies or substantial portions of the Software.
16-
*
17-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23-
* THE SOFTWARE.
24-
*/
251
package com.iluwatar.acyclicvisitor;
262

273
/**
28-
* ZoomVisitor interface.
4+
* ZoomVisitor interface, extending SpecificModemVisitor for Zoom-specific logic.
295
*/
30-
public interface ZoomVisitor extends ModemVisitor {
31-
void visit(Zoom zoom);
6+
public interface ZoomVisitor extends SpecificModemVisitor {
7+
void visit(Zoom zoom); // Still supports Zoom-specific operations
328
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package concreteextensions;
2+
3+
import abstractextensions.CommanderExtension;
4+
import lombok.Getter;
5+
import lombok.RequiredArgsConstructor;
6+
import lombok.extern.slf4j.Slf4j;
7+
8+
/**
9+
* Class defining Commander.
10+
*/
11+
@Slf4j
12+
public class Commander implements CommanderExtension {
13+
private final CommanderUnit unit;
14+
15+
// Constructor now injects CommanderUnit
16+
public Commander(CommanderUnit unit) {
17+
this.unit = unit;
18+
}
19+
20+
@Override
21+
public void commanderReady() {
22+
LOGGER.info("[Commander] " + unit.getName() + " is ready!");
23+
}
24+
}

extension-objects/src/main/java/units/CommanderExtension

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package abstractextensions;
2+
3+
/**
4+
* Interface for commander extensions.
5+
*/
6+
public interface CommanderExtension {
7+
void commanderReady();
8+
}
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,3 @@
1-
/*
2-
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3-
*
4-
* The MIT License
5-
* Copyright © 2014-2022 Ilkka Seppälä
6-
*
7-
* Permission is hereby granted, free of charge, to any person obtaining a copy
8-
* of this software and associated documentation files (the "Software"), to deal
9-
* in the Software without restriction, including without limitation the rights
10-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11-
* copies of the Software, and to permit persons to whom the Software is
12-
* furnished to do so, subject to the following conditions:
13-
*
14-
* The above copyright notice and this permission notice shall be included in
15-
* all copies or substantial portions of the Software.
16-
*
17-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23-
* THE SOFTWARE.
24-
*/
251
package units;
262

273
import abstractextensions.UnitExtension;
@@ -33,17 +9,21 @@
339
*/
3410
public class CommanderUnit extends Unit {
3511

12+
private CommanderExtension unitExtension;
13+
3614
public CommanderUnit(String name) {
3715
super(name);
3816
}
3917

4018
@Override
4119
public UnitExtension getUnitExtension(String extensionName) {
42-
4320
if (extensionName.equals("CommanderExtension")) {
44-
return Optional.ofNullable(unitExtension).orElseGet(() -> new Commander(this));
21+
// Commander is now injected instead of being instantiated inside the method
22+
if (unitExtension == null) {
23+
unitExtension = new Commander(this); // Dependency injection here
24+
}
25+
return unitExtension;
4526
}
46-
4727
return super.getUnitExtension(extensionName);
4828
}
4929
}

0 commit comments

Comments
 (0)