Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
1fa8a60
Sharding Pattern (#1056)
Azureyjt Nov 8, 2019
6d1c0b1
Resolves checkstyle errors for ambassador, async-method-invocation, b…
anuragagarwal561994 Nov 9, 2019
efc17fc
Resolves checkstyle errors for business-delegate, bytecode, caching (…
anuragagarwal561994 Nov 9, 2019
31f27a7
Resolves checkstyle errors for callback, chain, circuit-breaker (#1060)
anuragagarwal561994 Nov 9, 2019
2f49648
Resolves checkstyle errors for collection-pipeline, command, commande…
anuragagarwal561994 Nov 9, 2019
4f9ee01
Resolves checkstyle errors for converter, cqrs (#1063)
anuragagarwal561994 Nov 10, 2019
dda0953
Resolves checkstyle errors for guarded-suspension, half-sync-half-asy…
anuragagarwal561994 Nov 10, 2019
7f06f3b
Resolves checkstyle errors for intercepting-filter, interpreter, iter…
anuragagarwal561994 Nov 10, 2019
eae09fc
Resolves checkstyle errors for api-gateway, lazy-loading, leader-elec…
anuragagarwal561994 Nov 10, 2019
01e489c
Resolves checkstyle errors for dao data-bus data-locality data-mapper…
anuragagarwal561994 Nov 10, 2019
f2c91eb
Resolves checkstyle errors for delegation dependency-injection dirty-…
anuragagarwal561994 Nov 10, 2019
7c888e8
Resolves checkstyle errors for eip-* (#1069)
anuragagarwal561994 Nov 10, 2019
5ae2ce6
Resolves checkstyle errors for event-* (#1070)
anuragagarwal561994 Nov 10, 2019
4dae1fa
Resolves checkstyle errors for execute-around extension-objects (#1071)
anuragagarwal561994 Nov 10, 2019
9c8ad44
Resolves checkstyle errors for patterns starting with letter r (#1072)
anuragagarwal561994 Nov 10, 2019
b92eb52
Resolves checkstyle errors for template-method thread-pool throttling…
anuragagarwal561994 Nov 10, 2019
f0f0143
Resolves checkstyle errors for trampoline twin typeobjectpattern unit…
anuragagarwal561994 Nov 10, 2019
c441831
Java 11 migration: ambassador async-method-invocation balking bridge …
anuragagarwal561994 Nov 11, 2019
329479d
#984 update Ambassador readme
iluwatar Nov 11, 2019
0272d71
#984 update Bridge readme
iluwatar Nov 11, 2019
2628cc0
#984 update Builder readme
iluwatar Nov 11, 2019
c954a43
Resolves checkstyle errors for facade factory-kit spatial-partition s…
anuragagarwal561994 Nov 11, 2019
37599eb
Resolves checkstyle errors for feature-toggle fluentinterface flux fl…
anuragagarwal561994 Nov 11, 2019
3907951
Resolves checkstyle issues for semaphore servant serverless service-l…
anuragagarwal561994 Nov 11, 2019
1e76d91
Resolves checkstyle errors for abstract-document abstract-factory acy…
anuragagarwal561994 Nov 11, 2019
6ef840f
Resolves checkstyle errors for naked-objects null-object object-mothe…
anuragagarwal561994 Nov 12, 2019
33ea733
Java 11 migration: patterns (remaining b-c) (#1081)
anuragagarwal561994 Nov 12, 2019
3c57bf7
#984 #987 update readmes
iluwatar Nov 12, 2019
f04fc3c
Java 11 migration: patterns starting with a (#1084)
anuragagarwal561994 Nov 13, 2019
160b737
Add another real world example for Builder
iluwatar Nov 13, 2019
50467c9
Java 11 migration: patterns (t-v) (#1085)
anuragagarwal561994 Nov 14, 2019
cc571f4
Saga pattern (#1062)
besok Nov 14, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Resolves checkstyle issues for semaphore servant serverless service-l…
…ayer 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
  • Loading branch information
anuragagarwal561994 authored and iluwatar committed Nov 11, 2019
commit 390795154f66fb95de0684e883f3573fe0eb512f
19 changes: 9 additions & 10 deletions semaphore/src/main/java/com/iluwatar/semaphore/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,17 @@

/**
* 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();
Expand All @@ -47,5 +46,5 @@ public static void main(String[] args) {
new Customer("Ringo", shop).start();
new Customer("George", shop).start();
}

}
27 changes: 13 additions & 14 deletions semaphore/src/main/java/com/iluwatar/semaphore/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
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 {

Expand All @@ -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);
Expand All @@ -76,7 +75,7 @@ public void run() {
}

LOGGER.info("{} took {}", name, fruitBowl);

}

}
4 changes: 2 additions & 2 deletions semaphore/src/main/java/com/iluwatar/semaphore/Fruit.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class Fruit {

/**
* Enumeration of Fruit Types
* Enumeration of Fruit Types.
*/
public enum FruitType {
ORANGE, APPLE, LEMON
Expand All @@ -46,7 +46,7 @@ public FruitType getType() {
}

/**
* toString method
* toString method.
*/
public String toString() {
switch (type) {
Expand Down
26 changes: 14 additions & 12 deletions semaphore/src/main/java/com/iluwatar/semaphore/FruitBowl.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,37 @@

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();
}

/**
* 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() {
Expand All @@ -61,15 +63,15 @@ public Fruit take() {
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:
Expand All @@ -84,7 +86,7 @@ public String toString() {
default:
}
}

return apples + " Apples, " + oranges + " Oranges, and " + lemons + " Lemons";
}
}
53 changes: 26 additions & 27 deletions semaphore/src/main/java/com/iluwatar/semaphore/FruitShop.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,63 +27,63 @@
* 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++) {
bowls[0].put(new Fruit(Fruit.FruitType.APPLE));
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;
Expand All @@ -94,28 +94,27 @@ public synchronized FruitBowl takeBowl() {
bowl = bowls[2];
available[2] = false;
}

} catch (InterruptedException e) {
e.printStackTrace();
} finally {
semaphore.release();
}
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;
}
}

}
6 changes: 3 additions & 3 deletions semaphore/src/main/java/com/iluwatar/semaphore/Lock.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
* Lock is an interface for a lock which can be acquired and released.
*/
public interface Lock {

void acquire() throws InterruptedException;

void release();

}
23 changes: 11 additions & 12 deletions semaphore/src/main/java/com/iluwatar/semaphore/Semaphore.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,37 @@ 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) {
wait();
}
counter = counter - 1;
}

/**
* Method called by a thread to release the lock.
*/
Expand Down
Loading