X Tutup
Skip to content

Commit 3680953

Browse files
committed
checkstyle fixes - docs, indent etc
1 parent 6735c81 commit 3680953

File tree

6 files changed

+132
-110
lines changed

6 files changed

+132
-110
lines changed
Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Singleton pattern.
3+
*/
14
package com.iluwatar.singleton;
25

36
/**
@@ -16,40 +19,40 @@
1619
*/
1720
public class App {
1821

19-
/**
20-
* Program entry point
21-
* @param args command line args
22-
*/
23-
public static void main(String[] args) {
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);
2441

25-
// eagerly initialized singleton
26-
IvoryTower ivoryTower1 = IvoryTower.getInstance();
27-
IvoryTower ivoryTower2 = IvoryTower.getInstance();
28-
System.out.println("ivoryTower1=" + ivoryTower1);
29-
System.out.println("ivoryTower2=" + ivoryTower2);
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);
3047

31-
// lazily initialized singleton
32-
ThreadSafeLazyLoadedIvoryTower threadSafeIvoryTower1 = ThreadSafeLazyLoadedIvoryTower
33-
.getInstance();
34-
ThreadSafeLazyLoadedIvoryTower threadSafeIvoryTower2 = ThreadSafeLazyLoadedIvoryTower
35-
.getInstance();
36-
System.out.println("threadSafeIvoryTower1=" + threadSafeIvoryTower1);
37-
System.out.println("threadSafeIvoryTower2=" + threadSafeIvoryTower2);
48+
InitializingOnDemandHolderIdiom demandHolderIdiom = InitializingOnDemandHolderIdiom.getInstance();
49+
System.out.println(demandHolderIdiom);
50+
InitializingOnDemandHolderIdiom demandHolderIdiom2 = InitializingOnDemandHolderIdiom.getInstance();
51+
System.out.println(demandHolderIdiom2);
3852

39-
// enum singleton
40-
EnumIvoryTower enumIvoryTower1 = EnumIvoryTower.INSTANCE;
41-
EnumIvoryTower enumIvoryTower2 = EnumIvoryTower.INSTANCE;
42-
System.out.println("enumIvoryTower1=" + enumIvoryTower1);
43-
System.out.println("enumIvoryTower2=" + enumIvoryTower2);
44-
45-
InitializingOnDemandHolderIdiom demandHolderIdiom = InitializingOnDemandHolderIdiom.getInstance();
46-
System.out.println(demandHolderIdiom);
47-
InitializingOnDemandHolderIdiom demandHolderIdiom2 = InitializingOnDemandHolderIdiom.getInstance();
48-
System.out.println(demandHolderIdiom2);
49-
50-
ThreadSafeDoubleCheckLocking dcl1 = ThreadSafeDoubleCheckLocking.getInstance();
51-
System.out.println(dcl1);
52-
ThreadSafeDoubleCheckLocking dcl2 = ThreadSafeDoubleCheckLocking.getInstance();
53-
System.out.println(dcl2);
54-
}
53+
ThreadSafeDoubleCheckLocking dcl1 = ThreadSafeDoubleCheckLocking.getInstance();
54+
System.out.println(dcl1);
55+
ThreadSafeDoubleCheckLocking dcl2 = ThreadSafeDoubleCheckLocking.getInstance();
56+
System.out.println(dcl2);
57+
}
5558
}

singleton/src/main/java/com/iluwatar/singleton/EnumIvoryTower.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
package com.iluwatar.singleton;
2-
31
/**
4-
*
52
* Enum Singleton class.
63
* Effective Java 2nd Edition (Joshua Bloch) p. 18
7-
*
4+
*/
5+
package com.iluwatar.singleton;
6+
7+
/**
8+
* Enum based singleton implementation.
89
*/
910
public enum EnumIvoryTower {
10-
11+
1112
INSTANCE;
1213

1314
@Override
Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,38 @@
1+
/**
2+
* Singleton pattern.
3+
*/
14
package com.iluwatar.singleton;
25

36
import java.io.Serializable;
47

58
/**
6-
* The Initialize-on-demand-holder idiom is a secure way of
9+
* The Initialize-on-demand-holder idiom is a secure way of
710
* creating lazy initialized singleton object in Java.
811
* refer to "The CERT Oracle Secure Coding Standard for Java"
912
* By Dhruv Mohindra, Robert C. Seacord p.378
10-
* <p>
13+
* <p/>
1114
* Singleton objects usually are heavy to create and sometimes need to serialize them.
1215
* This class also shows how to preserve singleton in serialized version of singleton.
13-
*
14-
* @author mortezaadi@gmail.com
1516
*
17+
* @author mortezaadi@gmail.com
1618
*/
17-
public class InitializingOnDemandHolderIdiom implements Serializable{
19+
public class InitializingOnDemandHolderIdiom implements Serializable {
1820

19-
private static final long serialVersionUID = 1L;
21+
private static final long serialVersionUID = 1L;
2022

21-
private static class HelperHolder {
22-
public static final InitializingOnDemandHolderIdiom INSTANCE = new InitializingOnDemandHolderIdiom();
23-
}
23+
private InitializingOnDemandHolderIdiom() {
24+
}
2425

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

29-
private InitializingOnDemandHolderIdiom() {
30-
}
30+
protected Object readResolve() {
31+
return getInstance();
32+
}
3133

32-
protected Object readResolve() {
33-
return getInstance();
34-
}
34+
private static class HelperHolder {
35+
public static final InitializingOnDemandHolderIdiom INSTANCE = new InitializingOnDemandHolderIdiom();
36+
}
3537

3638
}
Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
1+
/**
2+
* Singleton pattern.
3+
*/
14
package com.iluwatar.singleton;
25

36
/**
4-
*
57
* Singleton class.
68
* Eagerly initialized static instance guarantees thread
79
* safety.
8-
*
910
*/
10-
public class IvoryTower {
11+
public final class IvoryTower {
1112

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

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

17-
public static IvoryTower getInstance() {
18-
return instance;
19-
}
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+
}
2033
}
Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
1+
/**
2+
* Singleton pattern.
3+
*
4+
*/
15
package com.iluwatar.singleton;
26

37
/**
48
* Double check locking
5-
* <p>
9+
* <p/>
610
* http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html
7-
* <p>
11+
* <p/>
812
* Broken under Java 1.4.
9-
*
10-
* @author mortezaadi@gmail.com
1113
*
14+
* @author mortezaadi@gmail.com
1215
*/
1316
public class ThreadSafeDoubleCheckLocking {
14-
15-
private static volatile ThreadSafeDoubleCheckLocking INSTANCE;
1617

17-
/**
18-
* private constructor to prevent client from instantiating.
19-
*
20-
*/
21-
private ThreadSafeDoubleCheckLocking() {
22-
//to prevent instantiating by Reflection call
23-
if(INSTANCE != null)
24-
throw new IllegalStateException("Already initialized.");
25-
}
26-
27-
public static ThreadSafeDoubleCheckLocking getInstance() {
28-
//local variable increases performance by 25 percent
29-
//Joshua Bloch "Effective Java, Second Edition", p. 283-284
30-
ThreadSafeDoubleCheckLocking result = INSTANCE;
31-
if (result == null) {
32-
synchronized (ThreadSafeDoubleCheckLocking.class) {
33-
result = INSTANCE;
34-
if (result == null) {
35-
INSTANCE = result = new ThreadSafeDoubleCheckLocking();
36-
}
37-
}
38-
}
39-
return result;
40-
}
18+
private static volatile ThreadSafeDoubleCheckLocking INSTANCE;
19+
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.");
27+
}
28+
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;
33+
if (result == null) {
34+
synchronized (ThreadSafeDoubleCheckLocking.class) {
35+
result = INSTANCE;
36+
if (result == null) {
37+
INSTANCE = result = new ThreadSafeDoubleCheckLocking();
38+
}
39+
}
40+
}
41+
return result;
42+
}
4143
}
Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1+
/**
2+
* Singleton pattern.
3+
*/
14
package com.iluwatar.singleton;
25

36
/**
4-
*
57
* Thread-safe Singleton class.
68
* The instance is lazily initialized and thus needs synchronization
79
* mechanism.
8-
*
910
*/
1011
public class ThreadSafeLazyLoadedIvoryTower {
1112

12-
private static ThreadSafeLazyLoadedIvoryTower instance = null;
13-
14-
private ThreadSafeLazyLoadedIvoryTower() {
15-
}
13+
private static ThreadSafeLazyLoadedIvoryTower instance = null;
14+
15+
private ThreadSafeLazyLoadedIvoryTower() {
16+
}
1617

17-
public synchronized static ThreadSafeLazyLoadedIvoryTower getInstance() {
18-
/*
19-
* The instance gets created only when it is called for first time.
20-
* Lazy-loading
21-
*/
22-
if (instance == null) {
23-
instance = new ThreadSafeLazyLoadedIvoryTower();
24-
}
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+
}
2526

26-
return instance;
27-
}
27+
return instance;
28+
}
2829
}

0 commit comments

Comments
 (0)
X Tutup