Merge branch 'master' of https://github.com/eugenp/tutorials into task/BAEL-17684_bc

This commit is contained in:
Dhawal Kapil
2019-10-14 20:53:44 +05:30
165 changed files with 1164 additions and 415 deletions

View File

@@ -3,22 +3,14 @@
This module contains articles about core features in the Java language
### Relevant Articles:
- [Generate equals() and hashCode() with Eclipse](https://www.baeldung.com/java-eclipse-equals-and-hashcode)
- [Iterating Over Enum Values in Java](https://www.baeldung.com/java-enum-iteration)
- [Comparator and Comparable in Java](https://www.baeldung.com/java-comparator-comparable)
- [Nested Classes in Java](https://www.baeldung.com/java-nested-classes)
- [A Guide to Inner Interfaces in Java](https://www.baeldung.com/java-inner-interfaces)
- [Recursion In Java](https://www.baeldung.com/java-recursion)
- [A Guide to the finalize Method in Java](https://www.baeldung.com/java-finalize)
- [Quick Guide to java.lang.System](https://www.baeldung.com/java-lang-system)
- [Using Java Assertions](https://www.baeldung.com/java-assert)
- [Static and Dynamic Binding in Java](https://www.baeldung.com/java-static-dynamic-binding)
- [Synthetic Constructs in Java](https://www.baeldung.com/java-synthetic)
- [How to Separate Double into Integer and Decimal Parts](https://www.baeldung.com/java-separate-double-into-integer-decimal-parts)
- [Retrieving a Class Name in Java](https://www.baeldung.com/java-class-name)
- [Java Interfaces](https://www.baeldung.com/java-interfaces)
- [Attaching Values to Java Enum](https://www.baeldung.com/java-enum-values)
- [Java Classes and Objects](https://www.baeldung.com/java-classes-objects)
- [A Guide to Java Enums](https://www.baeldung.com/a-guide-to-java-enums)
- [[More --> ]](/core-java-modules/core-java-lang-2)

View File

@@ -1,23 +0,0 @@
package com.baeldung.binding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by madhumita.g on 25-07-2018.
*/
public class Animal {
final static Logger logger = LoggerFactory.getLogger(Animal.class);
public void makeNoise() {
logger.info("generic animal noise");
}
public void makeNoise(Integer repetitions) {
while(repetitions != 0) {
logger.info("generic animal noise countdown " + repetitions);
repetitions -= 1;
}
}
}

View File

@@ -1,43 +0,0 @@
package com.baeldung.binding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by madhumita.g on 25-07-2018.
*/
public class AnimalActivity {
final static Logger logger = LoggerFactory.getLogger(AnimalActivity.class);
public static void sleep(Animal animal) {
logger.info("Animal is sleeping");
}
public static void sleep(Cat cat) {
logger.info("Cat is sleeping");
}
public static void main(String[] args) {
Animal animal = new Animal();
//calling methods of animal object
animal.makeNoise();
animal.makeNoise(3);
//assigning a dog object to reference of type Animal
Animal catAnimal = new Cat();
catAnimal.makeNoise();
// calling static function
AnimalActivity.sleep(catAnimal);
return;
}
}

View File

@@ -1,18 +0,0 @@
package com.baeldung.binding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Created by madhumita.g on 25-07-2018.
*/
public class Cat extends Animal {
final static Logger logger = LoggerFactory.getLogger(Cat.class);
public void makeNoise() {
logger.info("meow");
}
}

View File

@@ -1,40 +0,0 @@
package com.baeldung.doubles;
import java.math.BigDecimal;
public class SplitFloatingPointNumbers {
public static void main(String[] args) {
double doubleNumber = 24.04;
splitUsingFloatingTypes(doubleNumber);
splitUsingString(doubleNumber);
splitUsingBigDecimal(doubleNumber);
}
private static void splitUsingFloatingTypes(double doubleNumber) {
System.out.println("Using Floating Point Arithmetics:");
int intPart = (int) doubleNumber;
System.out.println("Double Number: "+doubleNumber);
System.out.println("Integer Part: "+ intPart);
System.out.println("Decimal Part: "+ (doubleNumber - intPart));
}
private static void splitUsingString(double doubleNumber) {
System.out.println("Using String Operations:");
String doubleAsString = String.valueOf(doubleNumber);
int indexOfDecimal = doubleAsString.indexOf(".");
System.out.println("Double Number: "+doubleNumber);
System.out.println("Integer Part: "+ doubleAsString.substring(0, indexOfDecimal));
System.out.println("Decimal Part: "+ doubleAsString.substring(indexOfDecimal));
}
private static void splitUsingBigDecimal(double doubleNumber) {
System.out.println("Using BigDecimal Operations:");
BigDecimal bigDecimal = new BigDecimal(String.valueOf(doubleNumber));
int intValue = bigDecimal.intValue();
System.out.println("Double Number: "+bigDecimal.toPlainString());
System.out.println("Integer Part: "+intValue);
System.out.println("Decimal Part: "+bigDecimal.subtract(new BigDecimal(intValue)).toPlainString());
}
}

View File

@@ -1,5 +0,0 @@
package com.baeldung.interfaces;
public interface Box extends HasColor {
int getHeight();
}

View File

@@ -1,21 +0,0 @@
package com.baeldung.interfaces;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class CommaSeparatedCustomers implements Customer.List {
private List<Customer> customers = new ArrayList<Customer>();
@Override
public void Add(Customer customer) {
customers.add(customer);
}
@Override
public String getCustomerNames() {
return customers.stream().map(customer -> customer.getName()).collect(Collectors.joining(","));
}
}

View File

@@ -1,19 +0,0 @@
package com.baeldung.interfaces;
public class Customer {
public interface List {
void Add(Customer customer);
String getCustomerNames();
}
private String name;
public Customer(String name) {
this.name = name;
}
String getName() {
return name;
}
}

View File

@@ -1,14 +0,0 @@
package com.baeldung.interfaces;
public class Employee {
private double salary;
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}

View File

@@ -1,17 +0,0 @@
package com.baeldung.interfaces;
import java.util.Comparator;
public class EmployeeSalaryComparator implements Comparator<Employee> {
@Override
public int compare(Employee employeeA, Employee employeeB) {
if (employeeA.getSalary() < employeeB.getSalary()) {
return -1;
} else if (employeeA.getSalary() > employeeB.getSalary()) {
return 1;
} else {
return 0;
}
}
}

View File

@@ -1,4 +0,0 @@
package com.baeldung.interfaces;
public interface HasColor {
}

View File

@@ -1,14 +0,0 @@
package com.baeldung.interfaces.multiinheritance;
public class Car implements Fly,Transform {
@Override
public void fly() {
System.out.println("I can Fly!!");
}
@Override
public void transform() {
System.out.println("I can Transform!!");
}
}

View File

@@ -1,6 +0,0 @@
package com.baeldung.interfaces.multiinheritance;
public interface Fly {
void fly();
}

View File

@@ -1,10 +0,0 @@
package com.baeldung.interfaces.multiinheritance;
public interface Transform {
void transform();
default void printSpecs(){
System.out.println("Transform Specification");
}
}

View File

@@ -1,4 +0,0 @@
package com.baeldung.interfaces.multiinheritance;
public abstract class Vehicle implements Transform {
}

View File

@@ -1,9 +0,0 @@
package com.baeldung.interfaces.polymorphysim;
public class Circle implements Shape {
@Override
public String name() {
return "Circle";
}
}

View File

@@ -1,20 +0,0 @@
package com.baeldung.interfaces.polymorphysim;
import java.util.ArrayList;
import java.util.List;
public class MainTestClass {
public static void main(String[] args) {
List<Shape> shapes = new ArrayList<>();
Shape circleShape = new Circle();
Shape squareShape = new Square();
shapes.add(circleShape);
shapes.add(squareShape);
for (Shape shape : shapes) {
System.out.println(shape.name());
}
}
}

View File

@@ -1,6 +0,0 @@
package com.baeldung.interfaces.polymorphysim;
public interface Shape {
String name();
}

View File

@@ -1,9 +0,0 @@
package com.baeldung.interfaces.polymorphysim;
public class Square implements Shape {
@Override
public String name() {
return "Square";
}
}

View File

@@ -1,51 +0,0 @@
package com.baeldung.objects;
public class Car {
private String type;
private String model;
private String color;
private int speed;
public Car(String type, String model, String color) {
this.type = type;
this.model = model;
this.color = color;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getSpeed() {
return speed;
}
public int increaseSpeed(int increment) {
if (increment > 0) {
this.speed += increment;
} else {
System.out.println("Increment can't be negative.");
}
return this.speed;
}
public int decreaseSpeed(int decrement) {
if (decrement > 0 && decrement <= this.speed) {
this.speed -= decrement;
} else {
System.out.println("Decrement can't be negative or greater than current speed.");
}
return this.speed;
}
@Override
public String toString() {
return "Car [type=" + type + ", model=" + model + ", color=" + color + ", speed=" + speed + "]";
}
}

View File

@@ -1,27 +0,0 @@
package com.baeldung.parameterpassing;
public class NonPrimitives {
public static void main(String[] args) {
FooClass a = new FooClass(1);
FooClass b = new FooClass(1);
System.out.printf("Before Modification: a = %d and b = %d ", a.num, b.num);
modify(a, b);
System.out.printf("\nAfter Modification: a = %d and b = %d ", a.num, b.num);
}
public static void modify(FooClass a1, FooClass b1) {
a1.num++;
b1 = new FooClass(1);
b1.num++;
}
}
class FooClass {
public int num;
public FooClass(int num) {
this.num = num;
}
}

View File

@@ -1,17 +0,0 @@
package com.baeldung.parameterpassing;
public class Primitives {
public static void main(String[] args) {
int x = 1;
int y = 2;
System.out.printf("Before Modification: x = %d and y = %d ", x, y);
modify(x, y);
System.out.printf("\nAfter Modification: x = %d and y = %d ", x, y);
}
public static void modify(int x1, int y1) {
x1 = 5;
y1 = 10;
}
}

View File

@@ -1,95 +0,0 @@
package com.baeldung.binding;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.Appender;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.LoggerFactory;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
/**
*https://gist.github.com/bloodredsun/a041de13e57bf3c6c040
*/
@RunWith(MockitoJUnitRunner.class)
public class AnimalActivityUnitTest {
@Mock
private Appender mockAppender;
@Captor
private ArgumentCaptor<LoggingEvent> captorLoggingEvent;
@Before
public void setup() {
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
logger.addAppender(mockAppender);
}
@After
public void teardown() {
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
logger.detachAppender(mockAppender);
}
@Test
public void givenAnimalReference__whenRefersAnimalObject_shouldCallFunctionWithAnimalParam() {
Animal animal = new Animal();
AnimalActivity.sleep(animal);
verify(mockAppender).doAppend(captorLoggingEvent.capture());
final LoggingEvent loggingEvent = captorLoggingEvent.getValue();
assertThat(loggingEvent.getLevel(), is(Level.INFO));
assertThat(loggingEvent.getFormattedMessage(),
is("Animal is sleeping"));
}
@Test
public void givenDogReference__whenRefersCatObject_shouldCallFunctionWithAnimalParam() {
Cat cat = new Cat();
AnimalActivity.sleep(cat);
verify(mockAppender).doAppend(captorLoggingEvent.capture());
final LoggingEvent loggingEvent = captorLoggingEvent.getValue();
assertThat(loggingEvent.getLevel(), is(Level.INFO));
assertThat(loggingEvent.getFormattedMessage(),
is("Cat is sleeping"));
}
@Test
public void givenAnimaReference__whenRefersDogObject_shouldCallFunctionWithAnimalParam() {
Animal cat = new Cat();
AnimalActivity.sleep(cat);
verify(mockAppender).doAppend(captorLoggingEvent.capture());
final LoggingEvent loggingEvent = captorLoggingEvent.getValue();
assertThat(loggingEvent.getLevel(), is(Level.INFO));
assertThat(loggingEvent.getFormattedMessage(),
is("Animal is sleeping"));
}
}

View File

@@ -1,87 +0,0 @@
package com.baeldung.binding;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.Appender;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.LoggerFactory;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.util.List;
/**
* Created by madhumita.g on 01-08-2018.
*/
@RunWith(MockitoJUnitRunner.class)
public class AnimalUnitTest {
@Mock
private Appender mockAppender;
@Captor
private ArgumentCaptor<LoggingEvent> captorLoggingEvent;
@Before
public void setup() {
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
logger.addAppender(mockAppender);
}
@After
public void teardown() {
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
logger.detachAppender(mockAppender);
}
@Test
public void whenCalledWithoutParameters_shouldCallFunctionMakeNoiseWithoutParameters() {
Animal animal = new Animal();
animal.makeNoise();
verify(mockAppender).doAppend(captorLoggingEvent.capture());
final LoggingEvent loggingEvent = captorLoggingEvent.getValue();
assertThat(loggingEvent.getLevel(), is(Level.INFO));
assertThat(loggingEvent.getFormattedMessage(),
is("generic animal noise"));
}
@Test
public void whenCalledWithParameters_shouldCallFunctionMakeNoiseWithParameters() {
Animal animal = new Animal();
int testValue = 3;
animal.makeNoise(testValue);
verify(mockAppender,times(3)).doAppend(captorLoggingEvent.capture());
final List<LoggingEvent> loggingEvents = captorLoggingEvent.getAllValues();
for(LoggingEvent loggingEvent : loggingEvents)
{
assertThat(loggingEvent.getLevel(), is(Level.INFO));
assertThat(loggingEvent.getFormattedMessage(),
is("generic animal noise countdown "+testValue));
testValue--;
}
}
}

View File

@@ -1,62 +0,0 @@
package com.baeldung.binding;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.Appender;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.LoggerFactory;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.verify;
/**
* Created by madhumita.g on 01-08-2018.
*/
@RunWith(MockitoJUnitRunner.class)
public class CatUnitTest {
@Mock
private Appender mockAppender;
@Captor
private ArgumentCaptor<LoggingEvent> captorLoggingEvent;
@Before
public void setup() {
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
logger.addAppender(mockAppender);
}
@After
public void teardown() {
final Logger logger = (Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
logger.detachAppender(mockAppender);
}
@Test
public void makeNoiseTest() {
Cat cat = new Cat();
cat.makeNoise();
verify(mockAppender).doAppend(captorLoggingEvent.capture());
final LoggingEvent loggingEvent = captorLoggingEvent.getValue();
assertThat(loggingEvent.getLevel(), is(Level.INFO));
assertThat(loggingEvent.getFormattedMessage(),
is("meow"));
}
}

View File

@@ -1,4 +1,4 @@
package com.baeldung.java.enumiteration;
package com.baeldung.enums.iteration;
import java.util.stream.Stream;

View File

@@ -1,4 +1,4 @@
package com.baeldung.java.enumiteration;
package com.baeldung.enums.iteration;
import java.util.ArrayList;
import java.util.Arrays;

View File

@@ -1,18 +0,0 @@
package com.baeldung.interfaces;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class InnerInterfaceUnitTest {
@Test
public void whenCustomerListJoined_thenReturnsJoinedNames() {
Customer.List customerList = new CommaSeparatedCustomers();
customerList.Add(new Customer("customer1"));
customerList.Add(new Customer("customer2"));
assertEquals("customer1,customer2", customerList.getCustomerNames());
}
}

View File

@@ -1,20 +0,0 @@
package com.baeldung.nestedclass;
import org.junit.Test;
abstract class SimpleAbstractClass {
abstract void run();
}
public class AnonymousInner {
@Test
public void run() {
SimpleAbstractClass simpleAbstractClass = new SimpleAbstractClass() {
void run() {
System.out.println("Running Anonymous Class...");
}
};
simpleAbstractClass.run();
}
}

View File

@@ -1,21 +0,0 @@
package com.baeldung.nestedclass;
import org.junit.Test;
public class Enclosing {
private static int x = 1;
public static class StaticNested {
private void run() {
System.out.println("x = " + x);
}
}
@Test
public void test() {
Enclosing.StaticNested nested = new Enclosing.StaticNested();
nested.run();
}
}

View File

@@ -1,22 +0,0 @@
package com.baeldung.nestedclass;
import org.junit.Test;
public class NewEnclosing {
private void run() {
class Local {
void run() {
System.out.println("Welcome to Baeldung!");
}
}
Local local = new Local();
local.run();
}
@Test
public void test() {
NewEnclosing newEnclosing = new NewEnclosing();
newEnclosing.run();
}
}

View File

@@ -1,30 +0,0 @@
package com.baeldung.nestedclass;
import org.junit.Test;
public class NewOuter {
int a = 1;
static int b = 2;
public class InnerClass {
int a = 3;
static final int b = 4;
public void run() {
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("NewOuter.this.a = " + NewOuter.this.a);
System.out.println("NewOuter.b = " + NewOuter.b);
System.out.println("NewOuter.this.b = " + NewOuter.this.b);
}
}
@Test
public void test() {
NewOuter outer = new NewOuter();
NewOuter.InnerClass inner = outer.new InnerClass();
inner.run();
}
}

View File

@@ -1,20 +0,0 @@
package com.baeldung.nestedclass;
import org.junit.Test;
public class Outer {
public class Inner {
public void run() {
System.out.println("Calling test...");
}
}
@Test
public void test() {
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.run();
}
}

View File

@@ -1,38 +0,0 @@
package com.baeldung.objects;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class CarUnitTest {
private Car car;
@Before
public void setUp() throws Exception {
car = new Car("Ford", "Focus", "red");
}
@Test
public final void when_speedIncreased_then_verifySpeed() {
car.increaseSpeed(30);
assertEquals(30, car.getSpeed());
car.increaseSpeed(20);
assertEquals(50, car.getSpeed());
}
@Test
public final void when_speedDecreased_then_verifySpeed() {
car.increaseSpeed(50);
assertEquals(50, car.getSpeed());
car.decreaseSpeed(30);
assertEquals(20, car.getSpeed());
car.decreaseSpeed(20);
assertEquals(0, car.getSpeed());
}
}