Skip to content

Commit f2c91eb

Browse files
anuragagarwal561994iluwatar
authored andcommitted
Resolves checkstyle errors for delegation dependency-injection dirty-flag double-buffer double-checked-locking double-dispatch (iluwatar#1068)
* Reduces checkstyle errors in delegation * Reduces checkstyle errors in dependency-injection * Reduces checkstyle errors in dirty-flag * Reduces checkstyle errors in double-buffer * Reduces checkstyle errors in double-checked-locking * Reduces checkstyle errors in double-dispatch
1 parent 01e489c commit f2c91eb

File tree

35 files changed

+154
-215
lines changed

35 files changed

+154
-215
lines changed

delegation/src/main/java/com/iluwatar/delegation/simple/App.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,25 @@
2828
import com.iluwatar.delegation.simple.printers.HpPrinter;
2929

3030
/**
31-
* The delegate pattern provides a mechanism to abstract away the implementation and control of the desired action.
32-
* The class being called in this case {@link PrinterController} is not responsible for the actual desired action,
33-
* but is actually delegated to a helper class either {@link CanonPrinter}, {@link EpsonPrinter} or {@link HpPrinter}.
34-
* The consumer does not have or require knowledge of the actual class carrying out the action, only the
35-
* container on which they are calling.
31+
* The delegate pattern provides a mechanism to abstract away the implementation and control of the
32+
* desired action. The class being called in this case {@link PrinterController} is not responsible
33+
* for the actual desired action, but is actually delegated to a helper class either {@link
34+
* CanonPrinter}, {@link EpsonPrinter} or {@link HpPrinter}. The consumer does not have or require
35+
* knowledge of the actual class carrying out the action, only the container on which they are
36+
* calling.
3637
*
37-
* In this example the delegates are {@link EpsonPrinter}, {@link HpPrinter} and {@link CanonPrinter} they all implement
38-
* {@link Printer}. The {@link PrinterController} class also implements {@link Printer}. However neither provide the
39-
* functionality of {@link Printer} by printing to the screen, they actually call upon the instance of {@link Printer}
40-
* that they were instantiated with. Therefore delegating the behaviour to another class.
38+
* <p>In this example the delegates are {@link EpsonPrinter}, {@link HpPrinter} and {@link
39+
* CanonPrinter} they all implement {@link Printer}. The {@link PrinterController} class also
40+
* implements {@link Printer}. However neither provide the functionality of {@link Printer} by
41+
* printing to the screen, they actually call upon the instance of {@link Printer} that they were
42+
* instantiated with. Therefore delegating the behaviour to another class.
4143
*/
4244
public class App {
4345

4446
private static final String MESSAGE_TO_PRINT = "hello world";
4547

4648
/**
47-
* Program entry point
49+
* Program entry point.
4850
*
4951
* @param args command line args
5052
*/

delegation/src/main/java/com/iluwatar/delegation/simple/Printer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public interface Printer {
3838

3939
/**
4040
* Method that takes a String to print to the screen. This will be implemented on both the
41-
* controller and the delegate allowing the controller to call the same method on the delegate class.
41+
* controller and the delegate allowing the controller to call the same method on the delegate
42+
* class.
4243
*
4344
* @param message to be printed to the screen
4445
*/

delegation/src/main/java/com/iluwatar/delegation/simple/PrinterController.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
package com.iluwatar.delegation.simple;
2525

2626
/**
27-
* Delegator Class to delegate the implementation of the Printer.
28-
* This ensures two things:
29-
* - when the actual implementation of the Printer class changes the delegation will still be operational
30-
* - the actual benefit is observed when there are more than one implementors and they share a delegation control
27+
* Delegator Class to delegate the implementation of the Printer. This ensures two things: - when
28+
* the actual implementation of the Printer class changes the delegation will still be operational -
29+
* the actual benefit is observed when there are more than one implementors and they share a
30+
* delegation control
3131
*/
3232
public class PrinterController implements Printer {
3333

@@ -38,10 +38,10 @@ public PrinterController(Printer printer) {
3838
}
3939

4040
/**
41-
* This method is implemented from {@link Printer} however instead on providing an
42-
* implementation, it instead calls upon the class passed through the constructor. This is the delegate,
43-
* hence the pattern. Therefore meaning that the caller does not care of the implementing class only the owning
44-
* controller.
41+
* This method is implemented from {@link Printer} however instead on providing an implementation,
42+
* it instead calls upon the class passed through the constructor. This is the delegate, hence the
43+
* pattern. Therefore meaning that the caller does not care of the implementing class only the
44+
* owning controller.
4545
*
4646
* @param message to be printed to the screen
4747
*/

delegation/src/main/java/com/iluwatar/delegation/simple/printers/CanonPrinter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import org.slf4j.LoggerFactory;
2929

3030
/**
31-
* Specialised Implementation of {@link Printer} for a Canon Printer, in
32-
* this case the message to be printed is appended to "Canon Printer : "
31+
* Specialised Implementation of {@link Printer} for a Canon Printer, in this case the message to be
32+
* printed is appended to "Canon Printer : ".
3333
*
3434
* @see Printer
3535
*/

delegation/src/main/java/com/iluwatar/delegation/simple/printers/EpsonPrinter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import org.slf4j.LoggerFactory;
2929

3030
/**
31-
* Specialised Implementation of {@link Printer} for a Epson Printer, in
32-
* this case the message to be printed is appended to "Epson Printer : "
31+
* Specialised Implementation of {@link Printer} for a Epson Printer, in this case the message to be
32+
* printed is appended to "Epson Printer : ".
3333
*
3434
* @see Printer
3535
*/

delegation/src/main/java/com/iluwatar/delegation/simple/printers/HpPrinter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
import org.slf4j.LoggerFactory;
2929

3030
/**
31-
* Specialised Implementation of {@link Printer} for a HP Printer, in
32-
* this case the message to be printed is appended to "HP Printer : "
31+
* Specialised Implementation of {@link Printer} for a HP Printer, in this case the message to be
32+
* printed is appended to "HP Printer : ".
3333
*
3434
* @see Printer
3535
*/

dependency-injection/src/main/java/com/iluwatar/dependency/injection/AdvancedSorceress.java

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,6 @@
2323

2424
package com.iluwatar.dependency.injection;
2525

26-
/**
27-
* The MIT License
28-
* Copyright (c) 2014-2017 Ilkka Seppälä
29-
* <p>
30-
* Permission is hereby granted, free of charge, to any person obtaining a copy
31-
* of this software and associated documentation files (the "Software"), to deal
32-
* in the Software without restriction, including without limitation the rights
33-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
34-
* copies of the Software, and to permit persons to whom the Software is
35-
* furnished to do so, subject to the following conditions:
36-
* <p>
37-
* The above copyright notice and this permission notice shall be included in
38-
* all copies or substantial portions of the Software.
39-
* <p>
40-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
41-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
42-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
43-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
44-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
45-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
46-
* THE SOFTWARE.
47-
*/
48-
49-
5026
/**
5127
* AdvancedSorceress implements inversion of control. It depends on abstraction that can be injected
5228
* through its setter.

dependency-injection/src/main/java/com/iluwatar/dependency/injection/AdvancedWizard.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@
2424
package com.iluwatar.dependency.injection;
2525

2626
/**
27-
*
2827
* AdvancedWizard implements inversion of control. It depends on abstraction that can be injected
2928
* through its constructor.
30-
*
3129
*/
3230
public class AdvancedWizard implements Wizard {
3331

dependency-injection/src/main/java/com/iluwatar/dependency/injection/App.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,26 @@
3131
* implements so called inversion of control principle. Inversion of control has two specific rules:
3232
* - High-level modules should not depend on low-level modules. Both should depend on abstractions.
3333
* - Abstractions should not depend on details. Details should depend on abstractions.
34-
* <p>
35-
* In this example we show you three different wizards. The first one ({@link SimpleWizard}) is a
36-
* naive implementation violating the inversion of control principle. It depends directly on a
34+
*
35+
* <p>In this example we show you three different wizards. The first one ({@link SimpleWizard}) is
36+
* a naive implementation violating the inversion of control principle. It depends directly on a
3737
* concrete implementation which cannot be changed.
38-
* <p>
39-
* The second and third wizards({@link AdvancedWizard} and {@link AdvancedSorceress}) are more flexible.
40-
* They do not depend on any concrete implementation but abstraction. They utilizes Dependency Injection
41-
* pattern allowing their {@link Tobacco} dependency to be injected through constructor ({@link AdvancedWizard})
42-
* or setter ({@link AdvancedSorceress}). This way, handling the dependency is no longer the wizard's
43-
* responsibility. It is resolved outside the wizard class.
44-
* <p>
45-
* The fourth example takes the pattern a step further. It uses Guice framework for Dependency
38+
*
39+
* <p>The second and third wizards({@link AdvancedWizard} and {@link AdvancedSorceress}) are more
40+
* flexible. They do not depend on any concrete implementation but abstraction. They utilizes
41+
* Dependency Injection pattern allowing their {@link Tobacco} dependency to be injected through
42+
* constructor ({@link AdvancedWizard}) or setter ({@link AdvancedSorceress}). This way, handling
43+
* the dependency is no longer the wizard's responsibility. It is resolved outside the wizard
44+
* class.
45+
*
46+
* <p>The fourth example takes the pattern a step further. It uses Guice framework for Dependency
4647
* Injection. {@link TobaccoModule} binds a concrete implementation to abstraction. Injector is then
4748
* used to create {@link GuiceWizard} object with correct dependencies.
4849
*/
4950
public class App {
5051

5152
/**
52-
* Program entry point
53+
* Program entry point.
5354
*
5455
* @param args command line args
5556
*/

dependency-injection/src/main/java/com/iluwatar/dependency/injection/GuiceWizard.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@
2626
import javax.inject.Inject;
2727

2828
/**
29-
*
3029
* GuiceWizard implements inversion of control. Its dependencies are injected through its
3130
* constructor by Guice framework.
32-
*
3331
*/
3432
public class GuiceWizard implements Wizard {
3533

dependency-injection/src/main/java/com/iluwatar/dependency/injection/OldTobyTobacco.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
package com.iluwatar.dependency.injection;
2525

2626
/**
27-
*
28-
* OldTobyTobacco concrete {@link Tobacco} implementation
29-
*
27+
* OldTobyTobacco concrete {@link Tobacco} implementation.
3028
*/
3129
public class OldTobyTobacco extends Tobacco {
3230
}

dependency-injection/src/main/java/com/iluwatar/dependency/injection/RivendellTobacco.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
package com.iluwatar.dependency.injection;
2525

2626
/**
27-
*
28-
* RivendellTobacco concrete {@link Tobacco} implementation
29-
*
27+
* RivendellTobacco concrete {@link Tobacco} implementation.
3028
*/
3129
public class RivendellTobacco extends Tobacco {
3230
}

dependency-injection/src/main/java/com/iluwatar/dependency/injection/SecondBreakfastTobacco.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
package com.iluwatar.dependency.injection;
2525

2626
/**
27-
*
28-
* SecondBreakfastTobacco concrete {@link Tobacco} implementation
29-
*
27+
* SecondBreakfastTobacco concrete {@link Tobacco} implementation.
3028
*/
3129
public class SecondBreakfastTobacco extends Tobacco {
3230
}

dependency-injection/src/main/java/com/iluwatar/dependency/injection/SimpleWizard.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,8 @@
2424
package com.iluwatar.dependency.injection;
2525

2626
/**
27-
*
2827
* Naive Wizard implementation violating the inversion of control principle. It should depend on
2928
* abstraction instead.
30-
*
3129
*/
3230
public class SimpleWizard implements Wizard {
3331

dependency-injection/src/main/java/com/iluwatar/dependency/injection/Tobacco.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
import org.slf4j.LoggerFactory;
2828

2929
/**
30-
*
31-
* Tobacco abstraction
32-
*
30+
* Tobacco abstraction.
3331
*/
3432
public abstract class Tobacco {
3533

dependency-injection/src/main/java/com/iluwatar/dependency/injection/TobaccoModule.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
import com.google.inject.AbstractModule;
2727

2828
/**
29-
*
3029
* Guice module for binding certain concrete {@link Tobacco} implementation.
31-
*
3230
*/
3331
public class TobaccoModule extends AbstractModule {
3432

dependency-injection/src/main/java/com/iluwatar/dependency/injection/Wizard.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
package com.iluwatar.dependency.injection;
2525

2626
/**
27-
*
28-
* Wizard interface
29-
*
27+
* Wizard interface.
3028
*/
3129
public interface Wizard {
3230

dirty-flag/src/main/java/com/iluwatar/dirtyflag/App.java

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,45 +23,49 @@
2323

2424
package com.iluwatar.dirtyflag;
2525

26-
import org.slf4j.Logger;
27-
import org.slf4j.LoggerFactory;
28-
2926
import java.util.List;
3027
import java.util.concurrent.Executors;
3128
import java.util.concurrent.ScheduledExecutorService;
3229
import java.util.concurrent.TimeUnit;
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
3332

3433
/**
34+
* This application demonstrates the <b>Dirty Flag</b> pattern. The dirty flag behavioral pattern
35+
* allows you to avoid expensive operations that would just need to be done again anyway. This is a
36+
* simple pattern that really just explains how to add a bool value to your class that you can set
37+
* anytime a property changes. This will let your class know that any results it may have previously
38+
* calculated will need to be calculated again when they’re requested. Once the results are
39+
* re-calculated, then the bool value can be cleared.
3540
*
36-
* This application demonstrates the <b>Dirty Flag</b> pattern. The dirty flag behavioral pattern allows you to avoid
37-
* expensive operations that would just need to be done again anyway. This is a simple pattern that really just explains
38-
* how to add a bool value to your class that you can set anytime a property changes. This will let your class know that
39-
* any results it may have previously calculated will need to be calculated again when they’re requested. Once the
40-
* results are re-calculated, then the bool value can be cleared.
41-
*
42-
* There are some points that need to be considered before diving into using this pattern:- there are some things you’ll
43-
* need to consider:- (1) Do you need it? This design pattern works well when the results to be calculated are difficult
44-
* or resource intensive to compute. You want to save them. You also don’t want to be calculating them several times in
45-
* a row when only the last one counts. (2) When do you set the dirty flag? Make sure that you set the dirty flag within
46-
* the class itself whenever an important property changes. This property should affect the result of the calculated
47-
* result and by changing the property, that makes the last result invalid. (3) When do you clear the dirty flag? It
48-
* might seem obvious that the dirty flag should be cleared whenever the result is calculated with up-to-date
49-
* information but there are other times when you might want to clear the flag.
41+
* <p>There are some points that need to be considered before diving into using this pattern:-
42+
* there are some things you’ll need to consider:- (1) Do you need it? This design pattern works
43+
* well when the results to be calculated are difficult or resource intensive to compute. You want
44+
* to save them. You also don’t want to be calculating them several times in a row when only the
45+
* last one counts. (2) When do you set the dirty flag? Make sure that you set the dirty flag within
46+
* the class itself whenever an important property changes. This property should affect the result
47+
* of the calculated result and by changing the property, that makes the last result invalid. (3)
48+
* When do you clear the dirty flag? It might seem obvious that the dirty flag should be cleared
49+
* whenever the result is calculated with up-to-date information but there are other times when you
50+
* might want to clear the flag.
5051
*
51-
* In this example, the {@link DataFetcher} holds the <i>dirty flag</i>. It fetches and re-fetches from <i>world.txt</i>
52-
* when needed. {@link World} mainly serves the data to the front-end.
52+
* <p>In this example, the {@link DataFetcher} holds the <i>dirty flag</i>. It fetches and
53+
* re-fetches from <i>world.txt</i> when needed. {@link World} mainly serves the data to the
54+
* front-end.
5355
*/
5456
public class App {
55-
57+
5658
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
59+
5760
/**
58-
* Program execution point
61+
* Program execution point.
5962
*/
6063
public void run() {
6164

6265
final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
6366
executorService.scheduleAtFixedRate(new Runnable() {
6467
final World world = new World();
68+
6569
@Override
6670
public void run() {
6771
List<String> countries = world.fetch();
@@ -74,10 +78,9 @@ public void run() {
7478
}
7579

7680
/**
77-
* Program entry point
81+
* Program entry point.
7882
*
79-
* @param args
80-
* command line args
83+
* @param args command line args
8184
*/
8285
public static void main(String[] args) {
8386
App app = new App();

dirty-flag/src/main/java/com/iluwatar/dirtyflag/DataFetcher.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,19 @@
2323

2424
package com.iluwatar.dirtyflag;
2525

26-
import org.slf4j.Logger;
27-
import org.slf4j.LoggerFactory;
28-
29-
import javax.xml.crypto.Data;
3026
import java.io.BufferedReader;
3127
import java.io.File;
3228
import java.io.FileReader;
3329
import java.io.IOException;
3430
import java.util.ArrayList;
3531
import java.util.List;
32+
import org.slf4j.Logger;
33+
import org.slf4j.LoggerFactory;
3634

3735
/**
3836
* A mock database manager -- Fetches data from a raw file.
39-
*
40-
* @author swaisuan
4137
*
38+
* @author swaisuan
4239
*/
4340
public class DataFetcher {
4441

@@ -61,7 +58,7 @@ private boolean isDirty(long fileLastModified) {
6158

6259
/**
6360
* Fetches data/content from raw file.
64-
*
61+
*
6562
* @return List of strings
6663
*/
6764
public List<String> fetch() {

0 commit comments

Comments
 (0)