add java 11 support (o) (#1222)

This commit is contained in:
Zhang WH 2020-04-26 20:06:09 +08:00 committed by GitHub
parent 751b3b9452
commit 845da1fa16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 57 additions and 57 deletions

View File

@ -60,7 +60,7 @@ public class King implements Royalty {
* @param queen Queen which should be flirted.
*/
public void flirt(Queen queen) {
boolean flirtStatus = queen.getFlirted(this);
var flirtStatus = queen.getFlirted(this);
if (!flirtStatus) {
this.makeUnhappy();
} else {

View File

@ -43,7 +43,7 @@ public final class RoyaltyObjectMother {
* @return A drunk {@link com.iluwatar.objectmother.King}.
*/
public static King createDrunkKing() {
King king = new King();
var king = new King();
king.makeDrunk();
return king;
}
@ -54,7 +54,7 @@ public final class RoyaltyObjectMother {
* @return A happy {@link com.iluwatar.objectmother.King}.
*/
public static King createHappyKing() {
King king = new King();
var king = new King();
king.makeHappy();
return king;
}
@ -65,7 +65,7 @@ public final class RoyaltyObjectMother {
* @return A drunk and happy {@link com.iluwatar.objectmother.King}.
*/
public static King createHappyDrunkKing() {
King king = new King();
var king = new King();
king.makeHappy();
king.makeDrunk();
return king;
@ -77,7 +77,7 @@ public final class RoyaltyObjectMother {
* @return A flirty {@link com.iluwatar.objectmother.Queen}.
*/
public static Queen createFlirtyQueen() {
Queen queen = new Queen();
var queen = new Queen();
queen.setFlirtiness(true);
return queen;
}

View File

@ -40,50 +40,50 @@ public class RoyaltyObjectMotherTest {
@Test
public void unsuccessfulKingFlirt() {
King soberUnhappyKing = RoyaltyObjectMother.createSoberUnhappyKing();
Queen flirtyQueen = RoyaltyObjectMother.createFlirtyQueen();
var soberUnhappyKing = RoyaltyObjectMother.createSoberUnhappyKing();
var flirtyQueen = RoyaltyObjectMother.createFlirtyQueen();
soberUnhappyKing.flirt(flirtyQueen);
assertFalse(soberUnhappyKing.isHappy());
}
@Test
public void queenIsBlockingFlirtCauseDrunkKing() {
King drunkUnhappyKing = RoyaltyObjectMother.createDrunkKing();
Queen notFlirtyQueen = RoyaltyObjectMother.createNotFlirtyQueen();
var drunkUnhappyKing = RoyaltyObjectMother.createDrunkKing();
var notFlirtyQueen = RoyaltyObjectMother.createNotFlirtyQueen();
drunkUnhappyKing.flirt(notFlirtyQueen);
assertFalse(drunkUnhappyKing.isHappy());
}
@Test
public void queenIsBlockingFlirt() {
King soberHappyKing = RoyaltyObjectMother.createHappyKing();
Queen notFlirtyQueen = RoyaltyObjectMother.createNotFlirtyQueen();
var soberHappyKing = RoyaltyObjectMother.createHappyKing();
var notFlirtyQueen = RoyaltyObjectMother.createNotFlirtyQueen();
soberHappyKing.flirt(notFlirtyQueen);
assertFalse(soberHappyKing.isHappy());
}
@Test
public void successfullKingFlirt() {
King soberHappyKing = RoyaltyObjectMother.createHappyKing();
Queen flirtyQueen = RoyaltyObjectMother.createFlirtyQueen();
var soberHappyKing = RoyaltyObjectMother.createHappyKing();
var flirtyQueen = RoyaltyObjectMother.createFlirtyQueen();
soberHappyKing.flirt(flirtyQueen);
assertTrue(soberHappyKing.isHappy());
}
@Test
public void testQueenType() {
Royalty flirtyQueen = RoyaltyObjectMother.createFlirtyQueen();
Royalty notFlirtyQueen = RoyaltyObjectMother.createNotFlirtyQueen();
var flirtyQueen = RoyaltyObjectMother.createFlirtyQueen();
var notFlirtyQueen = RoyaltyObjectMother.createNotFlirtyQueen();
assertEquals(flirtyQueen.getClass(), Queen.class);
assertEquals(notFlirtyQueen.getClass(), Queen.class);
}
@Test
public void testKingType() {
Royalty drunkKing = RoyaltyObjectMother.createDrunkKing();
Royalty happyDrunkKing = RoyaltyObjectMother.createHappyDrunkKing();
Royalty happyKing = RoyaltyObjectMother.createHappyKing();
Royalty soberUnhappyKing = RoyaltyObjectMother.createSoberUnhappyKing();
var drunkKing = RoyaltyObjectMother.createDrunkKing();
var happyDrunkKing = RoyaltyObjectMother.createHappyDrunkKing();
var happyKing = RoyaltyObjectMother.createHappyKing();
var soberUnhappyKing = RoyaltyObjectMother.createSoberUnhappyKing();
assertEquals(drunkKing.getClass(), King.class);
assertEquals(happyDrunkKing.getClass(), King.class);
assertEquals(happyKing.getClass(), King.class);

View File

@ -54,14 +54,14 @@ public class App {
* @param args command line args
*/
public static void main(String[] args) {
OliphauntPool pool = new OliphauntPool();
var pool = new OliphauntPool();
LOGGER.info(pool.toString());
Oliphaunt oliphaunt1 = pool.checkOut();
var oliphaunt1 = pool.checkOut();
LOGGER.info("Checked out {}", oliphaunt1);
LOGGER.info(pool.toString());
Oliphaunt oliphaunt2 = pool.checkOut();
var oliphaunt2 = pool.checkOut();
LOGGER.info("Checked out {}", oliphaunt2);
Oliphaunt oliphaunt3 = pool.checkOut();
var oliphaunt3 = pool.checkOut();
LOGGER.info("Checked out {}", oliphaunt3);
LOGGER.info(pool.toString());
LOGGER.info("Checking in {}", oliphaunt1);
@ -69,9 +69,9 @@ public class App {
LOGGER.info("Checking in {}", oliphaunt2);
pool.checkIn(oliphaunt2);
LOGGER.info(pool.toString());
Oliphaunt oliphaunt4 = pool.checkOut();
var oliphaunt4 = pool.checkOut();
LOGGER.info("Checked out {}", oliphaunt4);
Oliphaunt oliphaunt5 = pool.checkOut();
var oliphaunt5 = pool.checkOut();
LOGGER.info("Checked out {}", oliphaunt5);
LOGGER.info(pool.toString());
}

View File

@ -45,7 +45,7 @@ public abstract class ObjectPool<T> {
if (available.isEmpty()) {
available.add(create());
}
T instance = available.iterator().next();
var instance = available.iterator().next();
available.remove(instance);
inUse.add(instance);
return instance;

View File

@ -44,17 +44,17 @@ public class OliphauntPoolTest {
@Test
public void testSubsequentCheckinCheckout() {
assertTimeout(ofMillis(5000), () -> {
final OliphauntPool pool = new OliphauntPool();
final var pool = new OliphauntPool();
assertEquals("Pool available=0 inUse=0", pool.toString());
final Oliphaunt expectedOliphaunt = pool.checkOut();
final var expectedOliphaunt = pool.checkOut();
assertEquals("Pool available=0 inUse=1", pool.toString());
pool.checkIn(expectedOliphaunt);
assertEquals("Pool available=1 inUse=0", pool.toString());
for (int i = 0; i < 100; i++) {
final Oliphaunt oliphaunt = pool.checkOut();
final var oliphaunt = pool.checkOut();
assertEquals("Pool available=0 inUse=1", pool.toString());
assertSame(expectedOliphaunt, oliphaunt);
assertEquals(expectedOliphaunt.getId(), oliphaunt.getId());
@ -73,13 +73,13 @@ public class OliphauntPoolTest {
@Test
public void testConcurrentCheckinCheckout() {
assertTimeout(ofMillis(5000), () -> {
final OliphauntPool pool = new OliphauntPool();
final var pool = new OliphauntPool();
assertEquals(pool.toString(), "Pool available=0 inUse=0");
final Oliphaunt firstOliphaunt = pool.checkOut();
final var firstOliphaunt = pool.checkOut();
assertEquals(pool.toString(), "Pool available=0 inUse=1");
final Oliphaunt secondOliphaunt = pool.checkOut();
final var secondOliphaunt = pool.checkOut();
assertEquals(pool.toString(), "Pool available=0 inUse=2");
assertNotSame(firstOliphaunt, secondOliphaunt);
@ -89,7 +89,7 @@ public class OliphauntPoolTest {
pool.checkIn(secondOliphaunt);
assertEquals(pool.toString(), "Pool available=1 inUse=1");
final Oliphaunt oliphaunt3 = pool.checkOut();
final var oliphaunt3 = pool.checkOut();
assertEquals(pool.toString(), "Pool available=0 inUse=2");
assertSame(secondOliphaunt, oliphaunt3);
@ -97,7 +97,7 @@ public class OliphauntPoolTest {
pool.checkIn(firstOliphaunt);
assertEquals(pool.toString(), "Pool available=1 inUse=1");
final Oliphaunt oliphaunt4 = pool.checkOut();
final var oliphaunt4 = pool.checkOut();
assertEquals(pool.toString(), "Pool available=0 inUse=2");
assertSame(firstOliphaunt, oliphaunt4);
@ -110,7 +110,7 @@ public class OliphauntPoolTest {
// The order of the returned instances is not determined, so just put them in a list
// and verify if both expected instances are in there.
final List<Oliphaunt> oliphaunts = List.of(pool.checkOut(), pool.checkOut());
final var oliphaunts = List.of(pool.checkOut(), pool.checkOut());
assertEquals(pool.toString(), "Pool available=0 inUse=2");
assertTrue(oliphaunts.contains(firstOliphaunt));
assertTrue(oliphaunts.contains(secondOliphaunt));

View File

@ -51,7 +51,7 @@ public class App {
*/
public static void main(String[] args) {
Weather weather = new Weather();
var weather = new Weather();
weather.addObserver(new Orcs());
weather.addObserver(new Hobbits());
@ -62,7 +62,7 @@ public class App {
// Generic observer inspired by Java Generics and Collection by Naftalin & Wadler
LOGGER.info("--Running generic version--");
GWeather genericWeather = new GWeather();
var genericWeather = new GWeather();
genericWeather.addObserver(new GOrcs());
genericWeather.addObserver(new GHobbits());

View File

@ -56,14 +56,14 @@ public class Weather {
* Makes time pass for weather.
*/
public void timePasses() {
WeatherType[] enumValues = WeatherType.values();
var enumValues = WeatherType.values();
currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
LOGGER.info("The weather changed to {}.", currentWeather);
notifyObservers();
}
private void notifyObservers() {
for (WeatherObserver obs : observers) {
for (var obs : observers) {
obs.update(currentWeather);
}
}

View File

@ -44,7 +44,7 @@ public class GWeather extends Observable<GWeather, Race, WeatherType> {
* Makes time pass for weather.
*/
public void timePasses() {
WeatherType[] enumValues = WeatherType.values();
var enumValues = WeatherType.values();
currentWeather = enumValues[(currentWeather.ordinal() + 1) % enumValues.length];
LOGGER.info("The weather changed to {}.", currentWeather);
notifyObservers(currentWeather);

View File

@ -54,7 +54,7 @@ public abstract class Observable<S extends Observable<S, O, A>, O extends Observ
*/
@SuppressWarnings("unchecked")
public void notifyObservers(A argument) {
for (O observer : observers) {
for (var observer : observers) {
observer.update((S) this, argument);
}
}

View File

@ -80,7 +80,7 @@ public abstract class WeatherObserverTest<O extends WeatherObserver> {
@ParameterizedTest
@MethodSource("dataProvider")
public void testObserver(WeatherType weather, String response) {
final O observer = this.factory.get();
final var observer = this.factory.get();
assertEquals(0, appender.getLogSize());
observer.update(weather);

View File

@ -61,9 +61,9 @@ public class WeatherTest {
*/
@Test
public void testAddRemoveObserver() {
final WeatherObserver observer = mock(WeatherObserver.class);
final var observer = mock(WeatherObserver.class);
final Weather weather = new Weather();
final var weather = new Weather();
weather.addObserver(observer);
verifyZeroInteractions(observer);
@ -84,13 +84,13 @@ public class WeatherTest {
*/
@Test
public void testTimePasses() {
final WeatherObserver observer = mock(WeatherObserver.class);
final Weather weather = new Weather();
final var observer = mock(WeatherObserver.class);
final var weather = new Weather();
weather.addObserver(observer);
final InOrder inOrder = inOrder(observer);
final WeatherType[] weatherTypes = WeatherType.values();
for (int i = 1; i < 20; i++) {
final var inOrder = inOrder(observer);
final var weatherTypes = WeatherType.values();
for (var i = 1; i < 20; i++) {
weather.timePasses();
inOrder.verify(observer).update(weatherTypes[i % weatherTypes.length]);
}

View File

@ -59,9 +59,9 @@ public class GWeatherTest {
*/
@Test
public void testAddRemoveObserver() {
final Race observer = mock(Race.class);
final var observer = mock(Race.class);
final GWeather weather = new GWeather();
final var weather = new GWeather();
weather.addObserver(observer);
verifyZeroInteractions(observer);
@ -82,13 +82,13 @@ public class GWeatherTest {
*/
@Test
public void testTimePasses() {
final Race observer = mock(Race.class);
final GWeather weather = new GWeather();
final var observer = mock(Race.class);
final var weather = new GWeather();
weather.addObserver(observer);
final InOrder inOrder = inOrder(observer);
final WeatherType[] weatherTypes = WeatherType.values();
for (int i = 1; i < 20; i++) {
final var inOrder = inOrder(observer);
final var weatherTypes = WeatherType.values();
for (var i = 1; i < 20; i++) {
weather.timePasses();
inOrder.verify(observer).update(weather, weatherTypes[i % weatherTypes.length]);
}

View File

@ -79,7 +79,7 @@ public abstract class ObserverTest<O extends Observer<?, ?, WeatherType>> {
@ParameterizedTest
@MethodSource("dataProvider")
public void testObserver(WeatherType weather, String response) {
final O observer = this.factory.get();
final var observer = this.factory.get();
assertEquals(0, appender.getLogSize());
observer.update(null, weather);