From e9b942e360330e1d7757955d433deebb7d68721e Mon Sep 17 00:00:00 2001 From: YuCheng Hu Date: Wed, 1 May 2024 11:52:35 -0400 Subject: [PATCH] Got Code Clean up --- .../ChangeCallsToCurrentTimeInMillisMethod.aj | 9 -- .../com/baeldung/java_8_features/Address.java | 14 -- .../java_8_features/CustomException.java | 4 - .../java_8_features/OptionalAddress.java | 16 -- .../java_8_features/OptionalUser.java | 16 -- .../com/baeldung/java_8_features/User.java | 40 ----- .../com/baeldung/java_8_features/Vehicle.java | 18 --- .../baeldung/java_8_features/VehicleImpl.java | 9 -- ...lectionStreamsUsingCommonsEmptyIfNull.java | 19 --- ...ionStreamsUsingJava8OptionalContainer.java | 21 --- ...ctionStreamsUsingNullDereferenceCheck.java | 20 --- .../strategy/ChristmasDiscounter.java | 11 -- .../com/baeldung/strategy/Discounter.java | 23 --- .../baeldung/strategy/EasterDiscounter.java | 11 -- .../monad/MonadSamples.java | 2 +- .../spliterator/Article.java | 2 +- .../spliterator/Author.java | 2 +- .../spliterator/CustomSpliterator.java | 2 +- .../DateFormatUnitTest.java | 110 ------------- .../NumbersCurrenciesFormattingUnitTest.java | 76 --------- ...8DefaultStaticIntefaceMethodsUnitTest.java | 27 ---- .../baeldung/java8/Java8ForEachUnitTest.java | 111 ------------- .../java8/Java8MethodReferenceUnitTest.java | 67 -------- .../baeldung/java8/Java8OptionalUnitTest.java | 116 ------------- .../baeldung/java8/comparator/Employee.java | 22 --- .../comparator/Java8ComparatorUnitTest.java | 152 ------------------ ...treamsUsingCommonsEmptyIfNullUnitTest.java | 41 ----- ...msUsingJava8OptionalContainerUnitTest.java | 50 ------ ...eamsUsingNullDereferenceCheckUnitTest.java | 45 ------ .../StrategyDesignPatternUnitTest.java | 75 --------- .../typeinference/TypeInferenceUnitTest.java | 87 ---------- .../util/CurrentDateTimeUnitTest.java | 42 ----- .../monad/MonadSampleUnitTest.java | 3 +- ...treamsUsingCommonsEmptyIfNullUnitTest.java | 3 +- ...eamsUsingNullDereferenceCheckUnitTest.java | 3 +- .../spliterator/ExecutorUnitTest.java | 6 +- .../StrategyDesignPatternUnitTest.java | 7 +- 37 files changed, 20 insertions(+), 1262 deletions(-) delete mode 100644 core-java-modules/core-java-8/src/main/java/com/baeldung/aspect/ChangeCallsToCurrentTimeInMillisMethod.aj delete mode 100644 core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/Address.java delete mode 100644 core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/CustomException.java delete mode 100644 core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/OptionalAddress.java delete mode 100644 core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/OptionalUser.java delete mode 100644 core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/User.java delete mode 100644 core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/Vehicle.java delete mode 100644 core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/VehicleImpl.java delete mode 100644 core-java-modules/core-java-8/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingCommonsEmptyIfNull.java delete mode 100644 core-java-modules/core-java-8/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingJava8OptionalContainer.java delete mode 100644 core-java-modules/core-java-8/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingNullDereferenceCheck.java delete mode 100644 core-java-modules/core-java-8/src/main/java/com/baeldung/strategy/ChristmasDiscounter.java delete mode 100644 core-java-modules/core-java-8/src/main/java/com/baeldung/strategy/Discounter.java delete mode 100644 core-java-modules/core-java-8/src/main/java/com/baeldung/strategy/EasterDiscounter.java rename core-java-modules/core-java-8/src/main/java/com/{baeldung => ossez}/monad/MonadSamples.java (98%) rename core-java-modules/core-java-8/src/main/java/com/{baeldung => ossez}/spliterator/Article.java (95%) rename core-java-modules/core-java-8/src/main/java/com/{baeldung => ossez}/spliterator/Author.java (95%) rename core-java-modules/core-java-8/src/main/java/com/{baeldung => ossez}/spliterator/CustomSpliterator.java (97%) delete mode 100644 core-java-modules/core-java-8/src/test/java/com/baeldung/internationalization/DateFormatUnitTest.java delete mode 100644 core-java-modules/core-java-8/src/test/java/com/baeldung/internationalization/NumbersCurrenciesFormattingUnitTest.java delete mode 100644 core-java-modules/core-java-8/src/test/java/com/baeldung/java8/Java8DefaultStaticIntefaceMethodsUnitTest.java delete mode 100644 core-java-modules/core-java-8/src/test/java/com/baeldung/java8/Java8ForEachUnitTest.java delete mode 100644 core-java-modules/core-java-8/src/test/java/com/baeldung/java8/Java8MethodReferenceUnitTest.java delete mode 100644 core-java-modules/core-java-8/src/test/java/com/baeldung/java8/Java8OptionalUnitTest.java delete mode 100644 core-java-modules/core-java-8/src/test/java/com/baeldung/java8/comparator/Employee.java delete mode 100644 core-java-modules/core-java-8/src/test/java/com/baeldung/java8/comparator/Java8ComparatorUnitTest.java delete mode 100644 core-java-modules/core-java-8/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingCommonsEmptyIfNullUnitTest.java delete mode 100644 core-java-modules/core-java-8/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingJava8OptionalContainerUnitTest.java delete mode 100644 core-java-modules/core-java-8/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingNullDereferenceCheckUnitTest.java delete mode 100644 core-java-modules/core-java-8/src/test/java/com/baeldung/strategy/StrategyDesignPatternUnitTest.java delete mode 100644 core-java-modules/core-java-8/src/test/java/com/baeldung/typeinference/TypeInferenceUnitTest.java delete mode 100644 core-java-modules/core-java-8/src/test/java/com/baeldung/util/CurrentDateTimeUnitTest.java rename core-java-modules/core-java-8/src/test/java/com/{baeldung => ossez}/monad/MonadSampleUnitTest.java (95%) rename core-java-modules/core-java-8/src/test/java/com/{baeldung => ossez}/spliterator/ExecutorUnitTest.java (96%) diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/aspect/ChangeCallsToCurrentTimeInMillisMethod.aj b/core-java-modules/core-java-8/src/main/java/com/baeldung/aspect/ChangeCallsToCurrentTimeInMillisMethod.aj deleted file mode 100644 index b28bebfdaf..0000000000 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/aspect/ChangeCallsToCurrentTimeInMillisMethod.aj +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.aspect; - -public aspect ChangeCallsToCurrentTimeInMillisMethod { - long around(): - call(public static native long java.lang.System.currentTimeMillis()) - && within(user.code.base.pckg.*) { - return 0; - } -} diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/Address.java b/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/Address.java deleted file mode 100644 index 1f89503288..0000000000 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/Address.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.java_8_features; - -public class Address { - - private String street; - - public String getStreet() { - return street; - } - - public void setStreet(String street) { - this.street = street; - } -} diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/CustomException.java b/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/CustomException.java deleted file mode 100644 index ff9be6ab06..0000000000 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/CustomException.java +++ /dev/null @@ -1,4 +0,0 @@ -package com.baeldung.java_8_features; - -public class CustomException extends RuntimeException { -} diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/OptionalAddress.java b/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/OptionalAddress.java deleted file mode 100644 index 8d6c517ac5..0000000000 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/OptionalAddress.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.java_8_features; - -import java.util.Optional; - -public class OptionalAddress { - - private String street; - - public Optional getStreet() { - return Optional.ofNullable(street); - } - - public void setStreet(String street) { - this.street = street; - } -} diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/OptionalUser.java b/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/OptionalUser.java deleted file mode 100644 index ff06cd21d6..0000000000 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/OptionalUser.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.java_8_features; - -import java.util.Optional; - -public class OptionalUser { - - private OptionalAddress address; - - public Optional getAddress() { - return Optional.of(address); - } - - public void setAddress(OptionalAddress address) { - this.address = address; - } -} diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/User.java b/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/User.java deleted file mode 100644 index 3708d276c8..0000000000 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/User.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.baeldung.java_8_features; - -import java.util.Optional; - -public class User { - - private String name; - - private Address address; - - public Address getAddress() { - return address; - } - - public void setAddress(Address address) { - this.address = address; - } - - public User() { - } - - public User(String name) { - this.name = name; - } - - public static boolean isRealUser(User user) { - return true; - } - - public String getOrThrow() { - String value = null; - Optional valueOpt = Optional.ofNullable(value); - String result = valueOpt.orElseThrow(CustomException::new).toUpperCase(); - return result; - } - - public boolean isLegalName(String name) { - return name.length() > 3 && name.length() < 16; - } -} diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/Vehicle.java b/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/Vehicle.java deleted file mode 100644 index 045fc90590..0000000000 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/Vehicle.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.baeldung.java_8_features; - -public interface Vehicle { - - void moveTo(long altitude, long longitude); - - static String producer() { - return "N&F Vehicles"; - } - - default long[] startPosition() { - return new long[] { 23, 15 }; - } - - default String getOverview() { - return "ATV made by " + producer(); - } -} diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/VehicleImpl.java b/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/VehicleImpl.java deleted file mode 100644 index 64bec0246d..0000000000 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/java_8_features/VehicleImpl.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.java_8_features; - -public class VehicleImpl implements Vehicle { - - @Override - public void moveTo(long altitude, long longitude) { - // do nothing - } -} diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingCommonsEmptyIfNull.java b/core-java-modules/core-java-8/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingCommonsEmptyIfNull.java deleted file mode 100644 index 7587cc6834..0000000000 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingCommonsEmptyIfNull.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.nullsafecollectionstreams; - -import java.util.Collection; -import java.util.stream.Stream; -import static org.apache.commons.collections4.CollectionUtils.emptyIfNull; - -public class NullSafeCollectionStreamsUsingCommonsEmptyIfNull { - - /** - * This method shows how to make a null safe stream from a collection through the use of - * emptyIfNull() method from Apache Commons CollectionUtils library - * - * @param collection The collection that is to be converted into a stream - * @return The stream that has been created from the collection or an empty stream if the collection is null - */ - public Stream collectionAsStream(Collection collection) { - return emptyIfNull(collection).stream(); - } -} diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingJava8OptionalContainer.java b/core-java-modules/core-java-8/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingJava8OptionalContainer.java deleted file mode 100644 index ae8e399d53..0000000000 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingJava8OptionalContainer.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.nullsafecollectionstreams; - -import java.util.Collection; -import java.util.Optional; -import java.util.stream.Stream; - -public class NullSafeCollectionStreamsUsingJava8OptionalContainer { - - /** - * This method shows how to make a null safe stream from a collection through the use of - * Java SE 8’s Optional Container - * - * @param collection The collection that is to be converted into a stream - * @return The stream that has been created from the collection or an empty stream if the collection is null - */ - public Stream collectionAsStream(Collection collection) { - return Optional.ofNullable(collection) - .map(Collection::stream) - .orElseGet(Stream::empty); - } -} diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingNullDereferenceCheck.java b/core-java-modules/core-java-8/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingNullDereferenceCheck.java deleted file mode 100644 index 63b6d34f11..0000000000 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingNullDereferenceCheck.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.nullsafecollectionstreams; - -import java.util.Collection; -import java.util.stream.Stream; - -public class NullSafeCollectionStreamsUsingNullDereferenceCheck { - - /** - * This method shows how to make a null safe stream from a collection through the use of a check - * to prevent null dereferences - * - * @param collection The collection that is to be converted into a stream - * @return The stream that has been created from the collection or an empty stream if the collection is null - */ - public Stream collectionAsStream(Collection collection) { - return collection == null ? Stream.empty() : collection.stream(); - } - - -} diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/strategy/ChristmasDiscounter.java b/core-java-modules/core-java-8/src/main/java/com/baeldung/strategy/ChristmasDiscounter.java deleted file mode 100644 index a0c36bb63e..0000000000 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/strategy/ChristmasDiscounter.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.baeldung.strategy; - -import java.math.BigDecimal; - -public class ChristmasDiscounter implements Discounter { - - @Override - public BigDecimal apply(BigDecimal amount) { - return amount.multiply(BigDecimal.valueOf(0.9)); - } -} diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/strategy/Discounter.java b/core-java-modules/core-java-8/src/main/java/com/baeldung/strategy/Discounter.java deleted file mode 100644 index 00bf4855d1..0000000000 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/strategy/Discounter.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.strategy; - -import java.math.BigDecimal; -import java.util.function.UnaryOperator; - -public interface Discounter extends UnaryOperator { - - default Discounter combine(Discounter after) { - return value -> after.apply(this.apply(value)); - } - - static Discounter christmas() { - return (amount) -> amount.multiply(BigDecimal.valueOf(0.9)); - } - - static Discounter newYear() { - return (amount) -> amount.multiply(BigDecimal.valueOf(0.8)); - } - - static Discounter easter() { - return (amount) -> amount.multiply(BigDecimal.valueOf(0.5)); - } -} diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/strategy/EasterDiscounter.java b/core-java-modules/core-java-8/src/main/java/com/baeldung/strategy/EasterDiscounter.java deleted file mode 100644 index 990d10073b..0000000000 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/strategy/EasterDiscounter.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.baeldung.strategy; - -import java.math.BigDecimal; - -public class EasterDiscounter implements Discounter { - - @Override - public BigDecimal apply(BigDecimal amount) { - return amount.multiply(BigDecimal.valueOf(0.5)); - } -} diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/monad/MonadSamples.java b/core-java-modules/core-java-8/src/main/java/com/ossez/monad/MonadSamples.java similarity index 98% rename from core-java-modules/core-java-8/src/main/java/com/baeldung/monad/MonadSamples.java rename to core-java-modules/core-java-8/src/main/java/com/ossez/monad/MonadSamples.java index 87b7fe697e..0cf193028a 100644 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/monad/MonadSamples.java +++ b/core-java-modules/core-java-8/src/main/java/com/ossez/monad/MonadSamples.java @@ -1,4 +1,4 @@ -package com.baeldung.monad; +package com.ossez.monad; import java.util.Optional; import java.util.function.Function; diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/spliterator/Article.java b/core-java-modules/core-java-8/src/main/java/com/ossez/spliterator/Article.java similarity index 95% rename from core-java-modules/core-java-8/src/main/java/com/baeldung/spliterator/Article.java rename to core-java-modules/core-java-8/src/main/java/com/ossez/spliterator/Article.java index 7b9ef00078..eb0d97cc28 100644 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/spliterator/Article.java +++ b/core-java-modules/core-java-8/src/main/java/com/ossez/spliterator/Article.java @@ -1,4 +1,4 @@ -package com.baeldung.spliterator; +package com.ossez.spliterator; import java.util.List; diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/spliterator/Author.java b/core-java-modules/core-java-8/src/main/java/com/ossez/spliterator/Author.java similarity index 95% rename from core-java-modules/core-java-8/src/main/java/com/baeldung/spliterator/Author.java rename to core-java-modules/core-java-8/src/main/java/com/ossez/spliterator/Author.java index ae204657b5..e15ed26bc6 100644 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/spliterator/Author.java +++ b/core-java-modules/core-java-8/src/main/java/com/ossez/spliterator/Author.java @@ -1,4 +1,4 @@ -package com.baeldung.spliterator; +package com.ossez.spliterator; public class Author { private String name; diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/spliterator/CustomSpliterator.java b/core-java-modules/core-java-8/src/main/java/com/ossez/spliterator/CustomSpliterator.java similarity index 97% rename from core-java-modules/core-java-8/src/main/java/com/baeldung/spliterator/CustomSpliterator.java rename to core-java-modules/core-java-8/src/main/java/com/ossez/spliterator/CustomSpliterator.java index 2f806cccda..1fcc0a3eb2 100644 --- a/core-java-modules/core-java-8/src/main/java/com/baeldung/spliterator/CustomSpliterator.java +++ b/core-java-modules/core-java-8/src/main/java/com/ossez/spliterator/CustomSpliterator.java @@ -1,4 +1,4 @@ -package com.baeldung.spliterator; +package com.ossez.spliterator; import java.util.List; import java.util.Spliterator; diff --git a/core-java-modules/core-java-8/src/test/java/com/baeldung/internationalization/DateFormatUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/baeldung/internationalization/DateFormatUnitTest.java deleted file mode 100644 index b8ee07058a..0000000000 --- a/core-java-modules/core-java-8/src/test/java/com/baeldung/internationalization/DateFormatUnitTest.java +++ /dev/null @@ -1,110 +0,0 @@ -package com.baeldung.internationalization; - -import org.junit.Assert; -import org.junit.Test; - -import java.text.DateFormat; -import java.text.DateFormatSymbols; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.Locale; -import java.util.TimeZone; - -public class DateFormatUnitTest { - - @Test - public void givenGregorianCalendar_whenLocaleSpecificDateInstance_givenLanguageSpecificMonths() { - GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20); - Date date = gregorianCalendar.getTime(); - - DateFormat itInstance = DateFormat.getDateInstance(DateFormat.FULL, Locale.ITALY); - DateFormat usInstance = DateFormat.getDateInstance(DateFormat.FULL, Locale.US); - - Assert.assertEquals("giovedì 1 febbraio 2018", itInstance.format(date)); - Assert.assertEquals("Thursday, February 1, 2018", usInstance.format(date)); - } - - @Test - public void givenGregorianCalendar_whenDateInstanceWithDifferentFormats_givenSpecificDateFormatting() { - GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20); - Date date = gregorianCalendar.getTime(); - - DateFormat fullInstance = DateFormat.getDateInstance(DateFormat.FULL, Locale.ITALY); - DateFormat mediumInstance = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ITALY); - - Assert.assertEquals("giovedì 1 febbraio 2018", fullInstance.format(date)); - Assert.assertEquals("1-feb-2018", mediumInstance.format(date)); - } - - @Test - public void givenGregorianCalendar_whenTimeInstanceWithDifferentFormats_givenSpecificTimeFormatting() { - GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20); - gregorianCalendar.setTimeZone(TimeZone.getTimeZone("CET")); - TimeZone.setDefault(TimeZone.getTimeZone("CET")); - Date date = gregorianCalendar.getTime(); - - DateFormat fullInstance = DateFormat.getTimeInstance(DateFormat.FULL, Locale.ITALY); - DateFormat mediumInstance = DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.ITALY); - - Assert.assertEquals("10.15.20 CET", fullInstance.format(date)); - Assert.assertEquals("10.15.20" , mediumInstance.format(date)); - } - - @Test - public void givenGregorianCalendar_whenDateTimeInstanceWithDifferentFormats_givenSpecificDateTimeFormatting() { - GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20); - gregorianCalendar.setTimeZone(TimeZone.getTimeZone("CET")); - TimeZone.setDefault(TimeZone.getTimeZone("CET")); - Date date = gregorianCalendar.getTime(); - - DateFormat ffInstance = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.ITALY); - DateFormat smInstance = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, Locale.ITALY); - - Assert.assertEquals("giovedì 1 febbraio 2018 10.15.20 CET", ffInstance.format(date)); - Assert.assertEquals("01/02/18 10.15.20", smInstance.format(date)); - } - - @Test - public void givenGregorianCalendar_whenLocaleSpecificDateTimeInstance_givenLocaleSpecificFormatting() { - GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20); - gregorianCalendar.setTimeZone(TimeZone.getTimeZone("CET")); - TimeZone.setDefault(TimeZone.getTimeZone("CET")); - Date date = gregorianCalendar.getTime(); - - DateFormat itInstance = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.ITALY); - DateFormat jpInstance = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.JAPAN); - - Assert.assertEquals("giovedì 1 febbraio 2018 10.15.20 CET", itInstance.format(date)); - Assert.assertEquals("2018年2月1日 10時15分20秒 CET", jpInstance.format(date)); - } - - @Test - public void givenGregorianCalendar_whenCustomizedSimpleDateFormat_thenSpecificMonthRepresentations() { - GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20); - Date date = gregorianCalendar.getTime(); - Locale.setDefault(new Locale("pl", "PL")); - - SimpleDateFormat fullMonthDateFormat = new SimpleDateFormat("dd-MMMM-yyyy HH:mm:ss:SSS"); - SimpleDateFormat shortMonthsimpleDateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss:SSS"); - - Assert.assertEquals("01-lutego-2018 10:15:20:000", fullMonthDateFormat.format(date)); - Assert.assertEquals("01-02-2018 10:15:20:000" , shortMonthsimpleDateFormat.format(date)); - } - - @Test - public void givenGregorianCalendar_whenCustomizedDateFormatSymbols_thenChangedDayNames() { - GregorianCalendar gregorianCalendar = new GregorianCalendar(2018, 1, 1, 10, 15, 20); - Date date = gregorianCalendar.getTime(); - Locale.setDefault(new Locale("pl", "PL")); - - DateFormatSymbols dateFormatSymbols = new DateFormatSymbols(); - dateFormatSymbols.setWeekdays(new String[]{"A", "B", "C", "D", "E", "F", "G", "H"}); - SimpleDateFormat standardDateFormat = new SimpleDateFormat("EEEE-MMMM-yyyy HH:mm:ss:SSS"); - SimpleDateFormat newDaysDateFormat = new SimpleDateFormat("EEEE-MMMM-yyyy HH:mm:ss:SSS", dateFormatSymbols); - - Assert.assertEquals("czwartek-lutego-2018 10:15:20:000", standardDateFormat.format(date)); - Assert.assertEquals("F-lutego-2018 10:15:20:000", newDaysDateFormat.format(date)); - } - -} diff --git a/core-java-modules/core-java-8/src/test/java/com/baeldung/internationalization/NumbersCurrenciesFormattingUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/baeldung/internationalization/NumbersCurrenciesFormattingUnitTest.java deleted file mode 100644 index 658ebb7e45..0000000000 --- a/core-java-modules/core-java-8/src/test/java/com/baeldung/internationalization/NumbersCurrenciesFormattingUnitTest.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.baeldung.internationalization; - -import org.junit.Assert; -import org.junit.Test; - -import java.math.BigDecimal; -import java.text.DecimalFormat; -import java.text.DecimalFormatSymbols; -import java.text.NumberFormat; -import java.util.Currency; -import java.util.Locale; - -public class NumbersCurrenciesFormattingUnitTest { - - @Test - public void givenDifferentLocalesAndDoubleNumber_whenNumberInstance_thenDifferentOutput() { - Locale usLocale = Locale.US; - Locale plLocale = new Locale("pl", "PL"); - Locale deLocale = Locale.GERMANY; - double number = 102_300.456d; - - NumberFormat usNumberFormat = NumberFormat.getInstance(usLocale); - NumberFormat plNumberFormat = NumberFormat.getInstance(plLocale); - NumberFormat deNumberFormat = NumberFormat.getInstance(deLocale); - - Assert.assertEquals(usNumberFormat.format(number), "102,300.456"); - Assert.assertEquals(plNumberFormat.format(number), "102 300,456"); - Assert.assertEquals(deNumberFormat.format(number), "102.300,456"); - } - - @Test - public void givenDifferentLocalesAndDoubleAmount_whenCurrencyInstance_thenDifferentOutput() { - Locale usLocale = Locale.US; - Locale plLocale = new Locale("pl", "PL"); - Locale deLocale = Locale.GERMANY; - BigDecimal number = new BigDecimal(102_300.456d); - - NumberFormat usNumberFormat = NumberFormat.getCurrencyInstance(usLocale); - NumberFormat plNumberFormat = NumberFormat.getCurrencyInstance(plLocale); - NumberFormat deNumberFormat = NumberFormat.getCurrencyInstance(deLocale); - - Assert.assertEquals(usNumberFormat.format(number), "$102,300.46"); - Assert.assertEquals(plNumberFormat.format(number), "102 300,46 zł"); - Assert.assertEquals(deNumberFormat.format(number), "102.300,46 €"); - } - - @Test - public void givenLocaleAndNumber_whenSpecificDecimalFormat_thenSpecificOutput() { - Locale.setDefault(Locale.FRANCE); - BigDecimal number = new BigDecimal(102_300.456d); - - DecimalFormat zeroDecimalFormat = new DecimalFormat("000000000.0000"); - DecimalFormat hashDecimalFormat = new DecimalFormat("###,###.#"); - DecimalFormat dollarDecimalFormat = new DecimalFormat("$###,###.##"); - - Assert.assertEquals(zeroDecimalFormat.format(number), "000102300,4560"); - Assert.assertEquals(hashDecimalFormat.format(number), "102 300,5"); - Assert.assertEquals(dollarDecimalFormat.format(number), "$102 300,46"); - } - - @Test - public void givenLocaleAndNumber_whenSpecificDecimalFormatSymbols_thenSpecificOutput() { - Locale.setDefault(Locale.FRANCE); - BigDecimal number = new BigDecimal(102_300.456d); - - DecimalFormatSymbols decimalFormatSymbols = DecimalFormatSymbols.getInstance(); - decimalFormatSymbols.setGroupingSeparator('^'); - decimalFormatSymbols.setDecimalSeparator('@'); - DecimalFormat separatorsDecimalFormat = new DecimalFormat("$###,###.##"); - separatorsDecimalFormat.setGroupingSize(4); - separatorsDecimalFormat.setCurrency(Currency.getInstance(Locale.JAPAN)); - separatorsDecimalFormat.setDecimalFormatSymbols(decimalFormatSymbols); - - Assert.assertEquals(separatorsDecimalFormat.format(number), "$10^2300@46"); - } -} diff --git a/core-java-modules/core-java-8/src/test/java/com/baeldung/java8/Java8DefaultStaticIntefaceMethodsUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/baeldung/java8/Java8DefaultStaticIntefaceMethodsUnitTest.java deleted file mode 100644 index 5b07b3e3ae..0000000000 --- a/core-java-modules/core-java-8/src/test/java/com/baeldung/java8/Java8DefaultStaticIntefaceMethodsUnitTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.baeldung.java8; - -import com.baeldung.java_8_features.Vehicle; -import com.baeldung.java_8_features.VehicleImpl; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class Java8DefaultStaticIntefaceMethodsUnitTest { - - @Test - public void callStaticInterfaceMethdosMethods_whenExpectedResults_thenCorrect() { - Vehicle vehicle = new VehicleImpl(); - String overview = vehicle.getOverview(); - long[] startPosition = vehicle.startPosition(); - - assertEquals(overview, "ATV made by N&F Vehicles"); - assertEquals(startPosition[0], 23); - assertEquals(startPosition[1], 15); - } - - @Test - public void callDefaultInterfaceMethods_whenExpectedResults_thenCorrect() { - String producer = Vehicle.producer(); - assertEquals(producer, "N&F Vehicles"); - } -} diff --git a/core-java-modules/core-java-8/src/test/java/com/baeldung/java8/Java8ForEachUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/baeldung/java8/Java8ForEachUnitTest.java deleted file mode 100644 index f5201f54cf..0000000000 --- a/core-java-modules/core-java-8/src/test/java/com/baeldung/java8/Java8ForEachUnitTest.java +++ /dev/null @@ -1,111 +0,0 @@ -package com.baeldung.java8; - -import java.util.ArrayDeque; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Queue; -import java.util.Set; -import java.util.function.Consumer; - -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class Java8ForEachUnitTest { - - private static final Logger LOG = LoggerFactory.getLogger(Java8ForEachUnitTest.class); - - @Test - public void compareForEachMethods_thenPrintResults() { - - List names = new ArrayList<>(); - names.add("Larry"); - names.add("Steve"); - names.add("James"); - names.add("Conan"); - names.add("Ellen"); - - // Java 5 - for-loop - LOG.debug("--- Enhanced for-loop ---"); - for (String name : names) { - LOG.debug(name); - } - - // Java 8 - forEach - names.forEach(name -> { - System.out.println(name); - }); - - LOG.debug("--- Print Consumer ---"); - Consumer printConsumer = new Consumer() { - public void accept(String name) { - System.out.println(name); - }; - }; - - names.forEach(printConsumer); - - // Anonymous inner class that implements Consumer interface - LOG.debug("--- Anonymous inner class ---"); - names.forEach(new Consumer() { - public void accept(String name) { - LOG.debug(name); - } - }); - - // Java 8 - forEach - Lambda Syntax - LOG.debug("--- forEach method ---"); - names.forEach(name -> LOG.debug(name)); - - // Java 8 - forEach - Print elements using a Method Reference - LOG.debug("--- Method Reference ---"); - names.forEach(LOG::debug); - } - - @Test - public void givenList_thenIterateAndPrintResults() { - List names = Arrays.asList("Larry", "Steve", "James"); - - names.forEach(System.out::println); - } - - @Test - public void givenSet_thenIterateAndPrintResults() { - Set uniqueNames = new HashSet<>(Arrays.asList("Larry", "Steve", "James")); - - uniqueNames.forEach(System.out::println); - } - - @Test - public void givenQueue_thenIterateAndPrintResults() { - Queue namesQueue = new ArrayDeque<>(Arrays.asList("Larry", "Steve", "James")); - - namesQueue.forEach(System.out::println); - } - - @Test - public void givenMap_thenIterateAndPrintResults() { - Map namesMap = new HashMap<>(); - namesMap.put(1, "Larry"); - namesMap.put(2, "Steve"); - namesMap.put(3, "James"); - - namesMap.entrySet() - .forEach(entry -> System.out.println(entry.getKey() + " " + entry.getValue())); - } - - @Test - public void givenMap_whenUsingBiConsumer_thenIterateAndPrintResults2() { - Map namesMap = new HashMap<>(); - namesMap.put(1, "Larry"); - namesMap.put(2, "Steve"); - namesMap.put(3, "James"); - - namesMap.forEach((key, value) -> System.out.println(key + " " + value)); - } - -} diff --git a/core-java-modules/core-java-8/src/test/java/com/baeldung/java8/Java8MethodReferenceUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/baeldung/java8/Java8MethodReferenceUnitTest.java deleted file mode 100644 index 2dc1fe18e6..0000000000 --- a/core-java-modules/core-java-8/src/test/java/com/baeldung/java8/Java8MethodReferenceUnitTest.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.baeldung.java8; - -import com.baeldung.java_8_features.User; -import org.junit.Before; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; - -public class Java8MethodReferenceUnitTest { - - private List list; - - @Before - public void init() { - list = new ArrayList<>(); - list.add("One"); - list.add("OneAndOnly"); - list.add("Derek"); - list.add("Change"); - list.add("factory"); - list.add("justBefore"); - list.add("Italy"); - list.add("Italy"); - list.add("Thursday"); - list.add(""); - list.add(""); - } - - @Test - public void checkStaticMethodReferences_whenWork_thenCorrect() { - - List users = new ArrayList<>(); - users.add(new User()); - users.add(new User()); - boolean isReal = users.stream().anyMatch(u -> User.isRealUser(u)); - boolean isRealRef = users.stream().anyMatch(User::isRealUser); - assertTrue(isReal); - assertTrue(isRealRef); - } - - @Test - public void checkInstanceMethodReferences_whenWork_thenCorrect() { - User user = new User(); - boolean isLegalName = list.stream().anyMatch(user::isLegalName); - assertTrue(isLegalName); - } - - @Test - public void checkParticularTypeReferences_whenWork_thenCorrect() { - long count = list.stream().filter(String::isEmpty).count(); - assertEquals(count, 2); - } - - @Test - public void checkConstructorReferences_whenWork_thenCorrect() { - Stream stream = list.stream().map(User::new); - List userList = stream.collect(Collectors.toList()); - assertEquals(userList.size(), list.size()); - assertTrue(userList.get(0) instanceof User); - } -} diff --git a/core-java-modules/core-java-8/src/test/java/com/baeldung/java8/Java8OptionalUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/baeldung/java8/Java8OptionalUnitTest.java deleted file mode 100644 index c6d5836387..0000000000 --- a/core-java-modules/core-java-8/src/test/java/com/baeldung/java8/Java8OptionalUnitTest.java +++ /dev/null @@ -1,116 +0,0 @@ -package com.baeldung.java8; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; - -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.java_8_features.Address; -import com.baeldung.java_8_features.CustomException; -import com.baeldung.java_8_features.OptionalAddress; -import com.baeldung.java_8_features.OptionalUser; -import com.baeldung.java_8_features.User; - -public class Java8OptionalUnitTest { - - private List list; - - @Before - public void init() { - list = new ArrayList<>(); - list.add("One"); - list.add("OneAndOnly"); - list.add("Derek"); - list.add("Change"); - list.add("factory"); - list.add("justBefore"); - list.add("Italy"); - list.add("Italy"); - list.add("Thursday"); - list.add(""); - list.add(""); - } - - @Test - public void checkOptional_whenAsExpected_thenCorrect() { - Optional optionalEmpty = Optional.empty(); - assertFalse(optionalEmpty.isPresent()); - - String str = "value"; - Optional optional = Optional.of(str); - assertEquals(optional.get(), "value"); - - Optional optionalNullable = Optional.ofNullable(str); - Optional optionalNull = Optional.ofNullable(null); - assertEquals(optionalNullable.get(), "value"); - assertFalse(optionalNull.isPresent()); - - List listOpt = Optional.of(list).orElse(new ArrayList<>()); - List listNull = null; - List listOptNull = Optional.ofNullable(listNull).orElse(new ArrayList<>()); - assertTrue(listOpt == list); - assertTrue(listOptNull.isEmpty()); - - Optional user = Optional.ofNullable(getUser()); - String result = user.map(User::getAddress).map(Address::getStreet).orElse("not specified"); - assertEquals(result, "1st Avenue"); - - Optional optionalUser = Optional.ofNullable(getOptionalUser()); - String resultOpt = optionalUser.flatMap(OptionalUser::getAddress).flatMap(OptionalAddress::getStreet).orElse("not specified"); - assertEquals(resultOpt, "1st Avenue"); - - Optional userNull = Optional.ofNullable(getUserNull()); - String resultNull = userNull.map(User::getAddress).map(Address::getStreet).orElse("not specified"); - assertEquals(resultNull, "not specified"); - - Optional optionalUserNull = Optional.ofNullable(getOptionalUserNull()); - String resultOptNull = optionalUserNull.flatMap(OptionalUser::getAddress).flatMap(OptionalAddress::getStreet).orElse("not specified"); - assertEquals(resultOptNull, "not specified"); - - } - - @Test(expected = CustomException.class) - public void callMethod_whenCustomException_thenCorrect() { - User user = new User(); - String result = user.getOrThrow(); - } - - private User getUser() { - User user = new User(); - Address address = new Address(); - address.setStreet("1st Avenue"); - user.setAddress(address); - return user; - } - - private OptionalUser getOptionalUser() { - OptionalUser user = new OptionalUser(); - OptionalAddress address = new OptionalAddress(); - address.setStreet("1st Avenue"); - user.setAddress(address); - return user; - } - - private OptionalUser getOptionalUserNull() { - OptionalUser user = new OptionalUser(); - OptionalAddress address = new OptionalAddress(); - address.setStreet(null); - user.setAddress(address); - return user; - } - - private User getUserNull() { - User user = new User(); - Address address = new Address(); - address.setStreet(null); - user.setAddress(address); - return user; - } - -} diff --git a/core-java-modules/core-java-8/src/test/java/com/baeldung/java8/comparator/Employee.java b/core-java-modules/core-java-8/src/test/java/com/baeldung/java8/comparator/Employee.java deleted file mode 100644 index bec0c37880..0000000000 --- a/core-java-modules/core-java-8/src/test/java/com/baeldung/java8/comparator/Employee.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.java8.comparator; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.ToString; - -@Data -@AllArgsConstructor -@ToString -@EqualsAndHashCode -public class Employee implements Comparable{ - String name; - int age; - double salary; - long mobile; - - @Override - public int compareTo(Employee argEmployee) { - return name.compareTo(argEmployee.getName()); - } - -} diff --git a/core-java-modules/core-java-8/src/test/java/com/baeldung/java8/comparator/Java8ComparatorUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/baeldung/java8/comparator/Java8ComparatorUnitTest.java deleted file mode 100644 index 2475716554..0000000000 --- a/core-java-modules/core-java-8/src/test/java/com/baeldung/java8/comparator/Java8ComparatorUnitTest.java +++ /dev/null @@ -1,152 +0,0 @@ -package com.baeldung.java8.comparator; - -import java.util.Arrays; -import java.util.Comparator; - -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.assertTrue; - -public class Java8ComparatorUnitTest { - - private Employee[] employees; - private Employee[] employeesArrayWithNulls; - private Employee[] sortedEmployeesByName; - private Employee[] sortedEmployeesByNameDesc; - private Employee[] sortedEmployeesByAge; - private Employee[] sortedEmployeesByMobile; - private Employee[] sortedEmployeesBySalary; - private Employee[] sortedEmployeesArray_WithNullsFirst; - private Employee[] sortedEmployeesArray_WithNullsLast; - private Employee[] sortedEmployeesByNameAge; - private Employee[] someMoreEmployees; - private Employee[] sortedEmployeesByAgeName;; - - @Before - public void initData() { - employees = new Employee[] { new Employee("John", 25, 3000, 9922001), new Employee("Ace", 22, 2000, 5924001), new Employee("Keith", 35, 4000, 3924401) }; - employeesArrayWithNulls = new Employee[] { new Employee("John", 25, 3000, 9922001), null, new Employee("Ace", 22, 2000, 5924001), null, new Employee("Keith", 35, 4000, 3924401) }; - - sortedEmployeesByName = new Employee[] { new Employee("Ace", 22, 2000, 5924001), new Employee("John", 25, 3000, 9922001), new Employee("Keith", 35, 4000, 3924401) }; - sortedEmployeesByNameDesc = new Employee[] { new Employee("Keith", 35, 4000, 3924401), new Employee("John", 25, 3000, 9922001), new Employee("Ace", 22, 2000, 5924001) }; - - sortedEmployeesByAge = new Employee[] { new Employee("Ace", 22, 2000, 5924001), new Employee("John", 25, 3000, 9922001), new Employee("Keith", 35, 4000, 3924401) }; - - sortedEmployeesByMobile = new Employee[] { new Employee("Keith", 35, 4000, 3924401), new Employee("Ace", 22, 2000, 5924001), new Employee("John", 25, 3000, 9922001), }; - - sortedEmployeesBySalary = new Employee[] { new Employee("Ace", 22, 2000, 5924001), new Employee("John", 25, 3000, 9922001), new Employee("Keith", 35, 4000, 3924401), }; - - sortedEmployeesArray_WithNullsFirst = new Employee[] { null, null, new Employee("Ace", 22, 2000, 5924001), new Employee("John", 25, 3000, 9922001), new Employee("Keith", 35, 4000, 3924401) }; - sortedEmployeesArray_WithNullsLast = new Employee[] { new Employee("Ace", 22, 2000, 5924001), new Employee("John", 25, 3000, 9922001), new Employee("Keith", 35, 4000, 3924401), null, null }; - - someMoreEmployees = new Employee[] { new Employee("Jake", 25, 3000, 9922001), new Employee("Jake", 22, 2000, 5924001), new Employee("Ace", 22, 3000, 6423001), new Employee("Keith", 35, 4000, 3924401) }; - - sortedEmployeesByAgeName = new Employee[] { new Employee("Ace", 22, 3000, 6423001), new Employee("Jake", 22, 2000, 5924001), new Employee("Jake", 25, 3000, 9922001), new Employee("Keith", 35, 4000, 3924401) }; - sortedEmployeesByNameAge = new Employee[] { new Employee("Ace", 22, 3000, 6423001), new Employee("Jake", 22, 2000, 5924001), new Employee("Jake", 25, 3000, 9922001), new Employee("Keith", 35, 4000, 3924401) }; - } - - @Test - public void whenComparing_thenSortedByName() { - Comparator employeeNameComparator = Comparator.comparing(Employee::getName); - Arrays.sort(employees, employeeNameComparator); - // System.out.println(Arrays.toString(employees)); - assertTrue(Arrays.equals(employees, sortedEmployeesByName)); - } - - @Test - public void whenComparingWithComparator_thenSortedByNameDesc() { - Comparator employeeNameComparator = Comparator.comparing(Employee::getName, (s1, s2) -> { - return s2.compareTo(s1); - }); - Arrays.sort(employees, employeeNameComparator); - // System.out.println(Arrays.toString(employees)); - assertTrue(Arrays.equals(employees, sortedEmployeesByNameDesc)); - } - - @Test - public void whenReversed_thenSortedByNameDesc() { - Comparator employeeNameComparator = Comparator.comparing(Employee::getName); - Comparator employeeNameComparatorReversed = employeeNameComparator.reversed(); - Arrays.sort(employees, employeeNameComparatorReversed); - // System.out.println(Arrays.toString(employees)); - assertTrue(Arrays.equals(employees, sortedEmployeesByNameDesc)); - } - - @Test - public void whenComparingInt_thenSortedByAge() { - Comparator employeeAgeComparator = Comparator.comparingInt(Employee::getAge); - Arrays.sort(employees, employeeAgeComparator); - // System.out.println(Arrays.toString(employees)); - assertTrue(Arrays.equals(employees, sortedEmployeesByAge)); - } - - @Test - public void whenComparingLong_thenSortedByMobile() { - Comparator employeeMobileComparator = Comparator.comparingLong(Employee::getMobile); - Arrays.sort(employees, employeeMobileComparator); - // System.out.println(Arrays.toString(employees)); - assertTrue(Arrays.equals(employees, sortedEmployeesByMobile)); - } - - @Test - public void whenComparingDouble_thenSortedBySalary() { - Comparator employeeSalaryComparator = Comparator.comparingDouble(Employee::getSalary); - Arrays.sort(employees, employeeSalaryComparator); - // System.out.println(Arrays.toString(employees)); - assertTrue(Arrays.equals(employees, sortedEmployeesBySalary)); - } - - @Test - public void whenNaturalOrder_thenSortedByName() { - Comparator employeeNameComparator = Comparator. naturalOrder(); - Arrays.sort(employees, employeeNameComparator); - // System.out.println(Arrays.toString(employees)); - assertTrue(Arrays.equals(employees, sortedEmployeesByName)); - } - - @Test - public void whenReverseOrder_thenSortedByNameDesc() { - Comparator employeeNameComparator = Comparator. reverseOrder(); - Arrays.sort(employees, employeeNameComparator); - // System.out.println(Arrays.toString(employees)); - assertTrue(Arrays.equals(employees, sortedEmployeesByNameDesc)); - } - - @Test - public void whenNullsFirst_thenSortedByNameWithNullsFirst() { - Comparator employeeNameComparator = Comparator.comparing(Employee::getName); - Comparator employeeNameComparator_nullFirst = Comparator.nullsFirst(employeeNameComparator); - Arrays.sort(employeesArrayWithNulls, employeeNameComparator_nullFirst); - // System.out.println(Arrays.toString(employeesArrayWithNulls)); - assertTrue(Arrays.equals(employeesArrayWithNulls, sortedEmployeesArray_WithNullsFirst)); - } - - @Test - public void whenNullsLast_thenSortedByNameWithNullsLast() { - Comparator employeeNameComparator = Comparator.comparing(Employee::getName); - Comparator employeeNameComparator_nullLast = Comparator.nullsLast(employeeNameComparator); - Arrays.sort(employeesArrayWithNulls, employeeNameComparator_nullLast); - // System.out.println(Arrays.toString(employeesArrayWithNulls)); - assertTrue(Arrays.equals(employeesArrayWithNulls, sortedEmployeesArray_WithNullsLast)); - } - - @Test - public void whenThenComparing_thenSortedByAgeName() { - Comparator employee_Age_Name_Comparator = Comparator.comparing(Employee::getAge).thenComparing(Employee::getName); - - Arrays.sort(someMoreEmployees, employee_Age_Name_Comparator); - // System.out.println(Arrays.toString(someMoreEmployees)); - assertTrue(Arrays.equals(someMoreEmployees, sortedEmployeesByAgeName)); - } - - @Test - public void whenThenComparing_thenSortedByNameAge() { - Comparator employee_Name_Age_Comparator = Comparator.comparing(Employee::getName).thenComparingInt(Employee::getAge); - - Arrays.sort(someMoreEmployees, employee_Name_Age_Comparator); - // System.out.println(Arrays.toString(someMoreEmployees)); - assertTrue(Arrays.equals(someMoreEmployees, sortedEmployeesByNameAge)); - } - -} diff --git a/core-java-modules/core-java-8/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingCommonsEmptyIfNullUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingCommonsEmptyIfNullUnitTest.java deleted file mode 100644 index f68df2c821..0000000000 --- a/core-java-modules/core-java-8/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingCommonsEmptyIfNullUnitTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.baeldung.nullsafecollectionstreams; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Iterator; -import java.util.stream.Stream; -import org.junit.Test; -import static org.junit.Assert.*; - - -public class NullSafeCollectionStreamsUsingCommonsEmptyIfNullUnitTest { - - private final NullSafeCollectionStreamsUsingCommonsEmptyIfNull instance = - new NullSafeCollectionStreamsUsingCommonsEmptyIfNull(); - - @Test - public void whenCollectionIsNull_thenExpectAnEmptyStream() { - Collection collection = null; - Stream expResult = Stream.empty(); - Stream result = instance.collectionAsStream(collection); - assertStreamEquals(expResult, result); - - } - - @Test - public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() { - - Collection collection = Arrays.asList("a", "b", "c"); - Stream expResult = Arrays.stream(new String[] { "a", "b", "c" }); - Stream result = instance.collectionAsStream(collection); - assertStreamEquals(expResult, result); - } - - private static void assertStreamEquals(Stream s1, Stream s2) { - Iterator iter1 = s1.iterator(), iter2 = s2.iterator(); - while (iter1.hasNext() && iter2.hasNext()) - assertEquals(iter1.next(), iter2.next()); - assert !iter1.hasNext() && !iter2.hasNext(); - } - -} diff --git a/core-java-modules/core-java-8/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingJava8OptionalContainerUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingJava8OptionalContainerUnitTest.java deleted file mode 100644 index df6c72d346..0000000000 --- a/core-java-modules/core-java-8/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingJava8OptionalContainerUnitTest.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package com.baeldung.nullsafecollectionstreams; - -import static org.junit.Assert.assertEquals; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Iterator; -import java.util.stream.Stream; - -import org.junit.Test; - -/** - * - * @author Kwaje Anthony - */ -public class NullSafeCollectionStreamsUsingJava8OptionalContainerUnitTest { - - private final NullSafeCollectionStreamsUsingJava8OptionalContainer instance = new NullSafeCollectionStreamsUsingJava8OptionalContainer(); - - @Test - public void whenCollectionIsNull_thenExpectAnEmptyStream() { - Collection collection = null; - Stream expResult = Stream.empty(); - Stream result = instance.collectionAsStream(collection); - assertStreamEquals(expResult, result); - - } - - @Test - public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() { - - Collection collection = Arrays.asList("a", "b", "c"); - Stream expResult = Arrays.stream(new String[] { "a", "b", "c" }); - Stream result = instance.collectionAsStream(collection); - assertStreamEquals(expResult, result); - } - - private static void assertStreamEquals(Stream s1, Stream s2) { - Iterator iter1 = s1.iterator(), iter2 = s2.iterator(); - while (iter1.hasNext() && iter2.hasNext()) - assertEquals(iter1.next(), iter2.next()); - assert !iter1.hasNext() && !iter2.hasNext(); - } - -} diff --git a/core-java-modules/core-java-8/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingNullDereferenceCheckUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingNullDereferenceCheckUnitTest.java deleted file mode 100644 index ddb4dcdc12..0000000000 --- a/core-java-modules/core-java-8/src/test/java/com/baeldung/nullsafecollectionstreams/NullSafeCollectionStreamsUsingNullDereferenceCheckUnitTest.java +++ /dev/null @@ -1,45 +0,0 @@ - -package com.baeldung.nullsafecollectionstreams; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Iterator; -import java.util.stream.Stream; -import org.junit.Test; -import static org.junit.Assert.*; - -/** - * - * @author Kwaje Anthony - */ -public class NullSafeCollectionStreamsUsingNullDereferenceCheckUnitTest { - - private final NullSafeCollectionStreamsUsingNullDereferenceCheck instance = - new NullSafeCollectionStreamsUsingNullDereferenceCheck(); - - @Test - public void whenCollectionIsNull_thenExpectAnEmptyStream() { - Collection collection = null; - Stream expResult = Stream.empty(); - Stream result = instance.collectionAsStream(collection); - assertStreamEquals(expResult, result); - - } - - @Test - public void whenCollectionHasElements_thenExpectAStreamOfExactlyTheSameElements() { - - Collection collection = Arrays.asList("a", "b", "c"); - Stream expResult = Arrays.stream(new String[] { "a", "b", "c" }); - Stream result = instance.collectionAsStream(collection); - assertStreamEquals(expResult, result); - } - - private static void assertStreamEquals(Stream s1, Stream s2) { - Iterator iter1 = s1.iterator(), iter2 = s2.iterator(); - while (iter1.hasNext() && iter2.hasNext()) - assertEquals(iter1.next(), iter2.next()); - assert !iter1.hasNext() && !iter2.hasNext(); - } - -} diff --git a/core-java-modules/core-java-8/src/test/java/com/baeldung/strategy/StrategyDesignPatternUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/baeldung/strategy/StrategyDesignPatternUnitTest.java deleted file mode 100644 index 2238012e20..0000000000 --- a/core-java-modules/core-java-8/src/test/java/com/baeldung/strategy/StrategyDesignPatternUnitTest.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.baeldung.strategy; - -import org.junit.Test; - -import java.math.BigDecimal; -import java.util.Arrays; -import java.util.List; -import java.util.function.Function; - -import static com.baeldung.strategy.Discounter.christmas; -import static com.baeldung.strategy.Discounter.easter; -import static com.baeldung.strategy.Discounter.newYear; -import static org.assertj.core.api.Assertions.assertThat; - -public class StrategyDesignPatternUnitTest { - @Test - public void shouldDivideByTwo_WhenApplyingStaffDiscounter() { - Discounter staffDiscounter = new EasterDiscounter(); - - final BigDecimal discountedValue = staffDiscounter - .apply(BigDecimal.valueOf(100)); - - assertThat(discountedValue) - .isEqualByComparingTo(BigDecimal.valueOf(50)); - } - - @Test - public void shouldDivideByTwo_WhenApplyingStaffDiscounterWithAnonyousTypes() { - Discounter staffDiscounter = new Discounter() { - @Override - public BigDecimal apply(BigDecimal amount) { - return amount.multiply(BigDecimal.valueOf(0.5)); - } - }; - - final BigDecimal discountedValue = staffDiscounter - .apply(BigDecimal.valueOf(100)); - - assertThat(discountedValue) - .isEqualByComparingTo(BigDecimal.valueOf(50)); - } - - @Test - public void shouldDivideByTwo_WhenApplyingStaffDiscounterWithLamda() { - Discounter staffDiscounter = amount -> amount.multiply(BigDecimal.valueOf(0.5)); - - final BigDecimal discountedValue = staffDiscounter - .apply(BigDecimal.valueOf(100)); - - assertThat(discountedValue) - .isEqualByComparingTo(BigDecimal.valueOf(50)); - } - - @Test - public void shouldApplyAllDiscounts() { - List discounters = Arrays.asList(christmas(), newYear(), easter()); - - BigDecimal amount = BigDecimal.valueOf(100); - - final Discounter combinedDiscounter = discounters - .stream() - .reduce(v -> v, Discounter::combine); - - combinedDiscounter.apply(amount); - } - - @Test - public void shouldChainDiscounters() { - final Function combinedDiscounters = Discounter - .christmas() - .andThen(newYear()); - - combinedDiscounters.apply(BigDecimal.valueOf(100)); - } -} diff --git a/core-java-modules/core-java-8/src/test/java/com/baeldung/typeinference/TypeInferenceUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/baeldung/typeinference/TypeInferenceUnitTest.java deleted file mode 100644 index b9600a18cb..0000000000 --- a/core-java-modules/core-java-8/src/test/java/com/baeldung/typeinference/TypeInferenceUnitTest.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.baeldung.typeinference; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.junit.Test; - -public class TypeInferenceUnitTest { - - @Test - public void givenNoTypeInference_whenInvokingGenericMethodsWithTypeParameters_ObjectsAreCreated() { - // Without type inference. code is verbose. - Map> mapOfMaps = new HashMap>(); - List strList = Collections. emptyList(); - List intList = Collections. emptyList(); - - assertTrue(mapOfMaps.isEmpty()); - assertTrue(strList.isEmpty()); - assertTrue(intList.isEmpty()); - } - - @Test - public void givenTypeInference_whenInvokingGenericMethodsWithoutTypeParameters_ObjectsAreCreated() { - // With type inference. code is concise. - List strListInferred = Collections.emptyList(); - List intListInferred = Collections.emptyList(); - - assertTrue(strListInferred.isEmpty()); - assertTrue(intListInferred.isEmpty()); - } - - @Test - public void givenJava7_whenInvokingCostructorWithoutTypeParameters_ObjectsAreCreated() { - // Type Inference for constructor using diamond operator. - Map> mapOfMapsInferred = new HashMap<>(); - - assertTrue(mapOfMapsInferred.isEmpty()); - assertEquals("public class java.util.HashMap", mapOfMapsInferred.getClass() - .toGenericString()); - } - - static List add(List list, T a, T b) { - list.add(a); - list.add(b); - return list; - } - - @Test - public void givenGenericMethod_WhenInvokedWithoutExplicitTypes_TypesAreInferred() { - // Generalized target-type inference - List strListGeneralized = add(new ArrayList<>(), "abc", "def"); - List intListGeneralized = add(new ArrayList<>(), 1, 2); - List numListGeneralized = add(new ArrayList<>(), 1, 2.0); - - assertEquals("public class java.util.ArrayList", strListGeneralized.getClass() - .toGenericString()); - assertFalse(intListGeneralized.isEmpty()); - assertEquals(2, numListGeneralized.size()); - } - - @Test - public void givenLambdaExpressions_whenParameterTypesNotSpecified_ParameterTypesAreInferred() { - // Type Inference and Lambda Expressions. - List intList = Arrays.asList(5, 3, 4, 2, 1); - Collections.sort(intList, (a, b) -> { - assertEquals("java.lang.Integer", a.getClass().getName()); - return a.compareTo(b); - }); - assertEquals("[1, 2, 3, 4, 5]", Arrays.toString(intList.toArray())); - - List strList = Arrays.asList("Red", "Blue", "Green"); - Collections.sort(strList, (a, b) -> { - assertEquals("java.lang.String", a.getClass().getName()); - return a.compareTo(b); - }); - assertEquals("[Blue, Green, Red]", Arrays.toString(strList.toArray())); - } - -} diff --git a/core-java-modules/core-java-8/src/test/java/com/baeldung/util/CurrentDateTimeUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/baeldung/util/CurrentDateTimeUnitTest.java deleted file mode 100644 index ec20b7794b..0000000000 --- a/core-java-modules/core-java-8/src/test/java/com/baeldung/util/CurrentDateTimeUnitTest.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.baeldung.util; - -import static org.junit.Assert.assertEquals; - -import java.time.Clock; -import java.time.Instant; -import java.time.LocalDate; -import java.time.LocalTime; -import java.time.ZoneId; -import java.time.temporal.ChronoField; - -import org.junit.Test; - -public class CurrentDateTimeUnitTest { - - private static final Clock clock = Clock.fixed(Instant.parse("2016-10-09T15:10:30.00Z"), ZoneId.of("UTC")); - - @Test - public void shouldReturnCurrentDate() { - final LocalDate now = LocalDate.now(clock); - - assertEquals(9, now.get(ChronoField.DAY_OF_MONTH)); - assertEquals(10, now.get(ChronoField.MONTH_OF_YEAR)); - assertEquals(2016, now.get(ChronoField.YEAR)); - } - - @Test - public void shouldReturnCurrentTime() { - final LocalTime now = LocalTime.now(clock); - - assertEquals(15, now.get(ChronoField.HOUR_OF_DAY)); - assertEquals(10, now.get(ChronoField.MINUTE_OF_HOUR)); - assertEquals(30, now.get(ChronoField.SECOND_OF_MINUTE)); - } - - @Test - public void shouldReturnCurrentTimestamp() { - final Instant now = Instant.now(clock); - - assertEquals(clock.instant().getEpochSecond(), now.getEpochSecond()); - } -} diff --git a/core-java-modules/core-java-8/src/test/java/com/baeldung/monad/MonadSampleUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/ossez/monad/MonadSampleUnitTest.java similarity index 95% rename from core-java-modules/core-java-8/src/test/java/com/baeldung/monad/MonadSampleUnitTest.java rename to core-java-modules/core-java-8/src/test/java/com/ossez/monad/MonadSampleUnitTest.java index c851f5f750..9a7669c140 100644 --- a/core-java-modules/core-java-8/src/test/java/com/baeldung/monad/MonadSampleUnitTest.java +++ b/core-java-modules/core-java-8/src/test/java/com/ossez/monad/MonadSampleUnitTest.java @@ -1,5 +1,6 @@ -package com.baeldung.monad; +package com.ossez.monad; +import com.ossez.monad.*; import org.junit.Assert; import org.junit.Test; diff --git a/core-java-modules/core-java-8/src/test/java/com/ossez/nullsafecollectionstreams/NullSafeCollectionStreamsUsingCommonsEmptyIfNullUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/ossez/nullsafecollectionstreams/NullSafeCollectionStreamsUsingCommonsEmptyIfNullUnitTest.java index 12f9fcca48..3badf04c80 100644 --- a/core-java-modules/core-java-8/src/test/java/com/ossez/nullsafecollectionstreams/NullSafeCollectionStreamsUsingCommonsEmptyIfNullUnitTest.java +++ b/core-java-modules/core-java-8/src/test/java/com/ossez/nullsafecollectionstreams/NullSafeCollectionStreamsUsingCommonsEmptyIfNullUnitTest.java @@ -4,13 +4,14 @@ import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.stream.Stream; + import org.junit.Test; import static org.junit.Assert.*; public class NullSafeCollectionStreamsUsingCommonsEmptyIfNullUnitTest { - private final NullSafeCollectionStreamsUsingCommonsEmptyIfNull instance = + private final NullSafeCollectionStreamsUsingCommonsEmptyIfNull instance = new NullSafeCollectionStreamsUsingCommonsEmptyIfNull(); @Test diff --git a/core-java-modules/core-java-8/src/test/java/com/ossez/nullsafecollectionstreams/NullSafeCollectionStreamsUsingNullDereferenceCheckUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/ossez/nullsafecollectionstreams/NullSafeCollectionStreamsUsingNullDereferenceCheckUnitTest.java index 4cd3fc6539..a34f2492b3 100644 --- a/core-java-modules/core-java-8/src/test/java/com/ossez/nullsafecollectionstreams/NullSafeCollectionStreamsUsingNullDereferenceCheckUnitTest.java +++ b/core-java-modules/core-java-8/src/test/java/com/ossez/nullsafecollectionstreams/NullSafeCollectionStreamsUsingNullDereferenceCheckUnitTest.java @@ -5,6 +5,7 @@ import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.stream.Stream; + import org.junit.Test; import static org.junit.Assert.*; @@ -14,7 +15,7 @@ import static org.junit.Assert.*; */ public class NullSafeCollectionStreamsUsingNullDereferenceCheckUnitTest { - private final NullSafeCollectionStreamsUsingNullDereferenceCheck instance = + private final NullSafeCollectionStreamsUsingNullDereferenceCheck instance = new NullSafeCollectionStreamsUsingNullDereferenceCheck(); @Test diff --git a/core-java-modules/core-java-8/src/test/java/com/baeldung/spliterator/ExecutorUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/ossez/spliterator/ExecutorUnitTest.java similarity index 96% rename from core-java-modules/core-java-8/src/test/java/com/baeldung/spliterator/ExecutorUnitTest.java rename to core-java-modules/core-java-8/src/test/java/com/ossez/spliterator/ExecutorUnitTest.java index 30760ddf6f..2a2099a78d 100644 --- a/core-java-modules/core-java-8/src/test/java/com/baeldung/spliterator/ExecutorUnitTest.java +++ b/core-java-modules/core-java-8/src/test/java/com/ossez/spliterator/ExecutorUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.spliterator; +package com.ossez.spliterator; import static org.assertj.core.api.Assertions.assertThat; @@ -10,6 +10,10 @@ import java.util.concurrent.ForkJoinPool; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import java.util.stream.Stream; + +import com.ossez.spliterator.Article; +import com.ossez.spliterator.Author; +import com.ossez.spliterator.CustomSpliterator; import org.junit.Before; import org.junit.Test; diff --git a/core-java-modules/core-java-8/src/test/java/com/ossez/strategy/StrategyDesignPatternUnitTest.java b/core-java-modules/core-java-8/src/test/java/com/ossez/strategy/StrategyDesignPatternUnitTest.java index d10e283568..42c13ed246 100644 --- a/core-java-modules/core-java-8/src/test/java/com/ossez/strategy/StrategyDesignPatternUnitTest.java +++ b/core-java-modules/core-java-8/src/test/java/com/ossez/strategy/StrategyDesignPatternUnitTest.java @@ -7,6 +7,9 @@ import java.util.Arrays; import java.util.List; import java.util.function.Function; +import static com.ossez.strategy.Discounter.christmas; +import static com.ossez.strategy.Discounter.easter; +import static com.ossez.strategy.Discounter.newYear; import static org.assertj.core.api.Assertions.assertThat; public class StrategyDesignPatternUnitTest { @@ -50,7 +53,7 @@ public class StrategyDesignPatternUnitTest { @Test public void shouldApplyAllDiscounts() { - List discounters = Arrays.asList(Discounter.christmas(), Discounter.newYear(), Discounter.easter()); + List discounters = Arrays.asList(christmas(), newYear(), easter()); BigDecimal amount = BigDecimal.valueOf(100); @@ -65,7 +68,7 @@ public class StrategyDesignPatternUnitTest { public void shouldChainDiscounters() { final Function combinedDiscounters = Discounter .christmas() - .andThen(Discounter.newYear()); + .andThen(newYear()); combinedDiscounters.apply(BigDecimal.valueOf(100)); }