merged PR locally

This commit is contained in:
Seun Matt
2017-06-16 11:26:52 +01:00
35 changed files with 300 additions and 54 deletions
+18 -1
View File
@@ -113,4 +113,21 @@
- [Difference Between Wait and Sleep in Java](http://www.baeldung.com/java-wait-and-sleep)
- [LongAdder and LongAccumulator in Java](http://www.baeldung.com/java-longadder-and-longaccumulator)
- [Using Java MappedByteBuffer](http://www.baeldung.com/java-mapped-byte-buffer)
- [Dynamic Proxies in Java](http://www.baeldung.com/java-dynamic-proxies)
- [How to Copy an Array in Java](http://www.baeldung.com/java-array-copy)
- [Introduction to JDBC](http://www.baeldung.com/java-jdbc)
- [Guide to CopyOnWriteArrayList](http://www.baeldung.com/java-copy-on-write-arraylist)
- [Period and Duration in Java](http://www.baeldung.com/java-period-duration)
- [Converting a Stack Trace to a String in Java](http://www.baeldung.com/java-stacktrace-to-string)
- [Guide to the Java Phaser](http://www.baeldung.com/java-phaser)
- [Count Occurrences of a Char in a String](http://www.baeldung.com/java-count-chars)
- [Java Double Brace Initialization](http://www.baeldung.com/java-double-brace-initialization)
- [The StackOverflowError in Java](http://www.baeldung.com/java-stack-overflow-error)
- [Split a String in Java](http://www.baeldung.com/java-split-string)
- [Introduction to Java Serialization](http://www.baeldung.com/java-serialization)
- [How to Remove the Last Character of a String?](http://www.baeldung.com/java-remove-last-character-of-string)
- [Guide to Synchronized Keyword in Java](http://www.baeldung.com/java-synchronized)
- [ClassNotFoundException vs NoClassDefFoundError](http://www.baeldung.com/java-classnotfoundexception-and-noclassdeffounderror)
- [Guide to UUID in Java](http://www.baeldung.com/java-uuid)
- [How to Get the Last Element of a Stream in Java?](http://www.baeldung.com/java-stream-last-element)
- [Guide to Escaping Characters in Java RegExps](http://www.baeldung.com/java-regexp-escape-char)
@@ -0,0 +1,51 @@
package com.baeldung.maths;
import java.math.BigDecimal;
public class FloatingPointArithmetic {
public static void main(String[] args) {
double a = 13.22;
double b = 4.88;
double c = 21.45;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
double sum_ab = a + b;
System.out.println("a + b = " + sum_ab);
double abc = a + b + c;
System.out.println("a + b + c = " + abc);
double ab_c = sum_ab + c;
System.out.println("ab + c = " + ab_c);
double sum_ac = a + c;
System.out.println("a + c = " + sum_ac);
double acb = a + c + b;
System.out.println("a + c + b = " + acb);
double ac_b = sum_ac + b;
System.out.println("ac + b = " + ac_b);
double ab = 18.1;
double ac = 34.67;
double sum_ab_c = ab + c;
double sum_ac_b = ac + b;
System.out.println("ab + c = " + sum_ab_c);
System.out.println("ac + b = " + sum_ac_b);
BigDecimal d = new BigDecimal(String.valueOf(a));
BigDecimal e = new BigDecimal(String.valueOf(b));
BigDecimal f = new BigDecimal(String.valueOf(c));
BigDecimal def = d.add(e).add(f);
BigDecimal dfe = d.add(f).add(e);
System.out.println("d + e + f = " + def);
System.out.println("d + f + e = " + dfe);
}
}
@@ -0,0 +1,43 @@
package com.baeldung.java.currentmethod;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* The class presents various ways of finding the name of currently executed method.
*/
public class CurrentlyExecutedMethodFinderTest {
@Test
public void givenCurrentThread_whenGetStackTrace_thenFindMethod() {
final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
assertEquals("givenCurrentThread_whenGetStackTrace_thenFindMethod", stackTrace[1].getMethodName());
}
@Test
public void givenException_whenGetStackTrace_thenFindMethod() {
String methodName = new Exception().getStackTrace()[0].getMethodName();
assertEquals("givenException_whenGetStackTrace_thenFindMethod", methodName);
}
@Test
public void givenThrowable_whenGetStacktrace_thenFindMethod() {
StackTraceElement[] stackTrace = new Throwable().getStackTrace();
assertEquals("givenThrowable_whenGetStacktrace_thenFindMethod", stackTrace[0].getMethodName());
}
@Test
public void givenObject_whenGetEnclosingMethod_thenFindMethod() {
String methodName = new Object() {}.getClass().getEnclosingMethod().getName();
assertEquals("givenObject_whenGetEnclosingMethod_thenFindMethod", methodName);
}
@Test
public void givenLocal_whenGetEnclosingMethod_thenFindMethod() {
class Local {};
String methodName = Local.class.getEnclosingMethod().getName();
assertEquals("givenLocal_whenGetEnclosingMethod_thenFindMethod", methodName);
}
}
@@ -0,0 +1,45 @@
package com.baeldung.maths;
import java.math.BigDecimal;
import org.junit.Assert;
import org.junit.Test;
public class FloatingPointArithmeticTest {
@Test
public void givenDecimalNumbers_whenAddedTogether_thenGetExpectedResult() {
double a = 13.22;
double b = 4.88;
double c = 21.45;
double result = 39.55;
double abc = a + b + c;
double acb = a + c + b;
Assert.assertEquals(result, abc, 0);
Assert.assertNotEquals(result, acb, 0);
double ab = 18.1;
double ac = 34.67;
double ab_c = ab + c;
double ac_b = ac + b;
Assert.assertEquals(result, ab_c, 0);
Assert.assertNotEquals(result, ac_b, 0);
BigDecimal d = new BigDecimal(String.valueOf(a));
BigDecimal e = new BigDecimal(String.valueOf(b));
BigDecimal f = new BigDecimal(String.valueOf(c));
BigDecimal sum = new BigDecimal("39.55");
BigDecimal def = d.add(e).add(f);
BigDecimal dfe = d.add(f).add(e);
Assert.assertEquals(0, def.compareTo(sum));
Assert.assertEquals(0, dfe.compareTo(sum));
Assert.assertNotEquals(0, sum.compareTo(new BigDecimal(String.valueOf(acb))));
}
}
@@ -2,41 +2,58 @@ package com.baeldung.string;
import org.junit.Test;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
public class StringToCharStreamUnitTest {
private String testString = "Tests";
@Test
public void givenTestString_whenChars_thenReturnIntStream() {
assertThat(StringToCharStream.getIntStreamFromChars("test"), instanceOf(IntStream.class));
assertThat(testString.chars(), instanceOf(IntStream.class));
}
@Test
public void givenTestString_whenCodePoints_thenReturnIntStream() {
assertThat(StringToCharStream.getIntStreamFromCodePoints("test"), instanceOf(IntStream.class));
assertThat(testString.codePoints(), instanceOf(IntStream.class));
}
@Test
public void givenTestString_whenCodePoints_thenShowOccurences() throws Exception {
Map<Character, Integer> map = testString.codePoints()
.mapToObj(c -> (char) c)
.filter(Character::isLetter)
.collect(Collectors.toMap(c -> c, c -> 1, Integer::sum));
System.out.println(map);
}
@Test
public void givenIntStream_whenMapToObj_thenReturnCharacterStream() {
Stream<Character> characterStream
= StringToCharStream.mapIntStreamToCharStream(StringToCharStream.getIntStreamFromChars("test"));
Stream<Character> characterStream1
= StringToCharStream.mapIntStreamToCharStream(StringToCharStream.getIntStreamFromCodePoints("test"));
Stream<Character> characterStream = testString.chars()
.mapToObj(c -> (char) c);
Stream<Character> characterStream1 = testString.codePoints()
.mapToObj(c -> (char) c);
assertNotNull("IntStream returned by chars() did not map to Stream<Character>", characterStream);
assertNotNull("IntStream returned by codePoints() did not map to Stream<Character>", characterStream1);
}
@Test
public void givenIntStream_whenMapToObj_thenReturnStringStream() {
Stream<String> stringStream
= StringToCharStream.mapIntStreamToStringStream(StringToCharStream.getIntStreamFromCodePoints("test"));
assertNotNull(stringStream);
List<String> strings = testString.codePoints()
.mapToObj(c -> String.valueOf((char) c))
.collect(Collectors.toList());
assertEquals(strings.size(), 5);
}
}