Skip to content

Commit 694fbc5

Browse files
authored
Merge branch 'master' into master
2 parents 2215d17 + 87049b6 commit 694fbc5

138 files changed

Lines changed: 1841 additions & 419 deletions

File tree

Some content is hidden

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

‎core-java/README.md‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,21 @@
113113
- [Difference Between Wait and Sleep in Java](http://www.baeldung.com/java-wait-and-sleep)
114114
- [LongAdder and LongAccumulator in Java](http://www.baeldung.com/java-longadder-and-longaccumulator)
115115
- [Using Java MappedByteBuffer](http://www.baeldung.com/java-mapped-byte-buffer)
116-
116+
- [Dynamic Proxies in Java](http://www.baeldung.com/java-dynamic-proxies)
117+
- [How to Copy an Array in Java](http://www.baeldung.com/java-array-copy)
118+
- [Introduction to JDBC](http://www.baeldung.com/java-jdbc)
119+
- [Guide to CopyOnWriteArrayList](http://www.baeldung.com/java-copy-on-write-arraylist)
120+
- [Period and Duration in Java](http://www.baeldung.com/java-period-duration)
121+
- [Converting a Stack Trace to a String in Java](http://www.baeldung.com/java-stacktrace-to-string)
122+
- [Guide to the Java Phaser](http://www.baeldung.com/java-phaser)
123+
- [Count Occurrences of a Char in a String](http://www.baeldung.com/java-count-chars)
124+
- [Java Double Brace Initialization](http://www.baeldung.com/java-double-brace-initialization)
125+
- [The StackOverflowError in Java](http://www.baeldung.com/java-stack-overflow-error)
126+
- [Split a String in Java](http://www.baeldung.com/java-split-string)
127+
- [Introduction to Java Serialization](http://www.baeldung.com/java-serialization)
128+
- [How to Remove the Last Character of a String?](http://www.baeldung.com/java-remove-last-character-of-string)
129+
- [Guide to Synchronized Keyword in Java](http://www.baeldung.com/java-synchronized)
130+
- [ClassNotFoundException vs NoClassDefFoundError](http://www.baeldung.com/java-classnotfoundexception-and-noclassdeffounderror)
131+
- [Guide to UUID in Java](http://www.baeldung.com/java-uuid)
132+
- [How to Get the Last Element of a Stream in Java?](http://www.baeldung.com/java-stream-last-element)
133+
- [Guide to Escaping Characters in Java RegExps](http://www.baeldung.com/java-regexp-escape-char)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.baeldung.javanetworking.url;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.net.HttpURLConnection;
7+
import java.net.URL;
8+
import java.net.URLConnection;
9+
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
public class URLDemo {
14+
private final Logger log = LoggerFactory.getLogger(URLDemo.class);
15+
16+
String URLSTRING = "https://wordpress.org:443/support/topic/page-jumps-within-wordpress/?replies=3#post-2278484";
17+
// parsed locator
18+
String URLPROTOCOL = "https";
19+
// final static String URLAUTHORITY = "wordpress.org:443";
20+
String URLHOST = "wordpress.org";
21+
String URLPATH = "/support/topic/page-jumps-within-wordpress/";
22+
// final static String URLFILENAME = "/support/topic/page-jumps-within-wordpress/?replies=3";
23+
// final static int URLPORT = 443;
24+
int URLDEFAULTPORT = 443;
25+
String URLQUERY = "replies=3";
26+
String URLREFERENCE = "post-2278484";
27+
String URLCOMPOUND = URLPROTOCOL + "://" + URLHOST + ":" + URLDEFAULTPORT + URLPATH + "?" + URLQUERY + "#" + URLREFERENCE;
28+
29+
URL url;
30+
URLConnection urlConnection = null;
31+
HttpURLConnection connection = null;
32+
BufferedReader in = null;
33+
String urlContent = "";
34+
35+
public String testURL(String urlString) throws IOException, IllegalArgumentException {
36+
String urlStringCont = "";
37+
// comment the if clause if experiment with URL
38+
/*if (!URLSTRING.equals(urlString)) {
39+
throw new IllegalArgumentException("URL String argument is not proper: " + urlString);
40+
}*/
41+
// creating URL object
42+
url = new URL(urlString);
43+
// get URL connection
44+
urlConnection = url.openConnection();
45+
connection = null;
46+
// we can check, if connection is proper type
47+
if (urlConnection instanceof HttpURLConnection) {
48+
connection = (HttpURLConnection) urlConnection;
49+
} else {
50+
log.info("Please enter an HTTP URL");
51+
throw new IOException("HTTP URL is not correct");
52+
}
53+
// we can check response code (200 OK is expected)
54+
log.info(connection.getResponseCode() + " " + connection.getResponseMessage());
55+
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
56+
String current;
57+
58+
while ((current = in.readLine()) != null) {
59+
urlStringCont += current;
60+
}
61+
return urlStringCont;
62+
}
63+
64+
public static void main(String[] args) throws Exception {
65+
URLDemo demo = new URLDemo();
66+
String content = demo.testURL(demo.URLCOMPOUND);
67+
demo.log.info(content);
68+
}
69+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.maths;
2+
3+
import java.math.BigDecimal;
4+
5+
public class FloatingPointArithmetic {
6+
public static void main(String[] args) {
7+
8+
double a = 13.22;
9+
double b = 4.88;
10+
double c = 21.45;
11+
12+
System.out.println("a = " + a);
13+
System.out.println("b = " + b);
14+
System.out.println("c = " + c);
15+
16+
double sum_ab = a + b;
17+
System.out.println("a + b = " + sum_ab);
18+
19+
double abc = a + b + c;
20+
System.out.println("a + b + c = " + abc);
21+
22+
double ab_c = sum_ab + c;
23+
System.out.println("ab + c = " + ab_c);
24+
25+
double sum_ac = a + c;
26+
System.out.println("a + c = " + sum_ac);
27+
28+
double acb = a + c + b;
29+
System.out.println("a + c + b = " + acb);
30+
31+
double ac_b = sum_ac + b;
32+
System.out.println("ac + b = " + ac_b);
33+
34+
double ab = 18.1;
35+
double ac = 34.67;
36+
double sum_ab_c = ab + c;
37+
double sum_ac_b = ac + b;
38+
System.out.println("ab + c = " + sum_ab_c);
39+
System.out.println("ac + b = " + sum_ac_b);
40+
41+
BigDecimal d = new BigDecimal(String.valueOf(a));
42+
BigDecimal e = new BigDecimal(String.valueOf(b));
43+
BigDecimal f = new BigDecimal(String.valueOf(c));
44+
45+
BigDecimal def = d.add(e).add(f);
46+
BigDecimal dfe = d.add(f).add(e);
47+
48+
System.out.println("d + e + f = " + def);
49+
System.out.println("d + f + e = " + dfe);
50+
}
51+
}

‎core-java/src/main/java/com/baeldung/string/StringToCharStream.java‎

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Set root logger level to DEBUG and its only appender to A1.
2+
log4j.rootLogger=DEBUG, A1
3+
4+
# A1 is set to be a ConsoleAppender.
5+
log4j.appender.A1=org.apache.log4j.ConsoleAppender
6+
7+
# A1 uses PatternLayout.
8+
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
9+
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.baeldung.java.currentmethod;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.assertEquals;
6+
7+
/**
8+
* The class presents various ways of finding the name of currently executed method.
9+
*/
10+
public class CurrentlyExecutedMethodFinderTest {
11+
12+
@Test
13+
public void givenCurrentThread_whenGetStackTrace_thenFindMethod() {
14+
final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
15+
assertEquals("givenCurrentThread_whenGetStackTrace_thenFindMethod", stackTrace[1].getMethodName());
16+
}
17+
18+
@Test
19+
public void givenException_whenGetStackTrace_thenFindMethod() {
20+
String methodName = new Exception().getStackTrace()[0].getMethodName();
21+
assertEquals("givenException_whenGetStackTrace_thenFindMethod", methodName);
22+
}
23+
24+
@Test
25+
public void givenThrowable_whenGetStacktrace_thenFindMethod() {
26+
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
27+
assertEquals("givenThrowable_whenGetStacktrace_thenFindMethod", stackTrace[0].getMethodName());
28+
}
29+
30+
@Test
31+
public void givenObject_whenGetEnclosingMethod_thenFindMethod() {
32+
String methodName = new Object() {}.getClass().getEnclosingMethod().getName();
33+
assertEquals("givenObject_whenGetEnclosingMethod_thenFindMethod", methodName);
34+
}
35+
36+
@Test
37+
public void givenLocal_whenGetEnclosingMethod_thenFindMethod() {
38+
class Local {};
39+
String methodName = Local.class.getEnclosingMethod().getName();
40+
assertEquals("givenLocal_whenGetEnclosingMethod_thenFindMethod", methodName);
41+
}
42+
43+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.baeldung.javanetworking.url.test;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNotNull;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import java.io.BufferedReader;
8+
import java.io.IOException;
9+
import java.io.InputStreamReader;
10+
import java.net.HttpURLConnection;
11+
import java.net.MalformedURLException;
12+
import java.net.URL;
13+
import java.net.URLConnection;
14+
15+
import org.junit.BeforeClass;
16+
import org.junit.FixMethodOrder;
17+
import org.junit.Test;
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
21+
import com.baeldung.javanetworking.url.URLDemo;
22+
23+
@FixMethodOrder
24+
public class URLDemoTest {
25+
private final Logger log = LoggerFactory.getLogger(URLDemo.class);
26+
static String URLSTRING = "https://wordpress.org:443/support/topic/page-jumps-within-wordpress/?replies=3#post-2278484";
27+
// parsed locator
28+
static String URLPROTOCOL = "https";
29+
String URLAUTHORITY = "wordpress.org:443";
30+
static String URLHOST = "wordpress.org";
31+
static String URLPATH = "/support/topic/page-jumps-within-wordpress/";
32+
String URLFILENAME = "/support/topic/page-jumps-within-wordpress/?replies=3";
33+
int URLPORT = 443;
34+
static int URLDEFAULTPORT = 443;
35+
static String URLQUERY = "replies=3";
36+
static String URLREFERENCE = "post-2278484";
37+
static String URLCOMPOUND = URLPROTOCOL + "://" + URLHOST + ":" + URLDEFAULTPORT + URLPATH + "?" + URLQUERY + "#" + URLREFERENCE;
38+
39+
static URL url;
40+
URLConnection urlConnection = null;
41+
HttpURLConnection connection = null;
42+
BufferedReader in = null;
43+
String urlContent = "";
44+
45+
@BeforeClass
46+
public static void givenEmplyURL_whenInitializeURL_thenSuccess() throws MalformedURLException {
47+
url = new URL(URLCOMPOUND);
48+
}
49+
50+
// check parsed URL
51+
@Test
52+
public void givenURL_whenURLIsParsed_thenSuccess() {
53+
assertNotNull("URL is null", url);
54+
assertEquals("URL string is not equal", url.toString(), URLSTRING);
55+
assertEquals("Protocol is not equal", url.getProtocol(), URLPROTOCOL);
56+
assertEquals("Authority is not equal", url.getAuthority(), URLAUTHORITY);
57+
assertEquals("Host string is not equal", url.getHost(), URLHOST);
58+
assertEquals("Path string is not equal", url.getPath(), URLPATH);
59+
assertEquals("File string is not equal", url.getFile(), URLFILENAME);
60+
assertEquals("Port number is not equal", url.getPort(), URLPORT);
61+
assertEquals("Default port number is not equal", url.getDefaultPort(), URLDEFAULTPORT);
62+
assertEquals("Query string is not equal", url.getQuery(), URLQUERY);
63+
assertEquals("Reference string is not equal", url.getRef(), URLREFERENCE);
64+
}
65+
66+
// Obtain the content from location
67+
@Test
68+
public void givenURL_whenOpenConnectionAndContentIsNotEmpty_thenSuccess() throws IOException {
69+
try {
70+
urlConnection = url.openConnection();
71+
} catch (IOException ex) {
72+
urlConnection = null;
73+
ex.printStackTrace();
74+
}
75+
assertNotNull("URL Connection is null", urlConnection);
76+
77+
connection = null;
78+
assertTrue("URLConnection is not HttpURLConnection", urlConnection instanceof HttpURLConnection);
79+
if (urlConnection instanceof HttpURLConnection) {
80+
connection = (HttpURLConnection) urlConnection;
81+
}
82+
assertNotNull("Connection is null", connection);
83+
84+
log.info(connection.getResponseCode() + " " + connection.getResponseMessage());
85+
86+
try {
87+
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
88+
} catch (IOException ex) {
89+
in = null;
90+
ex.printStackTrace();
91+
}
92+
assertNotNull("Input stream failed", in);
93+
94+
String current;
95+
try {
96+
while ((current = in.readLine()) != null) {
97+
urlContent += current;
98+
}
99+
} catch (IOException ex) {
100+
urlContent = null;
101+
ex.printStackTrace();
102+
}
103+
assertNotNull("Content is null", urlContent);
104+
assertTrue("Content is empty", urlContent.length() > 0);
105+
}
106+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.baeldung.maths;
2+
3+
import java.math.BigDecimal;
4+
5+
import org.junit.Assert;
6+
import org.junit.Test;
7+
8+
public class FloatingPointArithmeticTest {
9+
10+
@Test
11+
public void givenDecimalNumbers_whenAddedTogether_thenGetExpectedResult() {
12+
double a = 13.22;
13+
double b = 4.88;
14+
double c = 21.45;
15+
double result = 39.55;
16+
17+
double abc = a + b + c;
18+
double acb = a + c + b;
19+
20+
Assert.assertEquals(result, abc, 0);
21+
Assert.assertNotEquals(result, acb, 0);
22+
23+
double ab = 18.1;
24+
double ac = 34.67;
25+
26+
double ab_c = ab + c;
27+
double ac_b = ac + b;
28+
29+
Assert.assertEquals(result, ab_c, 0);
30+
Assert.assertNotEquals(result, ac_b, 0);
31+
32+
BigDecimal d = new BigDecimal(String.valueOf(a));
33+
BigDecimal e = new BigDecimal(String.valueOf(b));
34+
BigDecimal f = new BigDecimal(String.valueOf(c));
35+
BigDecimal sum = new BigDecimal("39.55");
36+
37+
BigDecimal def = d.add(e).add(f);
38+
BigDecimal dfe = d.add(f).add(e);
39+
40+
Assert.assertEquals(0, def.compareTo(sum));
41+
Assert.assertEquals(0, dfe.compareTo(sum));
42+
43+
Assert.assertNotEquals(0, sum.compareTo(new BigDecimal(String.valueOf(acb))));
44+
}
45+
}

0 commit comments

Comments
 (0)