Merge branch 'master' into BAEL-16646-2

This commit is contained in:
Alessio Stalla
2019-10-24 13:20:08 +02:00
parent db85c8f275
commit c499158763
20506 changed files with 1643665 additions and 0 deletions
@@ -0,0 +1,9 @@
## Relevant Articles:
- [Guide to the Diamond Operator in Java](https://www.baeldung.com/java-diamond-operator)
- [Ternary Operator In Java](https://www.baeldung.com/java-ternary-operator)
- [The Modulo Operator in Java](https://www.baeldung.com/modulo-java)
- [Java instanceof Operator](https://www.baeldung.com/java-instanceof)
- [A Guide to Increment and Decrement Unary Operators in Java](https://www.baeldung.com/java-unary-operators)
- [Java Compound Operators](https://www.baeldung.com/java-compound-operators)
- [The XOR Operator in Java](https://www.baeldung.com/java-xor-operator)
@@ -0,0 +1,42 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-lang-operators</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-lang-operators</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<!-- test scoped -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-lang-operators</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<!-- testing -->
<assertj-core.version>3.10.0</assertj-core.version>
</properties>
</project>
@@ -0,0 +1,36 @@
package com.baeldung.booleanoperators;
public class Car {
private boolean diesel;
private boolean manual;
public Car(boolean diesel, boolean manual) {
this.diesel = diesel;
this.manual = manual;
}
public boolean isDiesel() {
return diesel;
}
public boolean isManual() {
return manual;
}
static Car dieselAndManualCar() {
return new Car(true, true);
}
static Car dieselAndAutomaticCar() {
return new Car(true, false);
}
static Car oilAndManualCar() {
return new Car(false, true);
}
static Car oilAndAutomaticCar() {
return new Car(false, false);
}
}
@@ -0,0 +1,5 @@
package com.baeldung.keyword;
public class Circle extends Round implements Shape {
}
@@ -0,0 +1,4 @@
package com.baeldung.keyword;
public class Ring extends Round {
}
@@ -0,0 +1,4 @@
package com.baeldung.keyword;
public class Round {
}
@@ -0,0 +1,4 @@
package com.baeldung.keyword;
public interface Shape {
}
@@ -0,0 +1,4 @@
package com.baeldung.keyword;
public class Triangle implements Shape {
}
@@ -0,0 +1,69 @@
package com.baeldung.booleanoperators;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class XorUnitTest {
@Test
void givenDieselManualCar_whenXorOldSchool_thenFalse() {
Car car = Car.dieselAndManualCar();
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());
assertThat(dieselXorManual).isFalse();
}
@Test
void givenDieselAutomaticCar_whenXorOldSchool_thenTrue() {
Car car = Car.dieselAndAutomaticCar();
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());
assertThat(dieselXorManual).isTrue();
}
@Test
void givenNonDieselManualCar_whenXorOldSchool_thenTrue() {
Car car = Car.oilAndManualCar();
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());
assertThat(dieselXorManual).isTrue();
}
@Test
void givenNonDieselAutomaticCar_whenXorOldSchool_thenFalse() {
Car car = Car.oilAndAutomaticCar();
boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual());
assertThat(dieselXorManual).isFalse();
}
@Test
void givenDieselManualCar_whenXor_thenFalse() {
Car car = Car.dieselAndManualCar();
boolean dieselXorManual = car.isDiesel() ^ car.isManual();
assertThat(dieselXorManual).isFalse();
}
@Test
void givenDieselAutomaticCar_whenXor_thenTrue() {
Car car = Car.dieselAndAutomaticCar();
boolean dieselXorManual = car.isDiesel() ^ car.isManual();
assertThat(dieselXorManual).isTrue();
}
@Test
void givenNonDieselManualCar_whenXor_thenTrue() {
Car car = Car.oilAndManualCar();
boolean dieselXorManual = car.isDiesel() ^ car.isManual();
assertThat(dieselXorManual).isTrue();
}
@Test
void givenNonDieselAutomaticCar_whenXor_thenFalse() {
Car car = Car.oilAndAutomaticCar();
boolean dieselXorManual = car.isDiesel() ^ car.isManual();
assertThat(dieselXorManual).isFalse();
}
@Test
void givenNumbersOneAndThree_whenXor_thenTwo() {
assertThat(1 ^ 3).isEqualTo(2);
}
}
@@ -0,0 +1,111 @@
package com.baeldung.compoundoperators;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class CompoundOperatorsUnitTest {
@Test
public void whenAssignmentOperatorIsUsed_thenValueIsAssigned() {
int x = 5;
assertEquals(5, x);
}
@Test
public void whenCompoundAssignmentUsed_thenSameAsSimpleAssignment() {
int a = 3, b = 3, c = -2;
a = a * c; // Simple assignment operator
b *= c; // Compound assignment operator
assertEquals(a, b);
}
@Test
public void whenAssignmentOperatorIsUsed_thenValueIsReturned() {
long x = 1;
long y = (x+=2);
assertEquals(3, y);
assertEquals(y, x);
}
@Test
public void whenCompoundOperatorsAreUsed_thenOperationsArePerformedAndAssigned() {
//Simple assignment
int x = 5; //x is 5
//Incrementation
x += 5; //x is 10
assertEquals(10, x);
//Decrementation
x -= 2; //x is 8
assertEquals(8, x);
//Multiplication
x *= 2; //x is 16
assertEquals(16, x);
//Division
x /= 4; //x is 4
assertEquals(4, x);
//Modulus
x %= 3; //x is 1
assertEquals(1, x);
//Binary AND
x &= 4; //x is 0
assertEquals(0, x);
//Binary exclusive OR
x ^= 4; //x is 4
assertEquals(4, x);
//Binary inclusive OR
x |= 8; //x is 12
assertEquals(12, x);
//Binary Left Shift
x <<= 2; //x is 48
assertEquals(48, x);
//Binary Right Shift
x >>= 2; //x is 12
assertEquals(12, x);
//Shift right zero fill
x >>>= 1; //x is 6
assertEquals(6, x);
}
@Test(expected = NullPointerException.class)
public void whenArrayIsNull_thenThrowNullException() {
int[] numbers = null;
//Trying Incrementation
numbers[2] += 5;
}
@Test(expected = ArrayIndexOutOfBoundsException.class)
public void whenArrayIndexNotCorrect_thenThrowArrayIndexException() {
int[] numbers = {0, 1};
//Trying Incrementation
numbers[2] += 5;
}
@Test
public void whenArrayIndexIsCorrect_thenPerformOperation() {
int[] numbers = {0, 1};
//Incrementation
numbers[1] += 5;
assertEquals(6, numbers[1]);
}
}
@@ -0,0 +1,5 @@
package com.baeldung.diamondoperator;
public class Car<T extends Engine> implements Vehicle<T> {
}
@@ -0,0 +1,13 @@
package com.baeldung.diamondoperator;
import static org.junit.Assert.assertNotNull;
import org.junit.Test;
public class DiamondOperatorUnitTest {
@Test
public void whenCreateCarUsingDiamondOperator_thenSuccess() {
Car<Diesel> myCar = new Car<>();
assertNotNull(myCar);
}
}
@@ -0,0 +1,10 @@
package com.baeldung.diamondoperator;
public class Diesel implements Engine {
@Override
public void start() {
System.out.println("Started Diesel...");
}
}
@@ -0,0 +1,6 @@
package com.baeldung.diamondoperator;
public interface Engine {
void start();
}
@@ -0,0 +1,5 @@
package com.baeldung.diamondoperator;
public interface Vehicle<T extends Engine> {
}
@@ -0,0 +1,55 @@
package com.baeldung.keyword;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import com.baeldung.keyword.Circle;
import com.baeldung.keyword.Ring;
import com.baeldung.keyword.Round;
import com.baeldung.keyword.Shape;
import com.baeldung.keyword.Triangle;
public class InstanceOfUnitTest {
@Test
public void giveWhenInstanceIsCorrect_thenReturnTrue() {
Ring ring = new Ring();
Assert.assertTrue("ring is instance of Round ", ring instanceof Round);
}
@Test
public void giveWhenObjectIsInstanceOfType_thenReturnTrue() {
Circle circle = new Circle();
Assert.assertTrue("circle is instance of Circle ", circle instanceof Circle);
}
@Test
public void giveWhenInstanceIsOfSubtype_thenReturnTrue() {
Circle circle = new Circle();
Assert.assertTrue("circle is instance of Round", circle instanceof Round);
}
@Test
public void giveWhenTypeIsInterface_thenReturnTrue() {
Circle circle = new Circle();
Assert.assertTrue("circle is instance of Shape", circle instanceof Shape);
}
@Test
public void giveWhenTypeIsOfObjectType_thenReturnTrue() {
Thread thread = new Thread();
Assert.assertTrue("thread is instance of Object", thread instanceof Object);
}
@Test
public void giveWhenInstanceValueIsNull_thenReturnFalse() {
Circle circle = null;
Assert.assertFalse("circle is instance of Round", circle instanceof Round);
}
@Test
public void giveWhenComparingClassInDiffHierarchy_thenCompilationError() {
// Assert.assertFalse("circle is instance of Triangle", circle instanceof Triangle);
}
}
@@ -0,0 +1,54 @@
package com.baeldung.modulo;
import org.junit.Test;
import static org.assertj.core.api.Java6Assertions.*;
public class ModuloUnitTest {
@Test
public void whenIntegerDivision_thenLosesRemainder(){
assertThat(11 / 4).isEqualTo(2);
}
@Test
public void whenDoubleDivision_thenKeepsRemainder(){
assertThat(11 / 4.0).isEqualTo(2.75);
}
@Test
public void whenModulo_thenReturnsRemainder(){
assertThat(11 % 4).isEqualTo(3);
}
@Test(expected = ArithmeticException.class)
public void whenDivisionByZero_thenArithmeticException(){
double result = 1 / 0;
}
@Test(expected = ArithmeticException.class)
public void whenModuloByZero_thenArithmeticException(){
double result = 1 % 0;
}
@Test
public void whenDivisorIsOddAndModulusIs2_thenResultIs1(){
assertThat(3 % 2).isEqualTo(1);
}
@Test
public void whenDivisorIsEvenAndModulusIs2_thenResultIs0(){
assertThat(4 % 2).isEqualTo(0);
}
@Test
public void whenItemsIsAddedToCircularQueue_thenNoArrayIndexOutOfBounds(){
int QUEUE_CAPACITY= 10;
int[] circularQueue = new int[QUEUE_CAPACITY];
int itemsInserted = 0;
for (int value = 0; value < 1000; value++) {
int writeIndex = ++itemsInserted % QUEUE_CAPACITY;
circularQueue[writeIndex] = value;
}
}
}
@@ -0,0 +1,43 @@
package com.baeldung.ternaryoperator;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class TernaryOperatorUnitTest {
@Test
public void givenACondition_whenUsingTernaryOperator_thenItEvaluatesConditionAndReturnsAValue() {
int number = 10;
String msg = number > 10 ? "Number is greater than 10" : "Number is less than or equal to 10";
assertThat(msg).isEqualTo("Number is less than or equal to 10");
}
@Test
public void givenATrueCondition_whenUsingTernaryOperator_thenOnlyExpression1IsEvaluated() {
int exp1 = 0, exp2 = 0;
int result = 12 > 10 ? ++exp1 : ++exp2;
assertThat(exp1).isEqualTo(1);
assertThat(exp2).isEqualTo(0);
assertThat(result).isEqualTo(1);
}
@Test
public void givenAFalseCondition_whenUsingTernaryOperator_thenOnlyExpression2IsEvaluated() {
int exp1 = 0, exp2 = 0;
int result = 8 > 10 ? ++exp1 : ++exp2;
assertThat(exp1).isEqualTo(0);
assertThat(exp2).isEqualTo(1);
assertThat(result).isEqualTo(1);
}
@Test
public void givenANestedCondition_whenUsingTernaryOperator_thenCorrectValueIsReturned() {
int number = 6;
String msg = number > 10 ? "Number is greater than 10" : number > 5 ? "Number is greater than 5" : "Number is less than or equal to 5";
assertThat(msg).isEqualTo("Number is greater than 5");
}
}
@@ -0,0 +1,64 @@
package com.baeldung.unaryoperators;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class IncrementDecrementUnaryOperatorUnitTest {
@Test
public void givenAnOperand_whenUsingPreIncrementUnaryOperator_thenOperandIsIncrementedByOne() {
int operand = 1;
++operand;
assertThat(operand).isEqualTo(2);
}
@Test
public void givenANumber_whenUsingPreIncrementUnaryOperatorInEvaluation_thenNumberIsIncrementedByOne() {
int operand = 1;
int number = ++operand;
assertThat(number).isEqualTo(2);
}
@Test
public void givenAnOperand_whenUsingPreDecrementUnaryOperator_thenOperandIsDecrementedByOne() {
int operand = 1;
--operand;
assertThat(operand).isEqualTo(0);
}
@Test
public void givenANumber_whenUsingPreDecrementUnaryOperatorInEvaluation_thenNumberIsDecrementedByOne() {
int operand = 1;
int number = --operand;
assertThat(number).isEqualTo(0);
}
@Test
public void givenAnOperand_whenUsingPostIncrementUnaryOperator_thenOperandIsIncrementedByOne() {
int operand = 1;
operand++;
assertThat(operand).isEqualTo(2);
}
@Test
public void givenANumber_whenUsingPostIncrementUnaryOperatorInEvaluation_thenNumberIsSameAsOldValue() {
int operand = 1;
int number = operand++;
assertThat(number).isEqualTo(1);
}
@Test
public void givenAnOperand_whenUsingPostDecrementUnaryOperator_thenOperandIsDecrementedByOne() {
int operand = 1;
operand--;
assertThat(operand).isEqualTo(0);
}
@Test
public void givenANumber_whenUsingPostDecrementUnaryOperatorInEvaluation_thenNumberIsSameAsOldValue() {
int operand = 1;
int number = operand--;
assertThat(number).isEqualTo(1);
}
}