This commit is contained in:
Jonathan Cook
2020-11-13 16:45:25 +01:00
57 changed files with 1159 additions and 130 deletions
+1
View File
@@ -10,3 +10,4 @@ This module contains articles about Java 10 core features
- [Deep Dive Into the New Java JIT Compiler – Graal](https://www.baeldung.com/graal-java-jit-compiler)
- [Copying Sets in Java](https://www.baeldung.com/java-copy-sets)
- [Converting between a List and a Set in Java](https://www.baeldung.com/convert-list-to-set-and-set-to-list)
- [Java IndexOutOfBoundsException “Source Does Not Fit in Dest”](https://www.baeldung.com/java-indexoutofboundsexception)
@@ -4,3 +4,5 @@
- [IllegalArgumentException or NullPointerException for a Null Parameter?](https://www.baeldung.com/java-illegalargumentexception-or-nullpointerexception)
- [IllegalMonitorStateException in Java](https://www.baeldung.com/java-illegalmonitorstateexception)
- [AbstractMethodError in Java](https://www.baeldung.com/java-abstractmethoderror)
- [Java IndexOutOfBoundsException “Source Does Not Fit in Dest”](https://www.baeldung.com/java-indexoutofboundsexception)
- [Localizing Exception Messages in Java](https://www.baeldung.com/java-localize-exception-messages)
@@ -0,0 +1,14 @@
package com.baeldung.exception.indexoutofbounds;
import java.util.ArrayList;
import java.util.List;
public class CopyListUsingAddAllMethodDemo {
static List<Integer> copyList(List<Integer> source) {
List<Integer> destination = new ArrayList<>();
destination.addAll(source);
return destination;
}
}
@@ -0,0 +1,10 @@
package com.baeldung.exception.indexoutofbounds;
import java.util.Collections;
import java.util.List;
public class CopyListUsingCollectionsCopyMethodDemo {
static void copyList(List<Integer> source, List<Integer> destination) {
Collections.copy(destination, source);
}
}
@@ -0,0 +1,10 @@
package com.baeldung.exception.indexoutofbounds;
import java.util.ArrayList;
import java.util.List;
public class CopyListUsingConstructorDemo {
static List<Integer> copyList(List<Integer> source) {
return new ArrayList<>(source);
}
}
@@ -0,0 +1,12 @@
package com.baeldung.exception.indexoutofbounds;
import java.util.List;
import java.util.stream.Collectors;
public class CopyListUsingJava8StreamDemo {
static List<Integer> copyList(List<Integer> source) {
return source
.stream()
.collect(Collectors.toList());
}
}
@@ -0,0 +1,13 @@
package com.baeldung.exception.indexoutofbounds;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class IndexOutOfBoundsExceptionDemo {
static List<Integer> copyList(List<Integer> source) {
List<Integer> destination = new ArrayList<>(source.size());
Collections.copy(destination, source);
return destination;
}
}
@@ -0,0 +1,47 @@
package com.baeldung.exceptions.localization;
import java.util.Locale;
public class LocalizedException extends Exception {
private static final long serialVersionUID = 1L;
private final String messageKey;
private final Locale locale;
public LocalizedException(String messageKey) {
this(messageKey, Locale.getDefault());
}
public LocalizedException(String messageKey, Locale locale) {
this.messageKey = messageKey;
this.locale = locale;
}
/**
* @return a localized message based on the messageKey provided at instantiation.
*/
public String getMessage() {
/*
* This is a deliberate role reversal of the default implementation of getLocalizedMessage.
* some logging frameworks like Log4J 1 & 2 and Logback will use getMessage instead of
* getLocalizedMessage when logging Throwables. If we want to use these frameworks in client
* applications to log localized messages, then we'll need to override getMessage in a
* similar fashion to return the appropriate content. Or, you can call getLocalizedMessage
* on your own to create the log content.
*/
return getLocalizedMessage();
}
/**
* @return a localized message based on the messageKey provided at instantiation.
*/
public String getLocalizedMessage() {
/*
* java.util.logging uses getLocalizedMessage when logging Throwables.
*/
return Messages.getMessageForLocale(messageKey, locale);
}
}
@@ -0,0 +1,27 @@
package com.baeldung.exceptions.localization;
import java.util.Locale;
import java.util.ResourceBundle;
public class Messages {
/**
* Retrieves the value for the messageKey from the locale-specific messages.properties, or from
* the base messages.properties for unsupported locales.
*
* @param messageKey The key for the message in the messages.properties ResourceBundle.
* @param locale The locale to search the message key.
* @return The value defined for the messageKey in the provided locale.
*/
public static String getMessageForLocale(String messageKey, Locale locale) {
/*
* For more complex implementations, you will want a var-args parameter for MessageFormat
* substitutions. Then we can read the value from the bundle and pass the value with the
* substitutions to MessageFormat to create the final message value.
*/
return ResourceBundle.getBundle("messages", locale)
.getString(messageKey);
}
}
@@ -0,0 +1 @@
message.exception = I am an exception.
@@ -0,0 +1 @@
message.exception = Je suis une exception.
@@ -0,0 +1,18 @@
package com.baeldung.exception.indexoutofbounds;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class CopyListUsingAddAllMethodDemoUnitTest {
@Test
void whenPassValidArrayList_thenCopyListUsingAddAllMethod() {
List<Integer> source = Arrays.asList(11, 22, 33);
assertEquals(source, CopyListUsingAddAllMethodDemo.copyList(source));
}
}
@@ -0,0 +1,31 @@
package com.baeldung.exception.indexoutofbounds;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class CopyListUsingCollectionsCopyMethodDemoUnitTest {
@Test
void whenCopyListUsingCollectionsCopy_thenOverrideAllDestinationListValues() {
List<Integer> source = Arrays.asList(11, 22, 33);
List<Integer> destination = Arrays.asList(1, 2, 3);
CopyListUsingCollectionsCopyMethodDemo.copyList(source, destination);
assertEquals(source, destination);
}
@Test
void whenCopyListUsingCollectionsCopy_thenOverrideInitialDestinationValuesAndOthersShouldBeUnchanged(){
List<Integer> source = Arrays.asList(11, 22, 33);
List<Integer> destination = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> expectedList = Arrays.asList(11, 22, 33, 4, 5);
CopyListUsingCollectionsCopyMethodDemo.copyList(source, destination);
assertEquals(expectedList, destination);
}
}
@@ -0,0 +1,18 @@
package com.baeldung.exception.indexoutofbounds;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class CopyListUsingConstructorDemoUnitTest {
@Test
void whenCopyListUsingConstructor_thenMakeACopyOfList() {
List<Integer> source = Arrays.asList(11, 22, 33);
assertEquals(source, CopyListUsingConstructorDemo.copyList(source));
}
}
@@ -0,0 +1,18 @@
package com.baeldung.exception.indexoutofbounds;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
class CopyListUsingJava8StreamDemoUnitTest {
@Test
void whenCopyListUsingStream_thenMakeACopyOfArrayList() {
List<Integer> source = Arrays.asList(11, 22, 33);
assertEquals(source, CopyListUsingJava8StreamDemo.copyList(source));
}
}
@@ -0,0 +1,28 @@
package com.baeldung.exception.indexoutofbounds;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class IndexOutOfBoundsExceptionDemoUnitTest {
@Test
void givenDestinationArrayListSizeIsZero_whenCopySourceArrayListToDestination_thenShouldThrowIndexOutOfBoundsException() {
List<Integer> source = Arrays.asList(1, 2, 3, 4, 5);
assertThrows(IndexOutOfBoundsException.class, () -> IndexOutOfBoundsExceptionDemo.copyList(source));
}
@Test
void givenSourceAndDestinationListSizeIsEqual_whenCopySourceArrayListToDestination_thenShouldNotThrowIndexOutOfBoundsException() {
List<Integer> source = Collections.emptyList();
assertEquals(source, IndexOutOfBoundsExceptionDemo.copyList(source));
}
}
@@ -0,0 +1,61 @@
package com.baeldung.exceptions.localization;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Locale;
import static org.assertj.core.api.Assertions.assertThat;
public class LocalizedExceptionUnitTest {
private Locale originalDefaultLocale;
@Before
public void saveOriginalDefaultLocale() {
originalDefaultLocale = Locale.getDefault();
}
@After
public void restoreOriginalDefaultLocale() {
Locale.setDefault(originalDefaultLocale);
}
@Test
public void givenUsEnglishDefaultLocale_whenLocalizingMessage_thenMessageComesFromDefaultMessages() {
Locale.setDefault(Locale.US);
LocalizedException localizedException = new LocalizedException("message.exception");
String usEnglishLocalizedExceptionMessage = localizedException.getLocalizedMessage();
assertThat(usEnglishLocalizedExceptionMessage).isEqualTo("I am an exception.");
}
@Test
public void givenFranceFrenchDefaultLocale_whenLocalizingMessage_thenMessageComesFromFrenchTranslationMessages() {
Locale.setDefault(Locale.FRANCE);
LocalizedException localizedException = new LocalizedException("message.exception");
String franceFrenchLocalizedExceptionMessage = localizedException.getLocalizedMessage();
assertThat(franceFrenchLocalizedExceptionMessage).isEqualTo("Je suis une exception.");
}
@Test
public void givenUsEnglishProvidedLocale_whenLocalizingMessage_thenMessageComesFromDefaultMessage() {
LocalizedException localizedException = new LocalizedException("message.exception", Locale.US);
String usEnglishLocalizedExceptionMessage = localizedException.getLocalizedMessage();
assertThat(usEnglishLocalizedExceptionMessage).isEqualTo("I am an exception.");
}
@Test
public void givenFranceFrenchProvidedLocale_whenLocalizingMessage_thenMessageComesFromFrenchTranslationMessages() {
LocalizedException localizedException = new LocalizedException("message.exception", Locale.FRANCE);
String franceFrenchLocalizedExceptionMessage = localizedException.getLocalizedMessage();
assertThat(franceFrenchLocalizedExceptionMessage).isEqualTo("Je suis une exception.");
}
}
@@ -0,0 +1,25 @@
package com.baeldung.exceptions.localization;
import org.junit.Test;
import java.util.Locale;
import static org.assertj.core.api.Assertions.assertThat;
public class MessagesUnitTest {
@Test
public void givenUsEnglishLocale_whenRetrievingMessage_thenEnglishTranslationIsReturned() {
String translatedMessage = Messages.getMessageForLocale("message.exception", Locale.US);
assertThat(translatedMessage).isEqualTo("I am an exception.");
}
@Test
public void givenFranceFrenchLocale_whenRetrievingMessage_thenFrenchTranslationIsReturned() {
String translatedMessage = Messages.getMessageForLocale("message.exception", Locale.FRANCE);
assertThat(translatedMessage).isEqualTo("Je suis une exception.");
}
}
@@ -1 +1,3 @@
## Relevant articles:
- [Functional Programming in Java](https://www.baeldung.com/java-functional-programming)
@@ -8,4 +8,5 @@ This module contains articles about core features in the Java language
- [Checking if a Class Exists in Java](https://www.baeldung.com/java-check-class-exists)
- [The Difference Between a.getClass() and A.class in Java](https://www.baeldung.com/java-getclass-vs-class)
- [Constants in Java: Patterns and Anti-Patterns](https://www.baeldung.com/java-constants-good-practices)
- [The transient Keyword in Java](https://www.baeldung.com/java-transient-keyword)
- [[<-- Prev]](/core-java-modules/core-java-lang-2)
@@ -12,3 +12,4 @@ This module contains articles about types in Java
- [Attaching Values to Java Enum](https://www.baeldung.com/java-enum-values)
- [A Guide to Java Enums](https://www.baeldung.com/a-guide-to-java-enums)
- [Determine if an Object is of Primitive Type](https://www.baeldung.com/java-object-primitive-type)
- [Extending Enums in Java](https://www.baeldung.com/java-extending-enums)
@@ -1,3 +1,4 @@
### Relevant Articles:
- [Version Comparison in Java](https://www.baeldung.com/java-comparing-versions)
- [Java (String) or .toString()?](https://www.baeldung.com/java-string-casting-vs-tostring)
@@ -1,4 +1,4 @@
package com.baeldung.channles
package com.baeldung.channels
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.channels.Channel
@@ -1,4 +1,4 @@
package com.baeldung.channles
package com.baeldung.channels
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.channels.Channel
@@ -1,6 +1,6 @@
package com.baeldung.channles
package com.baeldung.channels
import com.baeldung.channles.OrderStatus.*
import com.baeldung.channels.OrderStatus.*
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.produce
@@ -1,4 +1,4 @@
package com.baeldung.channles
package com.baeldung.channels
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -1,4 +1,4 @@
package com.baeldung.channles
package com.baeldung.channels
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
@@ -1,4 +1,4 @@
package com.baeldung.channles
package com.baeldung.channels
import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.channels.Channel
@@ -1,4 +1,4 @@
package com.baeldung.channles
package com.baeldung.channels
import kotlinx.coroutines.*
import kotlinx.coroutines.channels.ReceiveChannel
@@ -1,4 +1,4 @@
package com.baeldung.channles
package com.baeldung.channels
import kotlinx.coroutines.channels.ticker
import kotlinx.coroutines.delay

Some files were not shown because too many files have changed in this diff Show More