Compare commits
2 Commits
all-contri
...
all-contri
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7ad393c324 | ||
|
|
eee543f119 |
@@ -1377,24 +1377,6 @@
|
|||||||
"contributions": [
|
"contributions": [
|
||||||
"translation"
|
"translation"
|
||||||
]
|
]
|
||||||
},
|
|
||||||
{
|
|
||||||
"login": "rohit10000",
|
|
||||||
"name": "Rohit Singh",
|
|
||||||
"avatar_url": "https://avatars.githubusercontent.com/u/20845565?v=4",
|
|
||||||
"profile": "https://github.com/rohit10000",
|
|
||||||
"contributions": [
|
|
||||||
"code"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"login": "byoungju94",
|
|
||||||
"name": "byoungju94",
|
|
||||||
"avatar_url": "https://avatars.githubusercontent.com/u/42516378?v=4",
|
|
||||||
"profile": "https://github.com/byoungju94",
|
|
||||||
"contributions": [
|
|
||||||
"code"
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"contributorsPerLine": 4,
|
"contributorsPerLine": 4,
|
||||||
|
|||||||
8
.gitignore
vendored
8
.gitignore
vendored
@@ -4,8 +4,7 @@ target
|
|||||||
.classpath
|
.classpath
|
||||||
.project
|
.project
|
||||||
*.class
|
*.class
|
||||||
|
# Package Files #
|
||||||
### Package Files ###
|
|
||||||
*.jar
|
*.jar
|
||||||
*.war
|
*.war
|
||||||
*.ear
|
*.ear
|
||||||
@@ -14,8 +13,7 @@ target
|
|||||||
*.swp
|
*.swp
|
||||||
datanucleus.log
|
datanucleus.log
|
||||||
/bin/
|
/bin/
|
||||||
|
/bin/
|
||||||
|
/bin/
|
||||||
*.log
|
*.log
|
||||||
event-sourcing/Journal.json
|
event-sourcing/Journal.json
|
||||||
|
|
||||||
### Checkstyle ###
|
|
||||||
.checkstyle
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@
|
|||||||
[](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
|
[](https://sonarcloud.io/dashboard?id=iluwatar_java-design-patterns)
|
||||||
[](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
[](https://gitter.im/iluwatar/java-design-patterns?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
||||||
[](#contributors-)
|
[](#contributors-)
|
||||||
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
||||||
|
|
||||||
<br/>
|
<br/>
|
||||||
@@ -301,10 +301,6 @@ This project is licensed under the terms of the MIT license.
|
|||||||
<td align="center"><a href="http://libkhadir.fr"><img src="https://avatars1.githubusercontent.com/u/45130488?v=4?s=100" width="100px;" alt=""/><br /><sub><b>KHADIR Tayeb</b></sub></a><br /><a href="#translation-tkhadir" title="Translation">🌍</a></td>
|
<td align="center"><a href="http://libkhadir.fr"><img src="https://avatars1.githubusercontent.com/u/45130488?v=4?s=100" width="100px;" alt=""/><br /><sub><b>KHADIR Tayeb</b></sub></a><br /><a href="#translation-tkhadir" title="Translation">🌍</a></td>
|
||||||
<td align="center"><a href="https://github.com/ignite1771"><img src="https://avatars2.githubusercontent.com/u/59446563?v=4?s=100" width="100px;" alt=""/><br /><sub><b>ignite1771</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=ignite1771" title="Code">💻</a></td>
|
<td align="center"><a href="https://github.com/ignite1771"><img src="https://avatars2.githubusercontent.com/u/59446563?v=4?s=100" width="100px;" alt=""/><br /><sub><b>ignite1771</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=ignite1771" title="Code">💻</a></td>
|
||||||
<td align="center"><a href="https://github.com/demirhalil"><img src="https://avatars1.githubusercontent.com/u/22895118?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Halil Demir</b></sub></a><br /><a href="#translation-demirhalil" title="Translation">🌍</a></td>
|
<td align="center"><a href="https://github.com/demirhalil"><img src="https://avatars1.githubusercontent.com/u/22895118?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Halil Demir</b></sub></a><br /><a href="#translation-demirhalil" title="Translation">🌍</a></td>
|
||||||
<td align="center"><a href="https://github.com/rohit10000"><img src="https://avatars.githubusercontent.com/u/20845565?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Rohit Singh</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=rohit10000" title="Code">💻</a></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td align="center"><a href="https://github.com/byoungju94"><img src="https://avatars.githubusercontent.com/u/42516378?v=4?s=100" width="100px;" alt=""/><br /><sub><b>byoungju94</b></sub></a><br /><a href="https://github.com/iluwatar/java-design-patterns/commits?author=byoungju94" title="Code">💻</a></td>
|
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ public class DbCustomerDao implements CustomerDao {
|
|||||||
public Stream<Customer> getAll() throws Exception {
|
public Stream<Customer> getAll() throws Exception {
|
||||||
try {
|
try {
|
||||||
var connection = getConnection();
|
var connection = getConnection();
|
||||||
var statement = connection.prepareStatement("SELECT * FROM CUSTOMERS"); // NOSONAR
|
var statement = connection.prepareStatement("SELECT * FROM CUSTOMERS");
|
||||||
var resultSet = statement.executeQuery(); // NOSONAR
|
var resultSet = statement.executeQuery(); // NOSONAR
|
||||||
return StreamSupport.stream(new Spliterators.AbstractSpliterator<Customer>(Long.MAX_VALUE,
|
return StreamSupport.stream(new Spliterators.AbstractSpliterator<Customer>(Long.MAX_VALUE,
|
||||||
Spliterator.ORDERED) {
|
Spliterator.ORDERED) {
|
||||||
|
|||||||
54
data-transfer-object-enum-impl/README.md
Normal file
54
data-transfer-object-enum-impl/README.md
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
---
|
||||||
|
layout: pattern
|
||||||
|
title: Data Transfer Object
|
||||||
|
folder: data-transfer-object
|
||||||
|
permalink: /patterns/data-transfer-object/
|
||||||
|
categories: Architectural
|
||||||
|
tags:
|
||||||
|
- Performance
|
||||||
|
---
|
||||||
|
|
||||||
|
## Intent
|
||||||
|
|
||||||
|
Pass data with multiple attributes in one shot from client to server, to avoid multiple calls to
|
||||||
|
remote server.
|
||||||
|
|
||||||
|
## Explanation
|
||||||
|
|
||||||
|
Real world example
|
||||||
|
|
||||||
|
> We need to fetch information about customers from remote database. Instead of querying the
|
||||||
|
> attributes one at a time, we use DTOs to transfer all the relevant attributes in a single shot.
|
||||||
|
|
||||||
|
In plain words
|
||||||
|
|
||||||
|
> Using DTO relevant information can be fetched with a single backend query.
|
||||||
|
|
||||||
|
Wikipedia says
|
||||||
|
|
||||||
|
> In the field of programming a data transfer object (DTO) is an object that carries data between
|
||||||
|
> processes. The motivation for its use is that communication between processes is usually done
|
||||||
|
> resorting to remote interfaces (e.g. web services), where each call is an expensive operation.
|
||||||
|
> Because the majority of the cost of each call is related to the round-trip time between the client
|
||||||
|
> and the server, one way of reducing the number of calls is to use an object (the DTO) that
|
||||||
|
> aggregates the data that would have been transferred by the several calls, but that is served by
|
||||||
|
> one call only.
|
||||||
|
|
||||||
|
## Class diagram
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
## Applicability
|
||||||
|
|
||||||
|
Use the Data Transfer Object pattern when:
|
||||||
|
|
||||||
|
* The client is asking for multiple information. And the information is related.
|
||||||
|
* When you want to boost the performance to get resources.
|
||||||
|
* You want reduced number of remote calls.
|
||||||
|
|
||||||
|
## Credits
|
||||||
|
|
||||||
|
* [Design Pattern - Transfer Object Pattern](https://www.tutorialspoint.com/design_pattern/transfer_object_pattern.htm)
|
||||||
|
* [Data Transfer Object](https://msdn.microsoft.com/en-us/library/ff649585.aspx)
|
||||||
|
* [J2EE Design Patterns](https://www.amazon.com/gp/product/0596004273/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596004273&linkCode=as2&tag=javadesignpat-20&linkId=f27d2644fbe5026ea448791a8ad09c94)
|
||||||
|
* [Patterns of Enterprise Application Architecture](https://www.amazon.com/gp/product/0321127420/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0321127420&linkCode=as2&tag=javadesignpat-20&linkId=014237a67c9d46f384b35e10151956bd)
|
||||||
@@ -0,0 +1,129 @@
|
|||||||
|
@startuml
|
||||||
|
package com.iluwatar.datatransferenum {
|
||||||
|
class App {
|
||||||
|
- LOGGER : Logger {static}
|
||||||
|
+ App()
|
||||||
|
+ main(args : String[]) {static}
|
||||||
|
}
|
||||||
|
class Product {
|
||||||
|
- cost : Double
|
||||||
|
- id : Long
|
||||||
|
- name : String
|
||||||
|
- price : Double
|
||||||
|
- supplier : String
|
||||||
|
+ Product()
|
||||||
|
+ getCost() : Double
|
||||||
|
+ getId() : Long
|
||||||
|
+ getName() : String
|
||||||
|
+ getPrice() : Double
|
||||||
|
+ getSupplier() : String
|
||||||
|
+ setCost(cost : Double) : Product
|
||||||
|
+ setId(id : Long) : Product
|
||||||
|
+ setName(name : String) : Product
|
||||||
|
+ setPrice(price : Double) : Product
|
||||||
|
+ setSupplier(supplier : String) : Product
|
||||||
|
+ toString() : String
|
||||||
|
}
|
||||||
|
enum ProductDTO {
|
||||||
|
+ valueOf(name : String) : ProductDTO {static}
|
||||||
|
+ values() : ProductDTO[] {static}
|
||||||
|
}
|
||||||
|
-interface Cost {
|
||||||
|
+ getCost() : Double {abstract}
|
||||||
|
}
|
||||||
|
-interface Id {
|
||||||
|
+ getId() : Long {abstract}
|
||||||
|
}
|
||||||
|
-interface Name {
|
||||||
|
+ getName() : String {abstract}
|
||||||
|
}
|
||||||
|
-interface Price {
|
||||||
|
+ getPrice() : Double {abstract}
|
||||||
|
}
|
||||||
|
enum Request {
|
||||||
|
+ valueOf(name : String) : Request {static}
|
||||||
|
+ values() : Request[] {static}
|
||||||
|
}
|
||||||
|
class Create {
|
||||||
|
- cost : Double
|
||||||
|
- name : String
|
||||||
|
- price : Double
|
||||||
|
- supplier : String
|
||||||
|
+ Create()
|
||||||
|
+ getCost() : Double
|
||||||
|
+ getName() : String
|
||||||
|
+ getPrice() : Double
|
||||||
|
+ getSupplier() : String
|
||||||
|
+ setCost(cost : Double) : Create
|
||||||
|
+ setName(name : String) : Create
|
||||||
|
+ setPrice(price : Double) : Create
|
||||||
|
+ setSupplier(supplier : String) : Create
|
||||||
|
}
|
||||||
|
enum Response {
|
||||||
|
+ valueOf(name : String) : Response {static}
|
||||||
|
+ values() : Response[] {static}
|
||||||
|
}
|
||||||
|
class Private {
|
||||||
|
- cost : Double
|
||||||
|
- id : Long
|
||||||
|
- name : String
|
||||||
|
- price : Double
|
||||||
|
+ Private()
|
||||||
|
+ getCost() : Double
|
||||||
|
+ getId() : Long
|
||||||
|
+ getName() : String
|
||||||
|
+ getPrice() : Double
|
||||||
|
+ setCost(cost : Double) : Private
|
||||||
|
+ setId(id : Long) : Private
|
||||||
|
+ setName(name : String) : Private
|
||||||
|
+ setPrice(price : Double) : Private
|
||||||
|
+ toString() : String
|
||||||
|
}
|
||||||
|
class Public {
|
||||||
|
- id : Long
|
||||||
|
- name : String
|
||||||
|
- price : Double
|
||||||
|
+ Public()
|
||||||
|
+ getId() : Long
|
||||||
|
+ getName() : String
|
||||||
|
+ getPrice() : Double
|
||||||
|
+ setId(id : Long) : Public
|
||||||
|
+ setName(name : String) : Public
|
||||||
|
+ setPrice(price : Double) : Public
|
||||||
|
+ toString() : String
|
||||||
|
}
|
||||||
|
-interface Supplier {
|
||||||
|
+ getSupplier() : String {abstract}
|
||||||
|
}
|
||||||
|
class ProductResource {
|
||||||
|
- products : List<Product>
|
||||||
|
+ ProductResource(products : List<Product>)
|
||||||
|
+ getAllProductsForAdmin() : List<Private>
|
||||||
|
+ getAllProductsForCustomer() : List<Public>
|
||||||
|
+ getProducts() : List<Product>
|
||||||
|
+ save(createProductDTO : Create)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Create ..+ Request
|
||||||
|
Request ..+ ProductDTO
|
||||||
|
Private ..+ Response
|
||||||
|
Supplier ..+ ProductDTO
|
||||||
|
Name ..+ ProductDTO
|
||||||
|
ProductResource --> "-products" Product
|
||||||
|
Public ..+ Response
|
||||||
|
Id ..+ ProductDTO
|
||||||
|
Price ..+ ProductDTO
|
||||||
|
Response ..+ ProductDTO
|
||||||
|
Cost ..+ ProductDTO
|
||||||
|
Create ..|> Name
|
||||||
|
Create ..|> Price
|
||||||
|
Create ..|> Cost
|
||||||
|
Create ..|> Supplier
|
||||||
|
Private ..|> Id
|
||||||
|
Private ..|> Name
|
||||||
|
Private ..|> Price
|
||||||
|
Private ..|> Cost
|
||||||
|
Public ..|> Id
|
||||||
|
Public ..|> Name
|
||||||
|
Public ..|> Price
|
||||||
|
@enduml
|
||||||
BIN
data-transfer-object-enum-impl/etc/dto-enum-uml.png
Normal file
BIN
data-transfer-object-enum-impl/etc/dto-enum-uml.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 120 KiB |
61
data-transfer-object-enum-impl/pom.xml
Normal file
61
data-transfer-object-enum-impl/pom.xml
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
The MIT License
|
||||||
|
Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
-->
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.iluwatar</groupId>
|
||||||
|
<artifactId>java-design-patterns</artifactId>
|
||||||
|
<version>1.24.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<artifactId>data-transfer-object-enum-impl</artifactId>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<configuration>
|
||||||
|
<archive>
|
||||||
|
<manifest>
|
||||||
|
<mainClass>com.iluwatar.datatransferenum.App</mainClass>
|
||||||
|
</manifest>
|
||||||
|
</archive>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
package com.iluwatar.datatransferenum;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Data Transfer Object pattern is a design pattern in which an data transfer object is used to
|
||||||
|
* serve related information together to avoid multiple call for each piece of information.
|
||||||
|
*
|
||||||
|
* <p>In this example, ({@link App}) as as product details consumer i.e. client to
|
||||||
|
* request for product details to server.
|
||||||
|
*
|
||||||
|
* <p>productResource ({@link ProductResource}) act as server to serve product information. And
|
||||||
|
* The productDto ({@link ProductDto} is data transfer object to share product information.
|
||||||
|
*/
|
||||||
|
public class App {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method as act client and request to server for details.
|
||||||
|
*
|
||||||
|
* @param args program argument.
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Product tv =
|
||||||
|
new Product().setId(1L).setName("TV").setSupplier("Sony").setPrice(1000D).setCost(1090D);
|
||||||
|
Product microwave =
|
||||||
|
new Product().setId(2L).setName("microwave").setSupplier("Delonghi").setPrice(1000D)
|
||||||
|
.setCost(1090D);
|
||||||
|
Product refrigerator =
|
||||||
|
new Product().setId(3L).setName("refrigerator").setSupplier("Botsch").setPrice(1000D)
|
||||||
|
.setCost(1090D);
|
||||||
|
Product airConditioner =
|
||||||
|
new Product().setId(4L).setName("airConditioner").setSupplier("LG").setPrice(1000D)
|
||||||
|
.setCost(1090D);
|
||||||
|
List<Product> products =
|
||||||
|
new ArrayList<>(Arrays.asList(tv, microwave, refrigerator, airConditioner));
|
||||||
|
ProductResource productResource = new ProductResource(products);
|
||||||
|
|
||||||
|
LOGGER.info("####### List of products including sensitive data just for admins: \n {}",
|
||||||
|
Arrays.toString(productResource.getAllProductsForAdmin().toArray()));
|
||||||
|
LOGGER.info("####### List of products for customers: \n {}",
|
||||||
|
Arrays.toString(productResource.getAllProductsForCustomer().toArray()));
|
||||||
|
|
||||||
|
LOGGER.info("####### Going to save Sony PS5 ...");
|
||||||
|
ProductDto.Request.Create createProductRequestDto = new ProductDto.Request.Create()
|
||||||
|
.setName("PS5")
|
||||||
|
.setCost(1000D)
|
||||||
|
.setPrice(1220D)
|
||||||
|
.setSupplier("Sony");
|
||||||
|
productResource.save(createProductRequestDto);
|
||||||
|
LOGGER.info("####### List of products after adding PS5: {}",
|
||||||
|
Arrays.toString(productResource.getProducts().toArray()));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,27 +1,4 @@
|
|||||||
/*
|
package com.iluwatar.datatransferenum;
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.datatransfer.product;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link Product} is a entity class for product entity. This class act as entity in the demo.
|
* {@link Product} is a entity class for product entity. This class act as entity in the demo.
|
||||||
@@ -1,27 +1,4 @@
|
|||||||
/*
|
package com.iluwatar.datatransferenum;
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.datatransfer.product;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link ProductDto} is a data transfer object POJO.
|
* {@link ProductDto} is a data transfer object POJO.
|
||||||
@@ -1,27 +1,4 @@
|
|||||||
/*
|
package com.iluwatar.datatransferenum;
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.datatransfer.product;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.iluwatar.datatransferenum;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class AppTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Issue: Add at least one assertion to this test case.
|
||||||
|
* <p>
|
||||||
|
* Solution: Inserted assertion to check whether the execution of the main method in {@link App#main(String[])}
|
||||||
|
* throws an exception.
|
||||||
|
*/
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldExecuteApplicationWithoutException() {
|
||||||
|
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -23,13 +23,7 @@
|
|||||||
|
|
||||||
package com.iluwatar.datatransfer;
|
package com.iluwatar.datatransfer;
|
||||||
|
|
||||||
import com.iluwatar.datatransfer.customer.CustomerDto;
|
|
||||||
import com.iluwatar.datatransfer.customer.CustomerResource;
|
|
||||||
import com.iluwatar.datatransfer.product.Product;
|
|
||||||
import com.iluwatar.datatransfer.product.ProductDto;
|
|
||||||
import com.iluwatar.datatransfer.product.ProductResource;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@@ -38,17 +32,11 @@ import org.slf4j.LoggerFactory;
|
|||||||
* The Data Transfer Object pattern is a design pattern in which an data transfer object is used to
|
* The Data Transfer Object pattern is a design pattern in which an data transfer object is used to
|
||||||
* serve related information together to avoid multiple call for each piece of information.
|
* serve related information together to avoid multiple call for each piece of information.
|
||||||
*
|
*
|
||||||
* <p>In the first example, {@link App} is a customer details consumer i.e. client to
|
* <p>In this example, ({@link App}) as as customer details consumer i.e. client to
|
||||||
* request for customer details to server. {@link CustomerResource} act as server to serve customer
|
* request for customer details to server.
|
||||||
* information. {@link CustomerDto} is data transfer object to share customer information.
|
|
||||||
*
|
|
||||||
* <p>In the second example, {@link App} is a product details consumer i.e. client to
|
|
||||||
* request for product details to server. {@link ProductResource} acts as server to serve
|
|
||||||
* product information. {@link ProductDto} is data transfer object to share product information.
|
|
||||||
*
|
|
||||||
* <p>The pattern implementation is a bit different in each of the examples. The first can be
|
|
||||||
* thought as a traditional example and the second is an enum based implementation.
|
|
||||||
*
|
*
|
||||||
|
* <p>CustomerResource ({@link CustomerResource}) act as server to serve customer information. And
|
||||||
|
* The CustomerDto ({@link CustomerDto} is data transfer object to share customer information.
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
@@ -60,8 +48,6 @@ public class App {
|
|||||||
* @param args program argument.
|
* @param args program argument.
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
// Example 1: Customer DTO
|
|
||||||
var customerOne = new CustomerDto("1", "Kelly", "Brown");
|
var customerOne = new CustomerDto("1", "Kelly", "Brown");
|
||||||
var customerTwo = new CustomerDto("2", "Alfonso", "Bass");
|
var customerTwo = new CustomerDto("2", "Alfonso", "Bass");
|
||||||
var customers = new ArrayList<>(List.of(customerOne, customerTwo));
|
var customers = new ArrayList<>(List.of(customerOne, customerTwo));
|
||||||
@@ -86,53 +72,6 @@ public class App {
|
|||||||
customerResource.save(customerThree);
|
customerResource.save(customerThree);
|
||||||
allCustomers = customerResource.getAllCustomers();
|
allCustomers = customerResource.getAllCustomers();
|
||||||
printCustomerDetails(allCustomers);
|
printCustomerDetails(allCustomers);
|
||||||
|
|
||||||
// Example 2: Product DTO
|
|
||||||
Product tv =
|
|
||||||
new Product().setId(1L).setName("TV").setSupplier("Sony").setPrice(1000D).setCost(1090D);
|
|
||||||
Product microwave =
|
|
||||||
new Product()
|
|
||||||
.setId(2L)
|
|
||||||
.setName("microwave")
|
|
||||||
.setSupplier("Delonghi")
|
|
||||||
.setPrice(1000D)
|
|
||||||
.setCost(1090D);
|
|
||||||
Product refrigerator =
|
|
||||||
new Product()
|
|
||||||
.setId(3L)
|
|
||||||
.setName("refrigerator")
|
|
||||||
.setSupplier("Botsch")
|
|
||||||
.setPrice(1000D)
|
|
||||||
.setCost(1090D);
|
|
||||||
Product airConditioner =
|
|
||||||
new Product()
|
|
||||||
.setId(4L)
|
|
||||||
.setName("airConditioner")
|
|
||||||
.setSupplier("LG")
|
|
||||||
.setPrice(1000D)
|
|
||||||
.setCost(1090D);
|
|
||||||
List<Product> products =
|
|
||||||
new ArrayList<>(Arrays.asList(tv, microwave, refrigerator, airConditioner));
|
|
||||||
ProductResource productResource = new ProductResource(products);
|
|
||||||
|
|
||||||
LOGGER.info(
|
|
||||||
"####### List of products including sensitive data just for admins: \n {}",
|
|
||||||
Arrays.toString(productResource.getAllProductsForAdmin().toArray()));
|
|
||||||
LOGGER.info(
|
|
||||||
"####### List of products for customers: \n {}",
|
|
||||||
Arrays.toString(productResource.getAllProductsForCustomer().toArray()));
|
|
||||||
|
|
||||||
LOGGER.info("####### Going to save Sony PS5 ...");
|
|
||||||
ProductDto.Request.Create createProductRequestDto =
|
|
||||||
new ProductDto.Request.Create()
|
|
||||||
.setName("PS5")
|
|
||||||
.setCost(1000D)
|
|
||||||
.setPrice(1220D)
|
|
||||||
.setSupplier("Sony");
|
|
||||||
productResource.save(createProductRequestDto);
|
|
||||||
LOGGER.info(
|
|
||||||
"####### List of products after adding PS5: {}",
|
|
||||||
Arrays.toString(productResource.getProducts().toArray()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void printCustomerDetails(List<CustomerDto> allCustomers) {
|
private static void printCustomerDetails(List<CustomerDto> allCustomers) {
|
||||||
|
|||||||
@@ -21,7 +21,7 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.datatransfer.customer;
|
package com.iluwatar.datatransfer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link CustomerDto} is a data transfer object POJO. Instead of sending individual information to
|
* {@link CustomerDto} is a data transfer object POJO. Instead of sending individual information to
|
||||||
@@ -21,7 +21,7 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.datatransfer.customer;
|
package com.iluwatar.datatransfer;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -21,16 +21,13 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.datatransfer.customer;
|
package com.iluwatar.datatransfer;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import com.iluwatar.datatransfer.customer.CustomerDto;
|
|
||||||
import com.iluwatar.datatransfer.customer.CustomerResource;
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
31
mutex/README.md
Normal file
31
mutex/README.md
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
---
|
||||||
|
layout: pattern
|
||||||
|
title: Mutex
|
||||||
|
folder: mutex
|
||||||
|
permalink: /patterns/mutex/
|
||||||
|
categories: Concurrency
|
||||||
|
tags:
|
||||||
|
- Decoupling
|
||||||
|
---
|
||||||
|
|
||||||
|
## Also known as
|
||||||
|
|
||||||
|
* Mutual Exclusion Lock
|
||||||
|
* Binary Semaphore
|
||||||
|
|
||||||
|
## Intent
|
||||||
|
Create a lock which only allows a single thread to access a resource at any one instant.
|
||||||
|
|
||||||
|
## Class diagram
|
||||||
|

|
||||||
|
|
||||||
|
## Applicability
|
||||||
|
Use a Mutex when
|
||||||
|
|
||||||
|
* You need to prevent two threads accessing a critical section at the same time
|
||||||
|
* Concurrent access to a resource could lead to a race condition
|
||||||
|
|
||||||
|
## Credits
|
||||||
|
|
||||||
|
* [Lock (computer science)](http://en.wikipedia.org/wiki/Lock_(computer_science))
|
||||||
|
* [Semaphores](http://tutorials.jenkov.com/java-concurrency/semaphores.html)
|
||||||
BIN
mutex/etc/mutex.png
Normal file
BIN
mutex/etc/mutex.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
27
mutex/etc/mutex.urm.puml
Normal file
27
mutex/etc/mutex.urm.puml
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
@startuml
|
||||||
|
package com.iluwatar.mutex {
|
||||||
|
class App {
|
||||||
|
+ App()
|
||||||
|
+ main(args : String[]) {static}
|
||||||
|
}
|
||||||
|
class Jar {
|
||||||
|
- beans : int
|
||||||
|
- lock : Lock
|
||||||
|
+ Jar(beans : int, lock : Lock)
|
||||||
|
+ takeBean() : boolean
|
||||||
|
}
|
||||||
|
interface Lock {
|
||||||
|
+ acquire() {abstract}
|
||||||
|
+ release() {abstract}
|
||||||
|
}
|
||||||
|
class Mutex {
|
||||||
|
- owner : Object
|
||||||
|
+ Mutex()
|
||||||
|
+ acquire()
|
||||||
|
+ getOwner() : Object
|
||||||
|
+ release()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Jar --> "-lock" Lock
|
||||||
|
Mutex ..|> Lock
|
||||||
|
@enduml
|
||||||
61
mutex/pom.xml
Normal file
61
mutex/pom.xml
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
<?xml version="1.0"?>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
The MIT License
|
||||||
|
Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
-->
|
||||||
|
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.iluwatar</groupId>
|
||||||
|
<artifactId>java-design-patterns</artifactId>
|
||||||
|
<version>1.24.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<artifactId>mutex</artifactId>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<configuration>
|
||||||
|
<archive>
|
||||||
|
<manifest>
|
||||||
|
<mainClass>com.iluwatar.mutex.App</mainClass>
|
||||||
|
</manifest>
|
||||||
|
</archive>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
49
mutex/src/main/java/com/iluwatar/mutex/App.java
Normal file
49
mutex/src/main/java/com/iluwatar/mutex/App.java
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.iluwatar.mutex;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Mutex prevents multiple threads from accessing a resource simultaneously.
|
||||||
|
*
|
||||||
|
* <p>In this example we have two thieves who are taking beans from a jar. Only one thief can take
|
||||||
|
* a bean at a time. This is ensured by a Mutex lock which must be acquired in order to access the
|
||||||
|
* jar. Each thief attempts to acquire the lock, take a bean and then release the lock. If the lock
|
||||||
|
* has already been acquired, the thief will be prevented from continuing (blocked) until the lock
|
||||||
|
* has been released. The thieves stop taking beans once there are no beans left to take.
|
||||||
|
*/
|
||||||
|
public class App {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* main method.
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
var mutex = new Mutex();
|
||||||
|
var jar = new Jar(1000, mutex);
|
||||||
|
var peter = new Thief("Peter", jar);
|
||||||
|
var john = new Thief("John", jar);
|
||||||
|
peter.start();
|
||||||
|
john.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
67
mutex/src/main/java/com/iluwatar/mutex/Jar.java
Normal file
67
mutex/src/main/java/com/iluwatar/mutex/Jar.java
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.iluwatar.mutex;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Jar has a resource of beans which can only be accessed by a single Thief (thread) at any one
|
||||||
|
* time. A Mutex lock is used to prevent more than one Thief taking a bean simultaneously.
|
||||||
|
*/
|
||||||
|
public class Jar {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The lock which must be acquired to access the beans resource.
|
||||||
|
*/
|
||||||
|
private final Lock lock;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The resource within the jar.
|
||||||
|
*/
|
||||||
|
private int beans;
|
||||||
|
|
||||||
|
public Jar(int beans, Lock lock) {
|
||||||
|
this.beans = beans;
|
||||||
|
this.lock = lock;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method for a thief to take a bean.
|
||||||
|
*/
|
||||||
|
public boolean takeBean() {
|
||||||
|
var success = false;
|
||||||
|
try {
|
||||||
|
lock.acquire();
|
||||||
|
success = beans > 0;
|
||||||
|
if (success) {
|
||||||
|
beans = beans - 1;
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
lock.release();
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
35
mutex/src/main/java/com/iluwatar/mutex/Lock.java
Normal file
35
mutex/src/main/java/com/iluwatar/mutex/Lock.java
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.iluwatar.mutex;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lock is an interface for a lock which can be acquired and released.
|
||||||
|
*/
|
||||||
|
public interface Lock {
|
||||||
|
|
||||||
|
void acquire() throws InterruptedException;
|
||||||
|
|
||||||
|
void release();
|
||||||
|
|
||||||
|
}
|
||||||
67
mutex/src/main/java/com/iluwatar/mutex/Mutex.java
Normal file
67
mutex/src/main/java/com/iluwatar/mutex/Mutex.java
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.iluwatar.mutex;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Mutex is an implementation of a mutual exclusion lock.
|
||||||
|
*/
|
||||||
|
public class Mutex implements Lock {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The current owner of the lock.
|
||||||
|
*/
|
||||||
|
private Object owner;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the current owner of the Mutex, or null if available.
|
||||||
|
*/
|
||||||
|
public Object getOwner() {
|
||||||
|
return owner;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method called by a thread to acquire the lock. If the lock has already been acquired this will
|
||||||
|
* wait until the lock has been released to re-attempt the acquire.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public synchronized void acquire() throws InterruptedException {
|
||||||
|
while (owner != null) {
|
||||||
|
wait();
|
||||||
|
}
|
||||||
|
|
||||||
|
owner = Thread.currentThread();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method called by a thread to release the lock.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public synchronized void release() {
|
||||||
|
if (Thread.currentThread() == owner) {
|
||||||
|
owner = null;
|
||||||
|
notify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -21,32 +21,47 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package com.iluwatar.strategy;
|
package com.iluwatar.mutex;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public class LambdaStrategy {
|
/**
|
||||||
|
* Thief is a class which continually tries to acquire a jar and take a bean from it. When the jar
|
||||||
|
* is empty the thief stops.
|
||||||
|
*/
|
||||||
|
public class Thief extends Thread {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(LambdaStrategy.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(Thief.class);
|
||||||
|
|
||||||
public enum Strategy implements DragonSlayingStrategy {
|
/**
|
||||||
MeleeStrategy(() -> LOGGER.info(
|
* The name of the thief.
|
||||||
"With your Excalibur you severe the dragon's head!")),
|
*/
|
||||||
ProjectileStrategy(() -> LOGGER.info(
|
private final String name;
|
||||||
"You shoot the dragon with the magical crossbow and it falls dead on the ground!")),
|
|
||||||
SpellStrategy(() -> LOGGER.info(
|
|
||||||
"You cast the spell of disintegration and the dragon vaporizes in a pile of dust!"));
|
|
||||||
|
|
||||||
private final DragonSlayingStrategy dragonSlayingStrategy;
|
/**
|
||||||
|
* The jar.
|
||||||
|
*/
|
||||||
|
private final Jar jar;
|
||||||
|
|
||||||
Strategy(DragonSlayingStrategy dragonSlayingStrategy) {
|
public Thief(String name, Jar jar) {
|
||||||
this.dragonSlayingStrategy = dragonSlayingStrategy;
|
this.name = name;
|
||||||
}
|
this.jar = jar;
|
||||||
|
|
||||||
@Override
|
|
||||||
public void execute() {
|
|
||||||
dragonSlayingStrategy.execute();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* In the run method the thief repeatedly tries to take a bean until none are left.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
var beans = 0;
|
||||||
|
|
||||||
|
while (jar.takeBean()) {
|
||||||
|
beans = beans + 1;
|
||||||
|
LOGGER.info("{} took a bean.", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
LOGGER.info("{} took {} beans.", name, beans);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
39
mutex/src/test/java/com/iluwatar/mutex/AppTest.java
Normal file
39
mutex/src/test/java/com/iluwatar/mutex/AppTest.java
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.iluwatar.mutex;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Application Test Entrypoint
|
||||||
|
*/
|
||||||
|
class AppTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldExecuteApplicationWithoutException() {
|
||||||
|
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||||
|
}
|
||||||
|
}
|
||||||
45
mutex/src/test/java/com/iluwatar/mutex/JarTest.java
Normal file
45
mutex/src/test/java/com/iluwatar/mutex/JarTest.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.iluwatar.mutex;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case for taking beans from a Jar
|
||||||
|
*/
|
||||||
|
public class JarTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testTakeBeans() {
|
||||||
|
var mutex = new Mutex();
|
||||||
|
var jar = new Jar(10, mutex);
|
||||||
|
IntStream.range(0, 10).mapToObj(i -> jar.takeBean()).forEach(Assertions::assertTrue);
|
||||||
|
assertFalse(jar.takeBean());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
51
mutex/src/test/java/com/iluwatar/mutex/MutexTest.java
Normal file
51
mutex/src/test/java/com/iluwatar/mutex/MutexTest.java
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.iluwatar.mutex;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case for acquiring and releasing a Mutex
|
||||||
|
*/
|
||||||
|
public class MutexTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void acquireReleaseTest() {
|
||||||
|
var mutex = new Mutex();
|
||||||
|
assertNull(mutex.getOwner());
|
||||||
|
try {
|
||||||
|
mutex.acquire();
|
||||||
|
assertEquals(mutex.getOwner(), Thread.currentThread());
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
fail(e.toString());
|
||||||
|
}
|
||||||
|
mutex.release();
|
||||||
|
assertNull(mutex.getOwner());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
3
pom.xml
3
pom.xml
@@ -141,6 +141,8 @@
|
|||||||
<module>module</module>
|
<module>module</module>
|
||||||
<module>monad</module>
|
<module>monad</module>
|
||||||
<module>mute-idiom</module>
|
<module>mute-idiom</module>
|
||||||
|
<module>mutex</module>
|
||||||
|
<module>semaphore</module>
|
||||||
<module>hexagonal</module>
|
<module>hexagonal</module>
|
||||||
<module>abstract-document</module>
|
<module>abstract-document</module>
|
||||||
<module>aggregator-microservices</module>
|
<module>aggregator-microservices</module>
|
||||||
@@ -197,6 +199,7 @@
|
|||||||
<module>filterer</module>
|
<module>filterer</module>
|
||||||
<module>factory</module>
|
<module>factory</module>
|
||||||
<module>separated-interface</module>
|
<module>separated-interface</module>
|
||||||
|
<module>data-transfer-object-enum-impl</module>
|
||||||
<module>special-case</module>
|
<module>special-case</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
|
|||||||
@@ -1,28 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!--
|
|
||||||
|
|
||||||
The MIT License
|
|
||||||
Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
THE SOFTWARE.
|
|
||||||
|
|
||||||
-->
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
|||||||
@@ -1,26 +1,3 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.registry;
|
package com.iluwatar.registry;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|||||||
@@ -1,26 +1,3 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.registry;
|
package com.iluwatar.registry;
|
||||||
|
|
||||||
public class Customer {
|
public class Customer {
|
||||||
|
|||||||
@@ -1,26 +1,3 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.registry;
|
package com.iluwatar.registry;
|
||||||
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|||||||
@@ -1,26 +1,3 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.registry;
|
package com.iluwatar.registry;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
|||||||
33
semaphore/README.md
Normal file
33
semaphore/README.md
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
---
|
||||||
|
layout: pattern
|
||||||
|
title: Semaphore
|
||||||
|
folder: semaphore
|
||||||
|
permalink: /patterns/semaphore/
|
||||||
|
categories: Concurrency
|
||||||
|
tags:
|
||||||
|
- Performance
|
||||||
|
---
|
||||||
|
|
||||||
|
## Also known as
|
||||||
|
Counting Semaphore
|
||||||
|
|
||||||
|
## Intent
|
||||||
|
Create a lock which mediates access to a pool of resources.
|
||||||
|
Only a limited number of threads, specified at the creation
|
||||||
|
of the semaphore, can access the resources at any given time.
|
||||||
|
A semaphore which only allows one concurrent access to a resource
|
||||||
|
is called a binary semaphore.
|
||||||
|
|
||||||
|
## Class diagram
|
||||||
|

|
||||||
|
|
||||||
|
## Applicability
|
||||||
|
Use a Semaphore when
|
||||||
|
|
||||||
|
* You have a pool of resources to allocate to different threads
|
||||||
|
* Concurrent access to a resource could lead to a race condition
|
||||||
|
|
||||||
|
## Credits
|
||||||
|
|
||||||
|
* [Semaphore(programming)] (http://en.wikipedia.org/wiki/Semaphore_(programming))
|
||||||
|
* [Semaphores] (http://tutorials.jenkov.com/java-concurrency/semaphores.html)
|
||||||
BIN
semaphore/etc/semaphore.png
Normal file
BIN
semaphore/etc/semaphore.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 29 KiB |
56
semaphore/etc/semaphore.urm.puml
Normal file
56
semaphore/etc/semaphore.urm.puml
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
@startuml
|
||||||
|
package com.iluwatar.semaphore {
|
||||||
|
class App {
|
||||||
|
+ App()
|
||||||
|
+ main(args : String[]) {static}
|
||||||
|
}
|
||||||
|
class Fruit {
|
||||||
|
- type : FruitType
|
||||||
|
+ Fruit(type : FruitType)
|
||||||
|
+ getType() : FruitType
|
||||||
|
+ toString() : String
|
||||||
|
}
|
||||||
|
enum FruitType {
|
||||||
|
+ APPLE {static}
|
||||||
|
+ LEMON {static}
|
||||||
|
+ ORANGE {static}
|
||||||
|
+ valueOf(name : String) : FruitType {static}
|
||||||
|
+ values() : FruitType[] {static}
|
||||||
|
}
|
||||||
|
class FruitBowl {
|
||||||
|
- fruit : List<Fruit>
|
||||||
|
+ FruitBowl()
|
||||||
|
+ countFruit() : int
|
||||||
|
+ put(f : Fruit)
|
||||||
|
+ take() : Fruit
|
||||||
|
+ toString() : String
|
||||||
|
}
|
||||||
|
class FruitShop {
|
||||||
|
- available : boolean[]
|
||||||
|
- bowls : FruitBowl[]
|
||||||
|
- semaphore : Semaphore
|
||||||
|
+ FruitShop()
|
||||||
|
+ countFruit() : int
|
||||||
|
+ returnBowl(bowl : FruitBowl)
|
||||||
|
+ takeBowl() : FruitBowl
|
||||||
|
}
|
||||||
|
interface Lock {
|
||||||
|
+ acquire() {abstract}
|
||||||
|
+ release() {abstract}
|
||||||
|
}
|
||||||
|
class Semaphore {
|
||||||
|
- counter : int
|
||||||
|
- licenses : int
|
||||||
|
+ Semaphore(licenses : int)
|
||||||
|
+ acquire()
|
||||||
|
+ getAvailableLicenses() : int
|
||||||
|
+ getNumLicenses() : int
|
||||||
|
+ release()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
FruitType ..+ Fruit
|
||||||
|
Fruit --> "-type" FruitType
|
||||||
|
FruitShop --> "-semaphore" Semaphore
|
||||||
|
FruitBowl --> "-fruit" Fruit
|
||||||
|
Semaphore ..|> Lock
|
||||||
|
@enduml
|
||||||
61
semaphore/pom.xml
Normal file
61
semaphore/pom.xml
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
<?xml version="1.0"?>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
The MIT License
|
||||||
|
Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
||||||
|
|
||||||
|
-->
|
||||||
|
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<parent>
|
||||||
|
<groupId>com.iluwatar</groupId>
|
||||||
|
<artifactId>java-design-patterns</artifactId>
|
||||||
|
<version>1.24.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<artifactId>semaphore</artifactId>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<configuration>
|
||||||
|
<archive>
|
||||||
|
<manifest>
|
||||||
|
<mainClass>com.iluwatar.semaphore.App</mainClass>
|
||||||
|
</manifest>
|
||||||
|
</archive>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
50
semaphore/src/main/java/com/iluwatar/semaphore/App.java
Normal file
50
semaphore/src/main/java/com/iluwatar/semaphore/App.java
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
public class App {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* main method.
|
||||||
|
*/
|
||||||
|
public static void main(String[] args) {
|
||||||
|
var shop = new FruitShop();
|
||||||
|
new Customer("Peter", shop).start();
|
||||||
|
new Customer("Paul", shop).start();
|
||||||
|
new Customer("Mary", shop).start();
|
||||||
|
new Customer("John", shop).start();
|
||||||
|
new Customer("Ringo", shop).start();
|
||||||
|
new Customer("George", shop).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
82
semaphore/src/main/java/com/iluwatar/semaphore/Customer.java
Normal file
82
semaphore/src/main/java/com/iluwatar/semaphore/Customer.java
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.iluwatar.semaphore;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Customer attempts to repeatedly take Fruit from the FruitShop by taking Fruit from FruitBowl
|
||||||
|
* instances.
|
||||||
|
*/
|
||||||
|
public class Customer extends Thread {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(Customer.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
while (fruitShop.countFruit() > 0) {
|
||||||
|
var bowl = fruitShop.takeBowl();
|
||||||
|
if (bowl != null) {
|
||||||
|
var fruit = bowl.take();
|
||||||
|
if (fruit != null) {
|
||||||
|
LOGGER.info("{} took an {}", name, fruit);
|
||||||
|
fruitBowl.put(fruit);
|
||||||
|
fruitShop.returnBowl(bowl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
LOGGER.info("{} took {}", name, fruitBowl);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
64
semaphore/src/main/java/com/iluwatar/semaphore/Fruit.java
Normal file
64
semaphore/src/main/java/com/iluwatar/semaphore/Fruit.java
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.iluwatar.semaphore;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fruit is a resource stored in a FruitBowl.
|
||||||
|
*/
|
||||||
|
public class Fruit {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enumeration of Fruit Types.
|
||||||
|
*/
|
||||||
|
public enum FruitType {
|
||||||
|
ORANGE, APPLE, LEMON
|
||||||
|
}
|
||||||
|
|
||||||
|
private final FruitType type;
|
||||||
|
|
||||||
|
public Fruit(FruitType type) {
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FruitType getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* toString method.
|
||||||
|
*/
|
||||||
|
public String toString() {
|
||||||
|
switch (type) {
|
||||||
|
case ORANGE:
|
||||||
|
return "Orange";
|
||||||
|
case APPLE:
|
||||||
|
return "Apple";
|
||||||
|
case LEMON:
|
||||||
|
return "Lemon";
|
||||||
|
default:
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.iluwatar.semaphore;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A FruitBowl contains Fruit.
|
||||||
|
*/
|
||||||
|
public class FruitBowl {
|
||||||
|
|
||||||
|
private final List<Fruit> fruit = new ArrayList<>();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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() {
|
||||||
|
if (fruit.isEmpty()) {
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return fruit.remove(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* toString method.
|
||||||
|
*/
|
||||||
|
public String toString() {
|
||||||
|
var apples = 0;
|
||||||
|
var oranges = 0;
|
||||||
|
var lemons = 0;
|
||||||
|
|
||||||
|
for (var f : fruit) {
|
||||||
|
switch (f.getType()) {
|
||||||
|
case APPLE:
|
||||||
|
apples++;
|
||||||
|
break;
|
||||||
|
case ORANGE:
|
||||||
|
oranges++;
|
||||||
|
break;
|
||||||
|
case LEMON:
|
||||||
|
lemons++;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return apples + " Apples, " + oranges + " Oranges, and " + lemons + " Lemons";
|
||||||
|
}
|
||||||
|
}
|
||||||
120
semaphore/src/main/java/com/iluwatar/semaphore/FruitShop.java
Normal file
120
semaphore/src/main/java/com/iluwatar/semaphore/FruitShop.java
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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 final FruitBowl[] bowls = {
|
||||||
|
new FruitBowl(),
|
||||||
|
new FruitBowl(),
|
||||||
|
new FruitBowl()
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Access flags for each of the FruitBowl instances.
|
||||||
|
*/
|
||||||
|
private final boolean[] available = {
|
||||||
|
true,
|
||||||
|
true,
|
||||||
|
true
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Semaphore that controls access to the class resources.
|
||||||
|
*/
|
||||||
|
private final Semaphore semaphore;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* FruitShop constructor.
|
||||||
|
*/
|
||||||
|
public FruitShop() {
|
||||||
|
for (var 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.
|
||||||
|
*/
|
||||||
|
public synchronized FruitBowl takeBowl() {
|
||||||
|
|
||||||
|
FruitBowl bowl = null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
semaphore.acquire();
|
||||||
|
|
||||||
|
if (available[0]) {
|
||||||
|
bowl = bowls[0];
|
||||||
|
available[0] = false;
|
||||||
|
} else if (available[1]) {
|
||||||
|
bowl = bowls[1];
|
||||||
|
available[1] = false;
|
||||||
|
} else if (available[2]) {
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
35
semaphore/src/main/java/com/iluwatar/semaphore/Lock.java
Normal file
35
semaphore/src/main/java/com/iluwatar/semaphore/Lock.java
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.iluwatar.semaphore;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Semaphore is an implementation of a semaphore lock.
|
||||||
|
*/
|
||||||
|
public class Semaphore implements Lock {
|
||||||
|
|
||||||
|
private final int licenses;
|
||||||
|
/**
|
||||||
|
* The number of concurrent resource accesses which are allowed.
|
||||||
|
*/
|
||||||
|
private int counter;
|
||||||
|
|
||||||
|
public Semaphore(int licenses) {
|
||||||
|
this.licenses = licenses;
|
||||||
|
this.counter = licenses;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the number of licenses managed by the Semaphore.
|
||||||
|
*/
|
||||||
|
public int getNumLicenses() {
|
||||||
|
return licenses;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the number of available licenses.
|
||||||
|
*/
|
||||||
|
public int getAvailableLicenses() {
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
public synchronized void acquire() throws InterruptedException {
|
||||||
|
while (counter == 0) {
|
||||||
|
wait();
|
||||||
|
}
|
||||||
|
counter = counter - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method called by a thread to release the lock.
|
||||||
|
*/
|
||||||
|
public synchronized void release() {
|
||||||
|
if (counter < licenses) {
|
||||||
|
counter = counter + 1;
|
||||||
|
notify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
39
semaphore/src/test/java/com/iluwatar/semaphore/AppTest.java
Normal file
39
semaphore/src/test/java/com/iluwatar/semaphore/AppTest.java
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.iluwatar.semaphore;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Application Test Entrypoint
|
||||||
|
*/
|
||||||
|
class AppTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldExecuteWithoutException() {
|
||||||
|
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.iluwatar.semaphore;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test taking from and putting Fruit into a FruitBowl
|
||||||
|
*/
|
||||||
|
public class FruitBowlTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void fruitBowlTest() {
|
||||||
|
var fbowl = new FruitBowl();
|
||||||
|
|
||||||
|
assertEquals(0, fbowl.countFruit());
|
||||||
|
|
||||||
|
for (var i = 1; i <= 10; i++) {
|
||||||
|
fbowl.put(new Fruit(Fruit.FruitType.LEMON));
|
||||||
|
assertEquals(i, fbowl.countFruit());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 9; i >= 0; i--) {
|
||||||
|
assertNotNull(fbowl.take());
|
||||||
|
assertEquals(i, fbowl.countFruit());
|
||||||
|
}
|
||||||
|
|
||||||
|
assertNull(fbowl.take());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* The MIT License
|
||||||
|
* Copyright © 2014-2019 Ilkka Seppälä
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.iluwatar.semaphore;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test case for acquiring and releasing a Semaphore
|
||||||
|
*/
|
||||||
|
public class SemaphoreTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void acquireReleaseTest() {
|
||||||
|
var sphore = new Semaphore(3);
|
||||||
|
|
||||||
|
assertEquals(3, sphore.getAvailableLicenses());
|
||||||
|
|
||||||
|
for (var i = 2; i >= 0; i--) {
|
||||||
|
try {
|
||||||
|
sphore.acquire();
|
||||||
|
assertEquals(i, sphore.getAvailableLicenses());
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
fail(e.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 1; i <= 3; i++) {
|
||||||
|
sphore.release();
|
||||||
|
assertEquals(i, sphore.getAvailableLicenses());
|
||||||
|
}
|
||||||
|
|
||||||
|
sphore.release();
|
||||||
|
assertEquals(3, sphore.getAvailableLicenses());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,28 +1,4 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<!--
|
|
||||||
|
|
||||||
The MIT License
|
|
||||||
Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in
|
|
||||||
all copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
THE SOFTWARE.
|
|
||||||
|
|
||||||
-->
|
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
|||||||
@@ -1,26 +1,3 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.specialcase;
|
package com.iluwatar.specialcase;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|||||||
@@ -1,26 +1,3 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.specialcase;
|
package com.iluwatar.specialcase;
|
||||||
|
|
||||||
public interface ApplicationServices {
|
public interface ApplicationServices {
|
||||||
|
|||||||
@@ -1,26 +1,3 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.specialcase;
|
package com.iluwatar.specialcase;
|
||||||
|
|
||||||
public class ApplicationServicesImpl implements ApplicationServices {
|
public class ApplicationServicesImpl implements ApplicationServices {
|
||||||
|
|||||||
@@ -1,26 +1,3 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.specialcase;
|
package com.iluwatar.specialcase;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -38,13 +15,16 @@ public class Db {
|
|||||||
*
|
*
|
||||||
* @return singleton instance of Db class
|
* @return singleton instance of Db class
|
||||||
*/
|
*/
|
||||||
public static synchronized Db getInstance() {
|
public static Db getInstance() {
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
Db newInstance = new Db();
|
synchronized (Db.class) {
|
||||||
newInstance.userName2User = new HashMap<>();
|
if (instance == null) {
|
||||||
newInstance.user2Account = new HashMap<>();
|
instance = new Db();
|
||||||
newInstance.itemName2Product = new HashMap<>();
|
instance.userName2User = new HashMap<>();
|
||||||
instance = newInstance;
|
instance.user2Account = new HashMap<>();
|
||||||
|
instance.itemName2Product = new HashMap<>();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,3 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.specialcase;
|
package com.iluwatar.specialcase;
|
||||||
|
|
||||||
public interface DomainServices {
|
public interface DomainServices {
|
||||||
|
|||||||
@@ -1,26 +1,3 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.specialcase;
|
package com.iluwatar.specialcase;
|
||||||
|
|
||||||
public class DomainServicesImpl implements DomainServices {
|
public class DomainServicesImpl implements DomainServices {
|
||||||
|
|||||||
@@ -1,26 +1,3 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.specialcase;
|
package com.iluwatar.specialcase;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|||||||
@@ -1,26 +1,3 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.specialcase;
|
package com.iluwatar.specialcase;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|||||||
@@ -1,26 +1,3 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.specialcase;
|
package com.iluwatar.specialcase;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|||||||
@@ -1,26 +1,3 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.specialcase;
|
package com.iluwatar.specialcase;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@@ -38,9 +15,13 @@ public class MaintenanceLock {
|
|||||||
*
|
*
|
||||||
* @return singleton instance of MaintenanceLock
|
* @return singleton instance of MaintenanceLock
|
||||||
*/
|
*/
|
||||||
public static synchronized MaintenanceLock getInstance() {
|
public static MaintenanceLock getInstance() {
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
instance = new MaintenanceLock();
|
synchronized (MaintenanceLock.class) {
|
||||||
|
if (instance == null) {
|
||||||
|
instance = new MaintenanceLock();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
@@ -51,6 +32,6 @@ public class MaintenanceLock {
|
|||||||
|
|
||||||
public void setLock(boolean lock) {
|
public void setLock(boolean lock) {
|
||||||
this.lock = lock;
|
this.lock = lock;
|
||||||
LOGGER.info("Maintenance lock is set to: ", lock);
|
LOGGER.info("Maintenance lock is set to: " + lock);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,3 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.specialcase;
|
package com.iluwatar.specialcase;
|
||||||
|
|
||||||
public class MoneyTransaction {
|
public class MoneyTransaction {
|
||||||
|
|||||||
@@ -1,26 +1,3 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.specialcase;
|
package com.iluwatar.specialcase;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|||||||
@@ -1,26 +1,3 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.specialcase;
|
package com.iluwatar.specialcase;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|||||||
@@ -1,26 +1,3 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.specialcase;
|
package com.iluwatar.specialcase;
|
||||||
|
|
||||||
public interface ReceiptViewModel {
|
public interface ReceiptViewModel {
|
||||||
|
|||||||
@@ -1,26 +1,3 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.specialcase;
|
package com.iluwatar.specialcase;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
|||||||
@@ -1,26 +1,3 @@
|
|||||||
/*
|
|
||||||
* The MIT License
|
|
||||||
* Copyright © 2014-2019 Ilkka Seppälä
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package com.iluwatar.specialcase;
|
package com.iluwatar.specialcase;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|||||||
@@ -122,51 +122,6 @@ Program output:
|
|||||||
You cast the spell of disintegration and the dragon vaporizes in a pile of dust!
|
You cast the spell of disintegration and the dragon vaporizes in a pile of dust!
|
||||||
```
|
```
|
||||||
|
|
||||||
What's more, the new feature Lambda Expressions in Java 8 provides another approach for the implementation:
|
|
||||||
|
|
||||||
```java
|
|
||||||
public class LambdaStrategy {
|
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(LambdaStrategy.class);
|
|
||||||
|
|
||||||
public enum Strategy implements DragonSlayingStrategy {
|
|
||||||
MeleeStrategy(() -> LOGGER.info(
|
|
||||||
"With your Excalibur you severe the dragon's head!")),
|
|
||||||
ProjectileStrategy(() -> LOGGER.info(
|
|
||||||
"You shoot the dragon with the magical crossbow and it falls dead on the ground!")),
|
|
||||||
SpellStrategy(() -> LOGGER.info(
|
|
||||||
"You cast the spell of disintegration and the dragon vaporizes in a pile of dust!"));
|
|
||||||
|
|
||||||
private final DragonSlayingStrategy dragonSlayingStrategy;
|
|
||||||
|
|
||||||
Strategy(DragonSlayingStrategy dragonSlayingStrategy) {
|
|
||||||
this.dragonSlayingStrategy = dragonSlayingStrategy;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void execute() {
|
|
||||||
dragonSlayingStrategy.execute();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
And here's the dragonslayer in action.
|
|
||||||
|
|
||||||
```java
|
|
||||||
LOGGER.info("Green dragon spotted ahead!");
|
|
||||||
dragonSlayer.changeStrategy(LambdaStrategy.Strategy.MeleeStrategy);
|
|
||||||
dragonSlayer.goToBattle();
|
|
||||||
LOGGER.info("Red dragon emerges.");
|
|
||||||
dragonSlayer.changeStrategy(LambdaStrategy.Strategy.ProjectileStrategy);
|
|
||||||
dragonSlayer.goToBattle();
|
|
||||||
LOGGER.info("Black dragon lands before you.");
|
|
||||||
dragonSlayer.changeStrategy(LambdaStrategy.Strategy.SpellStrategy);
|
|
||||||
dragonSlayer.goToBattle();
|
|
||||||
```
|
|
||||||
|
|
||||||
Program output is the same as above one.
|
|
||||||
|
|
||||||
## Class diagram
|
## Class diagram
|
||||||
|
|
||||||

|

|
||||||
|
|||||||
@@ -14,19 +14,6 @@ package com.iluwatar.strategy {
|
|||||||
interface DragonSlayingStrategy {
|
interface DragonSlayingStrategy {
|
||||||
+ execute() {abstract}
|
+ execute() {abstract}
|
||||||
}
|
}
|
||||||
class LambdaStrategy {
|
|
||||||
- LOGGER : Logger {static}
|
|
||||||
+ LambdaStrategy()
|
|
||||||
}
|
|
||||||
enum Strategy {
|
|
||||||
+ MeleeStrategy {static}
|
|
||||||
+ ProjectileStrategy {static}
|
|
||||||
+ SpellStrategy {static}
|
|
||||||
- dragonSlayingStrategy : DragonSlayingStrategy
|
|
||||||
+ execute()
|
|
||||||
+ valueOf(name : String) : Strategy {static}
|
|
||||||
+ values() : Strategy[] {static}
|
|
||||||
}
|
|
||||||
class MeleeStrategy {
|
class MeleeStrategy {
|
||||||
- LOGGER : Logger {static}
|
- LOGGER : Logger {static}
|
||||||
+ MeleeStrategy()
|
+ MeleeStrategy()
|
||||||
@@ -43,10 +30,7 @@ package com.iluwatar.strategy {
|
|||||||
+ execute()
|
+ execute()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Strategy ..+ LambdaStrategy
|
|
||||||
Strategy --> "-dragonSlayingStrategy" DragonSlayingStrategy
|
|
||||||
DragonSlayer --> "-strategy" DragonSlayingStrategy
|
DragonSlayer --> "-strategy" DragonSlayingStrategy
|
||||||
Strategy ..|> DragonSlayingStrategy
|
|
||||||
MeleeStrategy ..|> DragonSlayingStrategy
|
MeleeStrategy ..|> DragonSlayingStrategy
|
||||||
ProjectileStrategy ..|> DragonSlayingStrategy
|
ProjectileStrategy ..|> DragonSlayingStrategy
|
||||||
SpellStrategy ..|> DragonSlayingStrategy
|
SpellStrategy ..|> DragonSlayingStrategy
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 140 KiB After Width: | Height: | Size: 29 KiB |
@@ -60,7 +60,7 @@ public class App {
|
|||||||
dragonSlayer.changeStrategy(new SpellStrategy());
|
dragonSlayer.changeStrategy(new SpellStrategy());
|
||||||
dragonSlayer.goToBattle();
|
dragonSlayer.goToBattle();
|
||||||
|
|
||||||
// Java 8 functional implementation Strategy pattern
|
// Java 8 Strategy pattern
|
||||||
LOGGER.info("Green dragon spotted ahead!");
|
LOGGER.info("Green dragon spotted ahead!");
|
||||||
dragonSlayer = new DragonSlayer(
|
dragonSlayer = new DragonSlayer(
|
||||||
() -> LOGGER.info("With your Excalibur you severe the dragon's head!"));
|
() -> LOGGER.info("With your Excalibur you severe the dragon's head!"));
|
||||||
@@ -73,16 +73,5 @@ public class App {
|
|||||||
dragonSlayer.changeStrategy(() -> LOGGER.info(
|
dragonSlayer.changeStrategy(() -> LOGGER.info(
|
||||||
"You cast the spell of disintegration and the dragon vaporizes in a pile of dust!"));
|
"You cast the spell of disintegration and the dragon vaporizes in a pile of dust!"));
|
||||||
dragonSlayer.goToBattle();
|
dragonSlayer.goToBattle();
|
||||||
|
|
||||||
// Java 8 lambda implementation with enum Strategy pattern
|
|
||||||
LOGGER.info("Green dragon spotted ahead!");
|
|
||||||
dragonSlayer.changeStrategy(LambdaStrategy.Strategy.MeleeStrategy);
|
|
||||||
dragonSlayer.goToBattle();
|
|
||||||
LOGGER.info("Red dragon emerges.");
|
|
||||||
dragonSlayer.changeStrategy(LambdaStrategy.Strategy.ProjectileStrategy);
|
|
||||||
dragonSlayer.goToBattle();
|
|
||||||
LOGGER.info("Black dragon lands before you.");
|
|
||||||
dragonSlayer.changeStrategy(LambdaStrategy.Strategy.SpellStrategy);
|
|
||||||
dragonSlayer.goToBattle();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ public class HotelDaoImpl implements HotelDao {
|
|||||||
public Stream<Room> getAll() throws Exception {
|
public Stream<Room> getAll() throws Exception {
|
||||||
try {
|
try {
|
||||||
var connection = getConnection();
|
var connection = getConnection();
|
||||||
var statement = connection.prepareStatement("SELECT * FROM ROOMS"); // NOSONAR
|
var statement = connection.prepareStatement("SELECT * FROM ROOMS");
|
||||||
var resultSet = statement.executeQuery(); // NOSONAR
|
var resultSet = statement.executeQuery(); // NOSONAR
|
||||||
return StreamSupport.stream(new Spliterators.AbstractSpliterator<Room>(Long.MAX_VALUE,
|
return StreamSupport.stream(new Spliterators.AbstractSpliterator<Room>(Long.MAX_VALUE,
|
||||||
Spliterator.ORDERED) {
|
Spliterator.ORDERED) {
|
||||||
|
|||||||
Reference in New Issue
Block a user