Skip to content

Commit 3207ee0

Browse files
authored
Merge branch 'master' into core-java-move-1
2 parents 88a20dc + 663d533 commit 3207ee0

File tree

328 files changed

+5462
-5575
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

328 files changed

+5462
-5575
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.baeldung.bucketsort;
2+
3+
import java.util.ArrayList;
4+
import java.util.Comparator;
5+
import java.util.List;
6+
7+
public class IntegerBucketSorter implements Sorter<Integer> {
8+
9+
private final Comparator<Integer> comparator;
10+
11+
public IntegerBucketSorter(Comparator<Integer> comparator) {
12+
this.comparator = comparator;
13+
}
14+
15+
public IntegerBucketSorter() {
16+
comparator = Comparator.naturalOrder();
17+
}
18+
19+
public List<Integer> sort(List<Integer> arrayToSort) {
20+
21+
List<List<Integer>> buckets = splitIntoUnsortedBuckets(arrayToSort);
22+
23+
for(List<Integer> bucket : buckets){
24+
bucket.sort(comparator);
25+
}
26+
27+
return concatenateSortedBuckets(buckets);
28+
}
29+
30+
private List<Integer> concatenateSortedBuckets(List<List<Integer>> buckets){
31+
List<Integer> sortedArray = new ArrayList<>();
32+
int index = 0;
33+
for(List<Integer> bucket : buckets){
34+
for(int number : bucket){
35+
sortedArray.add(index++, number);
36+
}
37+
}
38+
return sortedArray;
39+
}
40+
41+
private List<List<Integer>> splitIntoUnsortedBuckets(List<Integer> initialList){
42+
43+
final int[] codes = createHashes(initialList);
44+
45+
List<List<Integer>> buckets = new ArrayList<>(codes[1]);
46+
for(int i = 0; i < codes[1]; i++) buckets.add(new ArrayList<>());
47+
48+
//distribute the data
49+
for (int i : initialList) {
50+
buckets.get(hash(i, codes)).add(i);
51+
}
52+
return buckets;
53+
54+
}
55+
56+
private int[] createHashes(List<Integer> input){
57+
int m = input.get(0);
58+
for (int i = 1; i < input.size(); i++) {
59+
if (m < input.get(i)) {
60+
m = input.get(i);
61+
}
62+
}
63+
return new int[]{m, (int) Math.sqrt(input.size())};
64+
}
65+
66+
private static int hash(int i, int[] code) {
67+
return (int) ((double) i / code[0] * (code[1] - 1));
68+
}
69+
70+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.bucketsort;
2+
3+
import java.util.List;
4+
5+
public interface Sorter<T> {
6+
7+
List<T> sort(List<T> arrayToSort);
8+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.baeldung.bucketsort;
2+
3+
import com.baeldung.bucketsort.IntegerBucketSorter;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
import static org.junit.Assert.assertEquals;
11+
12+
public class IntegerBucketSorterUnitTest {
13+
14+
private IntegerBucketSorter sorter;
15+
16+
@Before
17+
public void setUp() throws Exception {
18+
sorter = new IntegerBucketSorter();
19+
}
20+
21+
@Test
22+
public void givenUnsortedList_whenSortedUsingBucketSorter_checkSortingCorrect() {
23+
24+
List<Integer> unsorted = Arrays.asList(80,50,60,30,20,10,70,0,40,500,600,602,200,15);
25+
List<Integer> expected = Arrays.asList(0,10,15,20,30,40,50,60,70,80,200,500,600,602);
26+
27+
List<Integer> actual = sorter.sort(unsorted);
28+
29+
assertEquals(expected, actual);
30+
31+
32+
}
33+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.baeldung.algorithms.radixsort;
2+
3+
import java.util.Arrays;
4+
5+
public class RadixSort {
6+
7+
public static void sort(int numbers[]) {
8+
int maximumNumber = findMaximumNumberIn(numbers);
9+
10+
int numberOfDigits = calculateNumberOfDigitsIn(maximumNumber);
11+
12+
int placeValue = 1;
13+
14+
while (numberOfDigits-- > 0) {
15+
applyCountingSortOn(numbers, placeValue);
16+
placeValue *= 10;
17+
}
18+
}
19+
20+
private static void applyCountingSortOn(int[] numbers, int placeValue) {
21+
int range = 10; // radix or the base
22+
23+
int length = numbers.length;
24+
int[] frequency = new int[range];
25+
int[] sortedValues = new int[length];
26+
27+
for (int i = 0; i < length; i++) {
28+
int digit = (numbers[i] / placeValue) % range;
29+
frequency[digit]++;
30+
}
31+
32+
for (int i = 1; i < 10; i++) {
33+
frequency[i] += frequency[i - 1];
34+
}
35+
36+
for (int i = length - 1; i >= 0; i--) {
37+
int digit = (numbers[i] / placeValue) % range;
38+
sortedValues[frequency[digit] - 1] = numbers[i];
39+
frequency[digit]--;
40+
}
41+
42+
System.arraycopy(sortedValues, 0, numbers, 0, length);
43+
}
44+
45+
private static int calculateNumberOfDigitsIn(int number) {
46+
return (int) Math.log10(number) + 1; // valid only if number > 0
47+
}
48+
49+
private static int findMaximumNumberIn(int[] arr) {
50+
return Arrays.stream(arr).max().getAsInt();
51+
}
52+
53+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.algorithms.radixsort;
2+
3+
import static org.junit.Assert.assertArrayEquals;
4+
5+
import org.junit.Test;
6+
7+
public class RadixSortUnitTest {
8+
9+
@Test
10+
public void givenUnsortedArrayWhenRadixSortThenArraySorted() {
11+
int[] numbers = { 387, 468, 134, 123, 68, 221, 769, 37, 7 };
12+
RadixSort.sort(numbers);
13+
int[] numbersSorted = { 7, 37, 68, 123, 134, 221, 387, 468, 769 };
14+
assertArrayEquals(numbersSorted, numbers);
15+
}
16+
}

core-groovy-2/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
<version>${spock-core.version}</version>
4949
<scope>test</scope>
5050
</dependency>
51+
<dependency>
52+
<groupId>com.github.groovy-wslite</groupId>
53+
<artifactId>groovy-wslite</artifactId>
54+
<version>${groovy-wslite.version}</version>
55+
</dependency>
5156
</dependencies>
5257

5358
<build>
@@ -175,6 +180,7 @@
175180
<junit.platform.version>1.0.0</junit.platform.version>
176181
<hsqldb.version>2.4.0</hsqldb.version>
177182
<spock-core.version>1.1-groovy-2.4</spock-core.version>
183+
<groovy-wslite.version>1.1.3</groovy-wslite.version>
178184
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
179185
<logback.version>1.2.3</logback.version>
180186
<groovy.version>2.5.7</groovy.version>
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package com.baeldung.webservice
2+
3+
import groovy.json.JsonSlurper
4+
import wslite.rest.ContentType
5+
import wslite.rest.RESTClient
6+
import wslite.rest.RESTClientException
7+
import wslite.soap.SOAPClient
8+
import wslite.soap.SOAPMessageBuilder
9+
import wslite.http.auth.HTTPBasicAuthorization
10+
import org.junit.Test
11+
12+
class WebserviceUnitTest extends GroovyTestCase {
13+
14+
JsonSlurper jsonSlurper = new JsonSlurper()
15+
16+
static RESTClient client = new RESTClient("https://postman-echo.com")
17+
18+
static {
19+
client.defaultAcceptHeader = ContentType.JSON
20+
client.httpClient.sslTrustAllCerts = true
21+
}
22+
23+
void test_whenSendingGet_thenRespose200() {
24+
def postmanGet = new URL('https://postman-echo.com/get')
25+
def getConnection = postmanGet.openConnection()
26+
getConnection.requestMethod = 'GET'
27+
assert getConnection.responseCode == 200
28+
if (getConnection.responseCode == 200) {
29+
assert jsonSlurper.parseText(getConnection.content.text)?.headers?.host == "postman-echo.com"
30+
}
31+
}
32+
33+
void test_whenSendingPost_thenRespose200() {
34+
def postmanPost = new URL('https://postman-echo.com/post')
35+
def query = "q=This is post request form parameter."
36+
def postConnection = postmanPost.openConnection()
37+
postConnection.requestMethod = 'POST'
38+
assert postConnection.responseCode == 200
39+
}
40+
41+
void test_whenSendingPostWithParams_thenRespose200() {
42+
def postmanPost = new URL('https://postman-echo.com/post')
43+
def form = "param1=This is request parameter."
44+
def postConnection = postmanPost.openConnection()
45+
postConnection.requestMethod = 'POST'
46+
postConnection.doOutput = true
47+
def text
48+
postConnection.with {
49+
outputStream.withWriter { outputStreamWriter ->
50+
outputStreamWriter << form
51+
}
52+
text = content.text
53+
}
54+
assert postConnection.responseCode == 200
55+
assert jsonSlurper.parseText(text)?.json.param1 == "This is request parameter."
56+
}
57+
58+
void test_whenReadingRSS_thenReceiveFeed() {
59+
def rssFeed = new XmlParser().parse("https://news.google.com/rss?hl=en-US&gl=US&ceid=US:en")
60+
def stories = []
61+
(0..4).each {
62+
def item = rssFeed.channel.item.get(it)
63+
stories << item.title.text()
64+
}
65+
assert stories.size() == 5
66+
}
67+
68+
void test_whenReadingAtom_thenReceiveFeed() {
69+
def atomFeed = new XmlParser().parse("https://news.google.com/atom?hl=en-US&gl=US&ceid=US:en")
70+
def stories = []
71+
(0..4).each {
72+
def entry = atomFeed.entry.get(it)
73+
stories << entry.title.text()
74+
}
75+
assert stories.size() == 5
76+
}
77+
78+
void test_whenConsumingSoap_thenReceiveResponse() {
79+
def url = "http://www.dataaccess.com/webservicesserver/numberconversion.wso"
80+
def soapClient = new SOAPClient(url)
81+
def message = new SOAPMessageBuilder().build({
82+
body {
83+
NumberToWords(xmlns: "http://www.dataaccess.com/webservicesserver/") {
84+
ubiNum(1234)
85+
}
86+
}
87+
})
88+
def response = soapClient.send(message.toString());
89+
def words = response.NumberToWordsResponse
90+
assert words == "one thousand two hundred and thirty four "
91+
}
92+
93+
void test_whenConsumingRestGet_thenReceiveResponse() {
94+
def path = "/get"
95+
def response
96+
try {
97+
response = client.get(path: path)
98+
assert response.statusCode == 200
99+
assert response.json?.headers?.host == "postman-echo.com"
100+
} catch (RESTClientException e) {
101+
assert e?.response?.statusCode != 200
102+
}
103+
}
104+
105+
void test_whenConsumingRestPost_thenReceiveResponse() {
106+
def path = "/post"
107+
def params = ["foo":1,"bar":2]
108+
def response
109+
try {
110+
response = client.post(path: path) {
111+
type ContentType.JSON
112+
json params
113+
}
114+
assert response.json?.data == params
115+
} catch (RESTClientException e) {
116+
e.printStackTrace()
117+
assert e?.response?.statusCode != 200
118+
}
119+
}
120+
121+
void test_whenBasicAuthentication_thenReceive200() {
122+
def path = "/basic-auth"
123+
def response
124+
try {
125+
client.authorization = new HTTPBasicAuthorization("postman", "password")
126+
response = client.get(path: path)
127+
assert response.statusCode == 200
128+
assert response.json?.authenticated == true
129+
} catch (RESTClientException e) {
130+
e.printStackTrace()
131+
assert e?.response?.statusCode != 200
132+
}
133+
}
134+
135+
void test_whenOAuth_thenReceive200() {
136+
RESTClient oAuthClient = new RESTClient("https://postman-echo.com")
137+
oAuthClient.defaultAcceptHeader = ContentType.JSON
138+
oAuthClient.httpClient.sslTrustAllCerts = true
139+
140+
def path = "/oauth1"
141+
def params = [oauth_consumer_key: "RKCGzna7bv9YD57c", oauth_signature_method: "HMAC-SHA1", oauth_timestamp:1567089944, oauth_nonce: "URT7v4", oauth_version: 1.0, oauth_signature: 'RGgR/ktDmclkM0ISWaFzebtlO0A=']
142+
def response
143+
try {
144+
response = oAuthClient.get(path: path, query: params)
145+
assert response.statusCode == 200
146+
assert response.statusMessage == "OK"
147+
assert response.json.status == "pass"
148+
} catch (RESTClientException e) {
149+
assert e?.response?.statusCode != 200
150+
}
151+
}
152+
}

core-java-modules/core-java-8/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
- [Finding Min/Max in an Array with Java](http://www.baeldung.com/java-array-min-max)
2121
- [Internationalization and Localization in Java 8](http://www.baeldung.com/java-8-localization)
2222
- [Java Optional – orElse() vs orElseGet()](http://www.baeldung.com/java-optional-or-else-vs-or-else-get)
23-
- [Method Parameter Reflection in Java](http://www.baeldung.com/java-parameter-reflection)
2423
- [Java 8 Unsigned Arithmetic Support](http://www.baeldung.com/java-unsigned-arithmetic)
2524
- [Generalized Target-Type Inference in Java](http://www.baeldung.com/java-generalized-target-type-inference)
2625
- [Overriding System Time for Testing in Java](http://www.baeldung.com/java-override-system-time)

core-java-modules/core-java-8/pom.xml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,6 @@
134134
</resources>
135135

136136
<plugins>
137-
<plugin>
138-
<groupId>org.apache.maven.plugins</groupId>
139-
<artifactId>maven-compiler-plugin</artifactId>
140-
<version>${maven-compiler-plugin.version}</version>
141-
<configuration>
142-
<source>1.8</source>
143-
<target>1.8</target>
144-
<compilerArgument>-parameters</compilerArgument>
145-
</configuration>
146-
</plugin>
147137
<plugin>
148138
<groupId>org.springframework.boot</groupId>
149139
<artifactId>spring-boot-maven-plugin</artifactId>
@@ -192,7 +182,6 @@
192182
<jmh-generator.version>1.19</jmh-generator.version>
193183
<spring-boot-maven-plugin.version>2.0.4.RELEASE</spring-boot-maven-plugin.version>
194184
<!-- plugins -->
195-
<maven-compiler-plugin.version>3.8.0</maven-compiler-plugin.version>
196185
<maven-surefire-plugin.version>2.22.1</maven-surefire-plugin.version>
197186
</properties>
198187
</project>

0 commit comments

Comments
 (0)