X Tutup
Skip to content

Commit 60f9b71

Browse files
committed
eliminate all warnings of checkstyle.
1 parent 3680953 commit 60f9b71

File tree

6 files changed

+125
-128
lines changed

6 files changed

+125
-128
lines changed
Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,63 @@
11
/**
22
* Singleton pattern.
33
*/
4+
45
package com.iluwatar.singleton;
56

67
/**
7-
*
88
* Singleton pattern ensures that the class ({@link IvoryTower}) can have only one
9-
* existing instance per Java classloader instance and provides global access to it.
10-
* <p>
9+
* existing instance per Java classloader instance and provides global access to it.
10+
* <p/>
1111
* http://stackoverflow.com/questions/70689/what-is-an-efficient-way-to-implement-a-singleton-pattern-in-java
12-
*<p>
13-
* The risk of this pattern is that bugs resulting from setting a singleton up in a distributed environment can
14-
* be tricky to debug, since it will work fine if you debug with a single classloader. Additionally, these
15-
* problems can crop up a while after the implementation of a singleton, since they may start out synchronous and
16-
* only become async with time, so you it may not be clear why you are seeing certain changes in behaviour.
17-
* <p>
12+
* <p/>
13+
* The risk of this pattern is that bugs resulting from setting a singleton up in
14+
* a distributed environment can be tricky to debug, since it will work fine if you
15+
* debug with a single classloader. Additionally, these problems can crop up a while
16+
* after the implementation of a singleton, since they may start out synchronous and
17+
* only become async with time, so you it may not be clear why you are seeing certain
18+
* changes in behaviour.
19+
* <p/>
1820
* http://stackoverflow.com/questions/17721263/singleton-across-jvm-or-application-instance-or-tomcat-instance
1921
*/
2022
public class App {
2123

22-
/**
23-
* Program entry point
24-
* @param args command line args
25-
*/
26-
public static void main(String[] args) {
27-
28-
// eagerly initialized singleton
29-
IvoryTower ivoryTower1 = IvoryTower.getInstance();
30-
IvoryTower ivoryTower2 = IvoryTower.getInstance();
31-
System.out.println("ivoryTower1=" + ivoryTower1);
32-
System.out.println("ivoryTower2=" + ivoryTower2);
33-
34-
// lazily initialized singleton
35-
ThreadSafeLazyLoadedIvoryTower threadSafeIvoryTower1 = ThreadSafeLazyLoadedIvoryTower
36-
.getInstance();
37-
ThreadSafeLazyLoadedIvoryTower threadSafeIvoryTower2 = ThreadSafeLazyLoadedIvoryTower
38-
.getInstance();
39-
System.out.println("threadSafeIvoryTower1=" + threadSafeIvoryTower1);
40-
System.out.println("threadSafeIvoryTower2=" + threadSafeIvoryTower2);
41-
42-
// enum singleton
43-
EnumIvoryTower enumIvoryTower1 = EnumIvoryTower.INSTANCE;
44-
EnumIvoryTower enumIvoryTower2 = EnumIvoryTower.INSTANCE;
45-
System.out.println("enumIvoryTower1=" + enumIvoryTower1);
46-
System.out.println("enumIvoryTower2=" + enumIvoryTower2);
47-
48-
InitializingOnDemandHolderIdiom demandHolderIdiom = InitializingOnDemandHolderIdiom.getInstance();
49-
System.out.println(demandHolderIdiom);
50-
InitializingOnDemandHolderIdiom demandHolderIdiom2 = InitializingOnDemandHolderIdiom.getInstance();
51-
System.out.println(demandHolderIdiom2);
52-
53-
ThreadSafeDoubleCheckLocking dcl1 = ThreadSafeDoubleCheckLocking.getInstance();
54-
System.out.println(dcl1);
55-
ThreadSafeDoubleCheckLocking dcl2 = ThreadSafeDoubleCheckLocking.getInstance();
56-
System.out.println(dcl2);
57-
}
24+
/**
25+
* Program entry point.
26+
*
27+
* @param args command line args
28+
*/
29+
public static void main(String[] args) {
30+
31+
// eagerly initialized singleton
32+
IvoryTower ivoryTower1 = IvoryTower.getInstance();
33+
IvoryTower ivoryTower2 = IvoryTower.getInstance();
34+
System.out.println("ivoryTower1=" + ivoryTower1);
35+
System.out.println("ivoryTower2=" + ivoryTower2);
36+
37+
// lazily initialized singleton
38+
ThreadSafeLazyLoadedIvoryTower threadSafeIvoryTower1 = ThreadSafeLazyLoadedIvoryTower
39+
.getInstance();
40+
ThreadSafeLazyLoadedIvoryTower threadSafeIvoryTower2 = ThreadSafeLazyLoadedIvoryTower
41+
.getInstance();
42+
System.out.println("threadSafeIvoryTower1=" + threadSafeIvoryTower1);
43+
System.out.println("threadSafeIvoryTower2=" + threadSafeIvoryTower2);
44+
45+
// enum singleton
46+
EnumIvoryTower enumIvoryTower1 = EnumIvoryTower.INSTANCE;
47+
EnumIvoryTower enumIvoryTower2 = EnumIvoryTower.INSTANCE;
48+
System.out.println("enumIvoryTower1=" + enumIvoryTower1);
49+
System.out.println("enumIvoryTower2=" + enumIvoryTower2);
50+
51+
InitializingOnDemandHolderIdiom demandHolderIdiom =
52+
InitializingOnDemandHolderIdiom.getInstance();
53+
System.out.println(demandHolderIdiom);
54+
InitializingOnDemandHolderIdiom demandHolderIdiom2 =
55+
InitializingOnDemandHolderIdiom.getInstance();
56+
System.out.println(demandHolderIdiom2);
57+
58+
ThreadSafeDoubleCheckLocking dcl1 = ThreadSafeDoubleCheckLocking.getInstance();
59+
System.out.println(dcl1);
60+
ThreadSafeDoubleCheckLocking dcl2 = ThreadSafeDoubleCheckLocking.getInstance();
61+
System.out.println(dcl2);
62+
}
5863
}
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
/**
2-
* Enum Singleton class.
3-
* Effective Java 2nd Edition (Joshua Bloch) p. 18
4-
*/
51
package com.iluwatar.singleton;
62

73
/**
84
* Enum based singleton implementation.
5+
* Effective Java 2nd Edition (Joshua Bloch) p. 18
96
*/
107
public enum EnumIvoryTower {
118

12-
INSTANCE;
9+
INSTANCE;
1310

14-
@Override
15-
public String toString() {
16-
return getDeclaringClass().getCanonicalName() + "@" + hashCode();
17-
}
11+
@Override
12+
public String toString() {
13+
return getDeclaringClass().getCanonicalName() + "@" + hashCode();
14+
}
1815
}
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/**
2-
* Singleton pattern.
3-
*/
41
package com.iluwatar.singleton;
52

63
import java.io.Serializable;
@@ -18,21 +15,22 @@
1815
*/
1916
public class InitializingOnDemandHolderIdiom implements Serializable {
2017

21-
private static final long serialVersionUID = 1L;
18+
private static final long serialVersionUID = 1L;
2219

23-
private InitializingOnDemandHolderIdiom() {
24-
}
20+
private InitializingOnDemandHolderIdiom() {
21+
}
2522

26-
public static InitializingOnDemandHolderIdiom getInstance() {
27-
return HelperHolder.INSTANCE;
28-
}
23+
public static InitializingOnDemandHolderIdiom getInstance() {
24+
return HelperHolder.INSTANCE;
25+
}
2926

30-
protected Object readResolve() {
31-
return getInstance();
32-
}
27+
protected Object readResolve() {
28+
return getInstance();
29+
}
3330

34-
private static class HelperHolder {
35-
public static final InitializingOnDemandHolderIdiom INSTANCE = new InitializingOnDemandHolderIdiom();
36-
}
31+
private static class HelperHolder {
32+
public static final InitializingOnDemandHolderIdiom INSTANCE =
33+
new InitializingOnDemandHolderIdiom();
34+
}
3735

3836
}
Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/**
2-
* Singleton pattern.
3-
*/
41
package com.iluwatar.singleton;
52

63
/**
@@ -10,24 +7,24 @@
107
*/
118
public final class IvoryTower {
129

13-
/**
14-
* Static to class instance of the class.
15-
*/
16-
private static IvoryTower instance = new IvoryTower();
10+
/**
11+
* Static to class instance of the class.
12+
*/
13+
private static IvoryTower instance = new IvoryTower();
1714

18-
/**
19-
* Private constructor so nobody can instantiate the class.
20-
*/
21-
private IvoryTower() {
22-
}
15+
/**
16+
* Private constructor so nobody can instantiate the class.
17+
*/
18+
private IvoryTower() {
19+
}
2320

24-
/**
25-
* To be called by user to
26-
* obtain instance of the class.
27-
*
28-
* @return instance of the singleton.
29-
*/
30-
public static IvoryTower getInstance() {
31-
return instance;
32-
}
21+
/**
22+
* To be called by user to
23+
* obtain instance of the class.
24+
*
25+
* @return instance of the singleton.
26+
*/
27+
public static IvoryTower getInstance() {
28+
return instance;
29+
}
3330
}
Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/**
2-
* Singleton pattern.
3-
*
4-
*/
51
package com.iluwatar.singleton;
62

73
/**
@@ -15,29 +11,35 @@
1511
*/
1612
public class ThreadSafeDoubleCheckLocking {
1713

18-
private static volatile ThreadSafeDoubleCheckLocking INSTANCE;
14+
private static volatile ThreadSafeDoubleCheckLocking INSTANCE;
1915

20-
/**
21-
* private constructor to prevent client from instantiating.
22-
*/
23-
private ThreadSafeDoubleCheckLocking() {
24-
//to prevent instantiating by Reflection call
25-
if (INSTANCE != null)
26-
throw new IllegalStateException("Already initialized.");
16+
/**
17+
* private constructor to prevent client from instantiating.
18+
*/
19+
private ThreadSafeDoubleCheckLocking() {
20+
//to prevent instantiating by Reflection call
21+
if (INSTANCE != null) {
22+
throw new IllegalStateException("Already initialized.");
2723
}
24+
}
2825

29-
public static ThreadSafeDoubleCheckLocking getInstance() {
30-
//local variable increases performance by 25 percent
31-
//Joshua Bloch "Effective Java, Second Edition", p. 283-284
32-
ThreadSafeDoubleCheckLocking result = INSTANCE;
26+
/**
27+
* Public accessor.
28+
*
29+
* @return an instance of the class.
30+
*/
31+
public static ThreadSafeDoubleCheckLocking getInstance() {
32+
//local variable increases performance by 25 percent
33+
//Joshua Bloch "Effective Java, Second Edition", p. 283-284
34+
ThreadSafeDoubleCheckLocking result = INSTANCE;
35+
if (result == null) {
36+
synchronized (ThreadSafeDoubleCheckLocking.class) {
37+
result = INSTANCE;
3338
if (result == null) {
34-
synchronized (ThreadSafeDoubleCheckLocking.class) {
35-
result = INSTANCE;
36-
if (result == null) {
37-
INSTANCE = result = new ThreadSafeDoubleCheckLocking();
38-
}
39-
}
39+
INSTANCE = result = new ThreadSafeDoubleCheckLocking();
4040
}
41-
return result;
41+
}
4242
}
43+
return result;
44+
}
4345
}
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/**
2-
* Singleton pattern.
3-
*/
41
package com.iluwatar.singleton;
52

63
/**
@@ -10,20 +7,21 @@
107
*/
118
public class ThreadSafeLazyLoadedIvoryTower {
129

13-
private static ThreadSafeLazyLoadedIvoryTower instance = null;
10+
private static ThreadSafeLazyLoadedIvoryTower instance = null;
1411

15-
private ThreadSafeLazyLoadedIvoryTower() {
16-
}
12+
private ThreadSafeLazyLoadedIvoryTower() {
13+
}
1714

18-
public synchronized static ThreadSafeLazyLoadedIvoryTower getInstance() {
19-
/*
20-
* The instance gets created only when it is called for first time.
21-
* Lazy-loading
22-
*/
23-
if (instance == null) {
24-
instance = new ThreadSafeLazyLoadedIvoryTower();
25-
}
15+
/**
16+
* The instance gets created only when it is called for first time.
17+
* Lazy-loading
18+
*/
19+
public synchronized static ThreadSafeLazyLoadedIvoryTower getInstance() {
2620

27-
return instance;
21+
if (instance == null) {
22+
instance = new ThreadSafeLazyLoadedIvoryTower();
2823
}
24+
25+
return instance;
26+
}
2927
}

0 commit comments

Comments
 (0)
X Tutup