Skip to content

Commit 5b137ba

Browse files
committed
Refactored the code.
1 parent 2335b43 commit 5b137ba

File tree

3 files changed

+35
-74
lines changed

3 files changed

+35
-74
lines changed

src/SingletonCloningPreventExample.java

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,43 +7,27 @@ public class SingletonCloningPreventExample extends SuperCloneableClass {
77

88
private static SingletonCloningPreventExample single_instance = null;
99

10-
/**
11-
* Override the clone() to throw the same instance
12-
*/
13-
@Override
14-
protected Object clone()
15-
{
16-
return single_instance;
10+
// Making the constructor as private
11+
private SingletonCloningPreventExample() {
1712
}
1813

19-
/**
20-
* Making the constructor as private
21-
*/
22-
private SingletonCloningPreventExample() {}
23-
2414
/**
2515
* Static method to create instance of Singleton class
16+
*
2617
* @return single object of 'SingletonCloningExample' class
2718
*/
2819
public static SingletonCloningPreventExample getInstance() {
29-
/**
30-
* Ensuring only one instance is created
31-
*/
32-
if (single_instance == null)
33-
single_instance = new SingletonCloningPreventExample();
20+
// Ensuring only one instance is created
21+
if (single_instance == null) single_instance = new SingletonCloningPreventExample();
3422
return single_instance;
3523
}
3624

3725
public static void main(String[] args) throws CloneNotSupportedException {
3826

39-
/**
40-
* Instantiating SingletonCloningExample class with variable 'objectOne'
41-
*/
27+
// Instantiating SingletonCloningExample class with variable 'objectOne'
4228
SingletonCloningPreventExample objectOne = SingletonCloningPreventExample.getInstance();
4329

44-
/**
45-
* Cloning the 'objectOne' using clone()
46-
*/
30+
// Cloning the 'objectOne' using clone()
4731
SingletonCloningPreventExample objectTwo = (SingletonCloningPreventExample) objectOne.clone();
4832

4933
/**
@@ -54,13 +38,18 @@ public static void main(String[] args) throws CloneNotSupportedException {
5438
System.out.println("Hashcode of Object 2 - " + objectTwo.hashCode());
5539
}
5640

41+
// Override the clone() to throw the same instance
42+
@Override
43+
protected Object clone() {
44+
return single_instance;
45+
}
46+
5747
}
5848

5949
class SuperCloneableClass implements Cloneable {
6050

6151
@Override
62-
protected Object clone() throws CloneNotSupportedException
63-
{
52+
protected Object clone() throws CloneNotSupportedException {
6453
return super.clone();
6554
}
6655

src/SingletonExample.java

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,33 @@
11
/**
22
* Singleton is a design pattern by which you create a singleton class that has only one instance
33
* at any time
4-
*
54
*/
65
public class SingletonExample {
76

87
private static SingletonExample single_instance = null;
98

109
public String msg;
1110

12-
/**
13-
* Making the constructor as private
14-
*/
11+
// Making the constructor as private
1512
private SingletonExample() {
16-
msg= "Hello World!";
13+
msg = "Hello World!";
1714
}
1815

1916
/**
2017
* Static method to create instance of Singleton class
18+
*
2119
* @return single object of 'SingletonExample' class
2220
*/
2321
public static SingletonExample getInstance() {
24-
/**
25-
* Ensuring only one instance is created
26-
*/
27-
if (single_instance == null)
28-
single_instance = new SingletonExample();
22+
// Ensuring only one instance is created
23+
if (single_instance == null) single_instance = new SingletonExample();
2924
return single_instance;
3025
}
3126

3227
public static void main(String[] args) {
33-
34-
/**
35-
* Instantiating SingletonExample class with variable 'singletonObject'
36-
*/
28+
// Instantiating SingletonExample class with variable 'singletonObject'
3729
SingletonExample singletonObject = SingletonExample.getInstance();
38-
3930
System.out.println(singletonObject.msg);
40-
4131
}
4232

4333
}

src/StreamsExample.java

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import java.sql.Timestamp;
2-
import java.util.*;
1+
import java.util.ArrayList;
2+
import java.util.Arrays;
3+
import java.util.IntSummaryStatistics;
4+
import java.util.List;
35
import java.util.stream.IntStream;
46
import java.util.stream.Stream;
57

@@ -45,86 +47,66 @@ public static void main(String[] args) {
4547
* ********************* MORE STREAM EXAMPLES ************************
4648
*/
4749

48-
/**
49-
* Example 1: This would print 1-9 in the console
50-
*/
50+
// Example 1: This would print 1-9 in the console
5151
IntStream
5252
.range(1, 10) // .range(inclusiveNum, exclusiveNum)
5353
.forEach(System.out::print);
5454
System.out.println();
5555

56-
/**
57-
* Example 2: IntStream with skip
58-
*/
56+
// Example 2: IntStream with skip
5957
IntStream
6058
.range(1, 10)
6159
.skip(5)
62-
.forEach(x -> System.out.print(x));
60+
.forEach(System.out::print);
6361
System.out.println();
6462

65-
/**
66-
* Example 3: IntStream with sum()
67-
*/
63+
// Example 3: IntStream with sum()
6864
int sumOfNumbers =
6965
IntStream
7066
.range(1, 5)
7167
.sum();
7268
System.out.println(sumOfNumbers);
7369

74-
/**
75-
* Example 4: Stream.of with sorted() and findFirst()
76-
*/
70+
// Example 4: Stream.of with sorted() and findFirst()
7771
Stream.of("Vivek", "Abi", "Anoop", "Arun")
7872
.sorted()
7973
.findFirst()
8074
.ifPresent(System.out::print);
8175
System.out.println();
8276

83-
/**
84-
* Example 5: Stream from Array, sort, filter
85-
*/
86-
77+
// Example 5: Stream from Array, sort, filter
8778
String[] names = {"Bruce", "Tony" , "Banner", "Sebastian", "Bertha"};
8879
Arrays.stream(names)
8980
.filter(s -> s.startsWith("B"))
9081
.sorted()
9182
.forEach(System.out::println);
9283

93-
/**
94-
* Example 6: Average of squares of an int array
95-
*/
84+
// Example 6: Average of squares of an int array
9685
Arrays.stream(new int[] {2, 4, 6, 8, 10})
9786
.map(x -> x*x)
9887
.average()
9988
.ifPresent(System.out::print);
10089
System.out.println();
10190

102-
/**
103-
* Example 7: Stream from List, filter and print
104-
*/
91+
// Example 7: Stream from List, filter and print
10592
List<String> people = Arrays.asList("Bruce", "Tony" , "Banner", "Sebastian", "Bertha", "Spyder");
10693
people
10794
.stream()
10895
.map(String::toUpperCase)
10996
.filter(x -> x.startsWith("S"))
11097
.forEach(System.out::println);
11198

112-
/**
113-
* Example 8: Reduction and Sum
114-
*/
99+
// Example 8: Reduction and Sum
115100
double total = Stream.of(7.3, 6.4, 34.6)
116-
.reduce(0.0, (Double a, Double b) -> a + b);
101+
.reduce(0.0, Double::sum);
117102
System.out.println(total);
118103

119104

120-
/**
121-
* Example 9: Reduction and Sum
122-
*/
105+
// Example 9: Reduction and Sum
123106
IntSummaryStatistics summary = IntStream.of(12, 4, 46, 8, 10, 76, 34)
124107
.summaryStatistics();
125108
System.out.println(summary);
126109
// Output is: IntSummaryStatistics{count=7, sum=190, min=4, average=27.142857, max=76}
127110

128-
129111
}
130112
}

0 commit comments

Comments
 (0)