Skip to content

Commit 3907951

Browse files
anuragagarwal561994iluwatar
authored andcommitted
Resolves checkstyle issues for semaphore servant serverless service-layer service-locator (iluwatar#1079)
* Reduces checkstyle errors in semaphore * Reduces checkstyle errors in servant * Reduces checkstyle errors in serverless * Reduces checkstyle errors in service-layer * Reduces checkstyle errors in service-locator
1 parent 37599eb commit 3907951

40 files changed

+211
-274
lines changed

semaphore/src/main/java/com/iluwatar/semaphore/App.java

+9-10
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,17 @@
2525

2626
/**
2727
* A Semaphore mediates access by a group of threads to a pool of resources.
28-
* <p>
29-
* In this example a group of customers are taking fruit from a fruit shop.
30-
* There is a bowl each of apples, oranges and lemons. Only one customer can
31-
* access a bowl simultaneously. A Semaphore is used to indicate how many
32-
* resources are currently available and must be acquired in order for a bowl
33-
* to be given to a customer. Customers continually try to take fruit until
34-
* there is no fruit left in the shop.
28+
*
29+
* <p>In this example a group of customers are taking fruit from a fruit shop. There is a bowl each
30+
* of apples, oranges and lemons. Only one customer can access a bowl simultaneously. A Semaphore is
31+
* used to indicate how many resources are currently available and must be acquired in order for a
32+
* bowl to be given to a customer. Customers continually try to take fruit until there is no fruit
33+
* left in the shop.
3534
*/
3635
public class App {
37-
36+
3837
/**
39-
* main method
38+
* main method.
4039
*/
4140
public static void main(String[] args) {
4241
FruitShop shop = new FruitShop();
@@ -47,5 +46,5 @@ public static void main(String[] args) {
4746
new Customer("Ringo", shop).start();
4847
new Customer("George", shop).start();
4948
}
50-
49+
5150
}

semaphore/src/main/java/com/iluwatar/semaphore/Customer.java

+13-14
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import org.slf4j.LoggerFactory;
2828

2929
/**
30-
* A Customer attempts to repeatedly take Fruit from the FruitShop by
31-
* taking Fruit from FruitBowl instances.
30+
* A Customer attempts to repeatedly take Fruit from the FruitShop by taking Fruit from FruitBowl
31+
* instances.
3232
*/
3333
public class Customer extends Thread {
3434

@@ -38,36 +38,35 @@ public class Customer extends Thread {
3838
* Name of the Customer.
3939
*/
4040
private final String name;
41-
41+
4242
/**
4343
* The FruitShop he is using.
4444
*/
4545
private final FruitShop fruitShop;
46-
46+
4747
/**
4848
* Their bowl of Fruit.
4949
*/
5050
private final FruitBowl fruitBowl;
51-
51+
5252
/**
53-
* Customer constructor
53+
* Customer constructor.
5454
*/
5555
public Customer(String name, FruitShop fruitShop) {
5656
this.name = name;
5757
this.fruitShop = fruitShop;
5858
this.fruitBowl = new FruitBowl();
5959
}
60-
60+
6161
/**
62-
* The Customer repeatedly takes Fruit from the FruitShop until no Fruit
63-
* remains.
64-
*/
62+
* The Customer repeatedly takes Fruit from the FruitShop until no Fruit remains.
63+
*/
6564
public void run() {
66-
65+
6766
while (fruitShop.countFruit() > 0) {
6867
FruitBowl bowl = fruitShop.takeBowl();
6968
Fruit fruit;
70-
69+
7170
if (bowl != null && (fruit = bowl.take()) != null) {
7271
LOGGER.info("{} took an {}", name, fruit);
7372
fruitBowl.put(fruit);
@@ -76,7 +75,7 @@ public void run() {
7675
}
7776

7877
LOGGER.info("{} took {}", name, fruitBowl);
79-
78+
8079
}
81-
80+
8281
}

semaphore/src/main/java/com/iluwatar/semaphore/Fruit.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
public class Fruit {
3030

3131
/**
32-
* Enumeration of Fruit Types
32+
* Enumeration of Fruit Types.
3333
*/
3434
public enum FruitType {
3535
ORANGE, APPLE, LEMON
@@ -46,7 +46,7 @@ public FruitType getType() {
4646
}
4747

4848
/**
49-
* toString method
49+
* toString method.
5050
*/
5151
public String toString() {
5252
switch (type) {

semaphore/src/main/java/com/iluwatar/semaphore/FruitBowl.java

+14-12
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,37 @@
2323

2424
package com.iluwatar.semaphore;
2525

26-
import java.util.List;
2726
import java.util.ArrayList;
27+
import java.util.List;
2828

2929
/**
30-
* A FruitBowl contains Fruit.
30+
* A FruitBowl contains Fruit.
3131
*/
3232
public class FruitBowl {
33-
33+
3434
private List<Fruit> fruit = new ArrayList<>();
3535

3636
/**
37-
*
38-
* @return The amount of Fruit left in the bowl.
37+
* Returns the amount of fruits left in bowl.
38+
*
39+
* @return The amount of Fruit left in the bowl.
3940
*/
4041
public int countFruit() {
4142
return fruit.size();
4243
}
4344

4445
/**
4546
* Put an item of Fruit into the bowl.
46-
*
47+
*
4748
* @param f fruit
4849
*/
4950
public void put(Fruit f) {
5051
fruit.add(f);
5152
}
52-
53+
5354
/**
5455
* Take an item of Fruit out of the bowl.
56+
*
5557
* @return The Fruit taken out of the bowl, or null if empty.
5658
*/
5759
public Fruit take() {
@@ -61,15 +63,15 @@ public Fruit take() {
6163
return fruit.remove(0);
6264
}
6365
}
64-
66+
6567
/**
66-
* toString method
67-
*/
68+
* toString method.
69+
*/
6870
public String toString() {
6971
int apples = 0;
7072
int oranges = 0;
7173
int lemons = 0;
72-
74+
7375
for (Fruit f : fruit) {
7476
switch (f.getType()) {
7577
case APPLE:
@@ -84,7 +86,7 @@ public String toString() {
8486
default:
8587
}
8688
}
87-
89+
8890
return apples + " Apples, " + oranges + " Oranges, and " + lemons + " Lemons";
8991
}
9092
}

semaphore/src/main/java/com/iluwatar/semaphore/FruitShop.java

+26-27
Original file line numberDiff line numberDiff line change
@@ -27,63 +27,63 @@
2727
* A FruitShop contains three FruitBowl instances and controls access to them.
2828
*/
2929
public class FruitShop {
30-
30+
3131
/**
3232
* The FruitBowl instances stored in the class.
3333
*/
3434
private FruitBowl[] bowls = {
35-
new FruitBowl(),
36-
new FruitBowl(),
37-
new FruitBowl()
35+
new FruitBowl(),
36+
new FruitBowl(),
37+
new FruitBowl()
3838
};
3939

4040
/**
4141
* Access flags for each of the FruitBowl instances.
4242
*/
4343
private boolean[] available = {
44-
true,
45-
true,
46-
true
44+
true,
45+
true,
46+
true
4747
};
4848

4949
/**
5050
* The Semaphore that controls access to the class resources.
5151
*/
5252
private Semaphore semaphore;
53-
53+
5454
/**
55-
* FruitShop constructor
55+
* FruitShop constructor.
5656
*/
5757
public FruitShop() {
5858
for (int i = 0; i < 100; i++) {
5959
bowls[0].put(new Fruit(Fruit.FruitType.APPLE));
6060
bowls[1].put(new Fruit(Fruit.FruitType.ORANGE));
6161
bowls[2].put(new Fruit(Fruit.FruitType.LEMON));
6262
}
63-
63+
6464
semaphore = new Semaphore(3);
6565
}
6666

6767
/**
68-
*
68+
* Returns the amount of fruits left in shop.
69+
*
6970
* @return The amount of Fruit left in the shop.
7071
*/
7172
public synchronized int countFruit() {
7273
return bowls[0].countFruit() + bowls[1].countFruit() + bowls[2].countFruit();
7374
}
74-
75+
7576
/**
76-
* Method called by Customer to get a FruitBowl from the shop. This method
77-
* will try to acquire the Semaphore before returning the first available
78-
* FruitBowl.
79-
*/
77+
* Method called by Customer to get a FruitBowl from the shop. This method will try to acquire the
78+
* Semaphore before returning the first available FruitBowl.
79+
*/
8080
public synchronized FruitBowl takeBowl() {
81-
81+
8282
FruitBowl bowl = null;
83-
83+
8484
try {
8585
semaphore.acquire();
86-
86+
8787
if (available[0]) {
8888
bowl = bowls[0];
8989
available[0] = false;
@@ -94,28 +94,27 @@ public synchronized FruitBowl takeBowl() {
9494
bowl = bowls[2];
9595
available[2] = false;
9696
}
97-
97+
9898
} catch (InterruptedException e) {
9999
e.printStackTrace();
100100
} finally {
101101
semaphore.release();
102102
}
103103
return bowl;
104104
}
105-
105+
106106
/**
107-
* Method called by a Customer instance to return a FruitBowl to the shop.
108-
* This method releases the Semaphore, making the FruitBowl available to
109-
* another Customer.
110-
*/
107+
* Method called by a Customer instance to return a FruitBowl to the shop. This method releases
108+
* the Semaphore, making the FruitBowl available to another Customer.
109+
*/
111110
public synchronized void returnBowl(FruitBowl bowl) {
112111
if (bowl == bowls[0]) {
113112
available[0] = true;
114113
} else if (bowl == bowls[1]) {
115114
available[1] = true;
116115
} else if (bowl == bowls[2]) {
117-
available [2] = true;
116+
available[2] = true;
118117
}
119118
}
120-
119+
121120
}

semaphore/src/main/java/com/iluwatar/semaphore/Lock.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
* Lock is an interface for a lock which can be acquired and released.
2828
*/
2929
public interface Lock {
30-
30+
3131
void acquire() throws InterruptedException;
32-
32+
3333
void release();
34-
34+
3535
}

semaphore/src/main/java/com/iluwatar/semaphore/Semaphore.java

+11-12
Original file line numberDiff line numberDiff line change
@@ -33,38 +33,37 @@ public class Semaphore implements Lock {
3333
* The number of concurrent resource accesses which are allowed.
3434
*/
3535
private int counter;
36-
36+
3737
public Semaphore(int licenses) {
3838
this.licenses = licenses;
39-
this.counter = licenses;
39+
this.counter = licenses;
4040
}
41-
41+
4242
/**
43-
* Returns the number of licenses managed by the Semaphore
43+
* Returns the number of licenses managed by the Semaphore.
4444
*/
4545
public int getNumLicenses() {
4646
return licenses;
4747
}
48-
48+
4949
/**
50-
* Returns the number of available licenses
50+
* Returns the number of available licenses.
5151
*/
5252
public int getAvailableLicenses() {
53-
return counter;
53+
return counter;
5454
}
55-
55+
5656
/**
57-
* Method called by a thread to acquire the lock. If there are no resources
58-
* available this will wait until the lock has been released to re-attempt
59-
* the acquire.
57+
* Method called by a thread to acquire the lock. If there are no resources available this will
58+
* wait until the lock has been released to re-attempt the acquire.
6059
*/
6160
public synchronized void acquire() throws InterruptedException {
6261
while (counter == 0) {
6362
wait();
6463
}
6564
counter = counter - 1;
6665
}
67-
66+
6867
/**
6968
* Method called by a thread to release the lock.
7069
*/

0 commit comments

Comments
 (0)