Resolves checkstyle issues for semaphore servant serverless service-layer service-locator (#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
This commit is contained in:
Anurag Agarwal
2019-11-12 01:57:43 +05:30
committed by Ilkka Seppälä
parent 37599eb48f
commit 390795154f
40 changed files with 211 additions and 274 deletions

View File

@ -25,18 +25,17 @@ package com.iluwatar.semaphore;
/**
* A Semaphore mediates access by a group of threads to a pool of resources.
* <p>
* In this example a group of customers are taking fruit from a fruit shop.
* There is a bowl each of apples, oranges and lemons. Only one customer can
* access a bowl simultaneously. A Semaphore is used to indicate how many
* resources are currently available and must be acquired in order for a bowl
* to be given to a customer. Customers continually try to take fruit until
* there is no fruit left in the shop.
*
* <p>In this example a group of customers are taking fruit from a fruit shop. There is a bowl each
* of apples, oranges and lemons. Only one customer can access a bowl simultaneously. A Semaphore is
* used to indicate how many resources are currently available and must be acquired in order for a
* bowl to be given to a customer. Customers continually try to take fruit until there is no fruit
* left in the shop.
*/
public class App {
/**
* main method
* main method.
*/
public static void main(String[] args) {
FruitShop shop = new FruitShop();
@ -47,5 +46,5 @@ public class App {
new Customer("Ringo", shop).start();
new Customer("George", shop).start();
}
}

View File

@ -27,8 +27,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A Customer attempts to repeatedly take Fruit from the FruitShop by
* taking Fruit from FruitBowl instances.
* A Customer attempts to repeatedly take Fruit from the FruitShop by taking Fruit from FruitBowl
* instances.
*/
public class Customer extends Thread {
@ -38,36 +38,35 @@ public class Customer extends Thread {
* Name of the Customer.
*/
private final String name;
/**
* The FruitShop he is using.
*/
private final FruitShop fruitShop;
/**
* Their bowl of Fruit.
*/
private final FruitBowl fruitBowl;
/**
* Customer constructor
* Customer constructor.
*/
public Customer(String name, FruitShop fruitShop) {
this.name = name;
this.fruitShop = fruitShop;
this.fruitBowl = new FruitBowl();
}
/**
* The Customer repeatedly takes Fruit from the FruitShop until no Fruit
* remains.
*/
* The Customer repeatedly takes Fruit from the FruitShop until no Fruit remains.
*/
public void run() {
while (fruitShop.countFruit() > 0) {
FruitBowl bowl = fruitShop.takeBowl();
Fruit fruit;
if (bowl != null && (fruit = bowl.take()) != null) {
LOGGER.info("{} took an {}", name, fruit);
fruitBowl.put(fruit);
@ -76,7 +75,7 @@ public class Customer extends Thread {
}
LOGGER.info("{} took {}", name, fruitBowl);
}
}

View File

@ -29,7 +29,7 @@ package com.iluwatar.semaphore;
public class Fruit {
/**
* Enumeration of Fruit Types
* Enumeration of Fruit Types.
*/
public enum FruitType {
ORANGE, APPLE, LEMON
@ -46,7 +46,7 @@ public class Fruit {
}
/**
* toString method
* toString method.
*/
public String toString() {
switch (type) {

View File

@ -23,19 +23,20 @@
package com.iluwatar.semaphore;
import java.util.List;
import java.util.ArrayList;
import java.util.List;
/**
* A FruitBowl contains Fruit.
* A FruitBowl contains Fruit.
*/
public class FruitBowl {
private List<Fruit> fruit = new ArrayList<>();
/**
*
* @return The amount of Fruit left in the bowl.
* Returns the amount of fruits left in bowl.
*
* @return The amount of Fruit left in the bowl.
*/
public int countFruit() {
return fruit.size();
@ -43,15 +44,16 @@ public class FruitBowl {
/**
* Put an item of Fruit into the bowl.
*
*
* @param f fruit
*/
public void put(Fruit f) {
fruit.add(f);
}
/**
* Take an item of Fruit out of the bowl.
*
* @return The Fruit taken out of the bowl, or null if empty.
*/
public Fruit take() {
@ -61,15 +63,15 @@ public class FruitBowl {
return fruit.remove(0);
}
}
/**
* toString method
*/
* toString method.
*/
public String toString() {
int apples = 0;
int oranges = 0;
int lemons = 0;
for (Fruit f : fruit) {
switch (f.getType()) {
case APPLE:
@ -84,7 +86,7 @@ public class FruitBowl {
default:
}
}
return apples + " Apples, " + oranges + " Oranges, and " + lemons + " Lemons";
}
}

View File

@ -27,32 +27,32 @@ package com.iluwatar.semaphore;
* A FruitShop contains three FruitBowl instances and controls access to them.
*/
public class FruitShop {
/**
* The FruitBowl instances stored in the class.
*/
private FruitBowl[] bowls = {
new FruitBowl(),
new FruitBowl(),
new FruitBowl()
new FruitBowl(),
new FruitBowl(),
new FruitBowl()
};
/**
* Access flags for each of the FruitBowl instances.
*/
private boolean[] available = {
true,
true,
true
true,
true,
true
};
/**
* The Semaphore that controls access to the class resources.
*/
private Semaphore semaphore;
/**
* FruitShop constructor
* FruitShop constructor.
*/
public FruitShop() {
for (int i = 0; i < 100; i++) {
@ -60,30 +60,30 @@ public class FruitShop {
bowls[1].put(new Fruit(Fruit.FruitType.ORANGE));
bowls[2].put(new Fruit(Fruit.FruitType.LEMON));
}
semaphore = new Semaphore(3);
}
/**
*
* Returns the amount of fruits left in shop.
*
* @return The amount of Fruit left in the shop.
*/
public synchronized int countFruit() {
return bowls[0].countFruit() + bowls[1].countFruit() + bowls[2].countFruit();
}
/**
* Method called by Customer to get a FruitBowl from the shop. This method
* will try to acquire the Semaphore before returning the first available
* FruitBowl.
*/
* Method called by Customer to get a FruitBowl from the shop. This method will try to acquire the
* Semaphore before returning the first available FruitBowl.
*/
public synchronized FruitBowl takeBowl() {
FruitBowl bowl = null;
try {
semaphore.acquire();
if (available[0]) {
bowl = bowls[0];
available[0] = false;
@ -94,7 +94,7 @@ public class FruitShop {
bowl = bowls[2];
available[2] = false;
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
@ -102,20 +102,19 @@ public class FruitShop {
}
return bowl;
}
/**
* Method called by a Customer instance to return a FruitBowl to the shop.
* This method releases the Semaphore, making the FruitBowl available to
* another Customer.
*/
* Method called by a Customer instance to return a FruitBowl to the shop. This method releases
* the Semaphore, making the FruitBowl available to another Customer.
*/
public synchronized void returnBowl(FruitBowl bowl) {
if (bowl == bowls[0]) {
available[0] = true;
} else if (bowl == bowls[1]) {
available[1] = true;
} else if (bowl == bowls[2]) {
available [2] = true;
available[2] = true;
}
}
}

View File

@ -27,9 +27,9 @@ package com.iluwatar.semaphore;
* Lock is an interface for a lock which can be acquired and released.
*/
public interface Lock {
void acquire() throws InterruptedException;
void release();
}

View File

@ -33,30 +33,29 @@ public class Semaphore implements Lock {
* The number of concurrent resource accesses which are allowed.
*/
private int counter;
public Semaphore(int licenses) {
this.licenses = licenses;
this.counter = licenses;
this.counter = licenses;
}
/**
* Returns the number of licenses managed by the Semaphore
* Returns the number of licenses managed by the Semaphore.
*/
public int getNumLicenses() {
return licenses;
}
/**
* Returns the number of available licenses
* Returns the number of available licenses.
*/
public int getAvailableLicenses() {
return counter;
return counter;
}
/**
* Method called by a thread to acquire the lock. If there are no resources
* available this will wait until the lock has been released to re-attempt
* the acquire.
* Method called by a thread to acquire the lock. If there are no resources available this will
* wait until the lock has been released to re-attempt the acquire.
*/
public synchronized void acquire() throws InterruptedException {
while (counter == 0) {
@ -64,7 +63,7 @@ public class Semaphore implements Lock {
}
counter = counter - 1;
}
/**
* Method called by a thread to release the lock.
*/