Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java 11 migrate all remaining s #1120

Merged
merged 16 commits into from
Jan 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Moves semaphore to Java 11
  • Loading branch information
anuragagarwal561994 committed Dec 25, 2019
commit ae5606a10dadd23db6c92033b5fa86d4ac69ed65
2 changes: 1 addition & 1 deletion semaphore/src/main/java/com/iluwatar/semaphore/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class App {
* main method.
*/
public static void main(String[] args) {
FruitShop shop = new FruitShop();
var shop = new FruitShop();
new Customer("Peter", shop).start();
new Customer("Paul", shop).start();
new Customer("Mary", shop).start();
Expand Down
15 changes: 8 additions & 7 deletions semaphore/src/main/java/com/iluwatar/semaphore/Customer.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ public Customer(String name, FruitShop fruitShop) {
public void run() {

while (fruitShop.countFruit() > 0) {
FruitBowl bowl = fruitShop.takeBowl();
Fruit fruit;

if (bowl != null && (fruit = bowl.take()) != null) {
LOGGER.info("{} took an {}", name, fruit);
fruitBowl.put(fruit);
fruitShop.returnBowl(bowl);
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);
}
}
}

Expand Down
8 changes: 4 additions & 4 deletions semaphore/src/main/java/com/iluwatar/semaphore/FruitBowl.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ public Fruit take() {
* toString method.
*/
public String toString() {
int apples = 0;
int oranges = 0;
int lemons = 0;
var apples = 0;
var oranges = 0;
var lemons = 0;

for (Fruit f : fruit) {
for (var f : fruit) {
switch (f.getType()) {
case APPLE:
apples++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class FruitShop {
* FruitShop constructor.
*/
public FruitShop() {
for (int i = 0; i < 100; i++) {
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));
Expand Down
5 changes: 1 addition & 4 deletions semaphore/src/test/java/com/iluwatar/semaphore/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,12 @@

import org.junit.jupiter.api.Test;

import java.io.IOException;

/**
* Application Test Entrypoint
*/
public class AppTest {
@Test
public void test() {
String[] args = {};
App.main(args);
App.main(new String[]{});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@

package com.iluwatar.semaphore;

import org.junit.jupiter.api.Test;

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() {
FruitBowl fbowl = new FruitBowl();
var fbowl = new FruitBowl();

assertEquals(0, fbowl.countFruit());
for (int i = 1; i <= 10; i++) {

for (var i = 1; i <= 10; i++) {
fbowl.put(new Fruit(Fruit.FruitType.LEMON));
assertEquals(i, fbowl.countFruit());
}

for (int i = 9; i >= 0; i--) {
for (var i = 9; i >= 0; i--) {
assertNotNull(fbowl.take());
assertEquals(i, fbowl.countFruit());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,32 @@

package com.iluwatar.semaphore;

import org.junit.jupiter.api.Test;

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() {
Semaphore sphore = new Semaphore(3);
var sphore = new Semaphore(3);

assertEquals(3, sphore.getAvailableLicenses());

for (int i = 2; i >= 0; i--) {
for (var i = 2; i >= 0; i--) {
try {
sphore.acquire();
assertEquals(i, sphore.getAvailableLicenses());
} catch (InterruptedException e) {
fail(e.toString());
}
}
for (int i = 1; i <= 3; i++) {

for (var i = 1; i <= 3; i++) {
sphore.release();
assertEquals(i, sphore.getAvailableLicenses());
}
Expand Down