[JAVA-621] core-java-lang-oop-types module

* Creation

* Moved code from https://www.baeldung.com/java-marker-interfaces

* Moved code from https://www.baeldung.com/java-abstract-class

* Moved code from https://www.baeldung.com/java-this

* Moved code from https://www.baeldung.com/java-nested-classes

* Moved code from https://www.baeldung.com/java-inner-interfaces

* Moved code from https://www.baeldung.com/java-classes-objects

* Moved article references to the new README.md
This commit is contained in:
dupirefr
2020-04-04 10:18:15 +02:00
parent c182eaca8b
commit ea61137058
28 changed files with 50 additions and 7 deletions

View File

@@ -0,0 +1,11 @@
## Core Java Lang OOP - Types
This module contains articles about types in Java
### Relevant Articles:
- [Java Classes and Objects](https://www.baeldung.com/java-classes-objects)
- [Guide to the this Java Keyword](https://www.baeldung.com/java-this)
- [Abstract Classes in Java](https://www.baeldung.com/java-abstract-class)
- [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)
- [Marker Interfaces in Java](https://www.baeldung.com/java-marker-interfaces)

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>core-java-lang-oop-modules</artifactId>
<groupId>com.baeldung.core-java-lang-oop-modules</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-lang-oop-types</artifactId>
<name>core-java-lang-oop-types</name>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<assertj-core.version>3.10.0</assertj-core.version>
</properties>
</project>

View File

@@ -0,0 +1,29 @@
package com.baeldung.abstractclasses.application;
import com.baeldung.abstractclasses.filereaders.BaseFileReader;
import com.baeldung.abstractclasses.filereaders.LowercaseFileReader;
import com.baeldung.abstractclasses.filereaders.UppercaseFileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Application {
public static void main(String[] args) throws IOException, URISyntaxException {
Application application = new Application();
Path path = application.getPathFromResourcesFile("files/test.txt");
BaseFileReader lowercaseFileReader = new LowercaseFileReader(path);
lowercaseFileReader.readFile().forEach(line -> System.out.println(line));
BaseFileReader uppercaseFileReader = new UppercaseFileReader(path);
uppercaseFileReader.readFile().forEach(line -> System.out.println(line));
}
private Path getPathFromResourcesFile(String filePath) throws URISyntaxException {
return Paths.get(getClass().getClassLoader().getResource(filePath).toURI());
}
}

View File

@@ -0,0 +1,27 @@
package com.baeldung.abstractclasses.filereaders;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
public abstract class BaseFileReader {
protected Path filePath;
protected BaseFileReader(Path filePath) {
this.filePath = filePath;
}
public Path getFilePath() {
return filePath;
}
public List<String> readFile() throws IOException {
return Files.lines(filePath)
.map(this::mapFileLine).collect(Collectors.toList());
}
protected abstract String mapFileLine(String line);
}

View File

@@ -0,0 +1,15 @@
package com.baeldung.abstractclasses.filereaders;
import java.nio.file.Path;
public class LowercaseFileReader extends BaseFileReader {
public LowercaseFileReader(Path filePath) {
super(filePath);
}
@Override
public String mapFileLine(String line) {
return line.toLowerCase();
}
}

View File

@@ -0,0 +1,15 @@
package com.baeldung.abstractclasses.filereaders;
import java.nio.file.Path;
public class UppercaseFileReader extends BaseFileReader {
public UppercaseFileReader(Path filePath) {
super(filePath);
}
@Override
public String mapFileLine(String line) {
return line.toUpperCase();
}
}

View File

@@ -0,0 +1,21 @@
package com.baeldung.innerinterfaces;
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

@@ -0,0 +1,19 @@
package com.baeldung.innerinterfaces;
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

@@ -0,0 +1,49 @@
package com.baeldung.keyword.thiskeyword;
public class KeywordUnitTest {
private String name;
private int age;
public KeywordUnitTest() {
this("John", 27);
this.printMessage();
printInstance(this);
}
public KeywordUnitTest(String name, int age) {
this.name = name;
this.age = age;
}
public void printMessage() {
System.out.println("invoked by this");
}
public void printInstance(KeywordUnitTest thisKeyword) {
System.out.println(thisKeyword);
}
public KeywordUnitTest getCurrentInstance() {
return this;
}
class ThisInnerClass {
boolean isInnerClass = true;
public ThisInnerClass() {
KeywordUnitTest thisKeyword = KeywordUnitTest.this;
String outerString = KeywordUnitTest.this.name;
System.out.println(this.isInnerClass);
}
}
@Override
public String toString() {
return "KeywordTest{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}

View File

@@ -0,0 +1,5 @@
package com.baeldung.markerinterface;
public interface DeletableShape extends Shape {
}

View File

@@ -0,0 +1,22 @@
package com.baeldung.markerinterface;
public class Rectangle implements DeletableShape {
private double width;
private double height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double getArea() {
return width * height;
}
@Override
public double getCircumference() {
return 2 * (width + height);
}
}

View File

@@ -0,0 +1,6 @@
package com.baeldung.markerinterface;
public interface Shape {
double getArea();
double getCircumference();
}

View File

@@ -0,0 +1,14 @@
package com.baeldung.markerinterface;
public class ShapeDao {
public boolean delete(Object object) {
if (!(object instanceof DeletableShape)) {
return false;
}
// Calling the code that deletes the entity from the database
return true;
}
}

View File

@@ -0,0 +1,51 @@
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

@@ -0,0 +1,10 @@
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9
This is line 10

View File

@@ -0,0 +1,20 @@
package com.baeldung.abstractclasses.test;
import com.baeldung.abstractclasses.filereaders.BaseFileReader;
import com.baeldung.abstractclasses.filereaders.LowercaseFileReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class LowercaseFileReaderUnitTest {
@Test
public void givenLowercaseFileReaderInstance_whenCalledreadFile_thenCorrect() throws Exception {
Path path = Paths.get(getClass().getClassLoader().getResource("files/test.txt").toURI());
BaseFileReader lowercaseFileReader = new LowercaseFileReader(path);
assertThat(lowercaseFileReader.readFile()).isInstanceOf(List.class);
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.abstractclasses.test;
import com.baeldung.abstractclasses.filereaders.BaseFileReader;
import com.baeldung.abstractclasses.filereaders.UppercaseFileReader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class UppercaseFileReaderUnitTest {
@Test
public void givenUppercaseFileReaderInstance_whenCalledreadFile_thenCorrect() throws Exception {
Path path = Paths.get(getClass().getClassLoader().getResource("files/test.txt").toURI());
BaseFileReader uppercaseFileReader = new UppercaseFileReader(path);
assertThat(uppercaseFileReader.readFile()).isInstanceOf(List.class);
}
}

View File

@@ -0,0 +1,20 @@
package com.baeldung.innerinterfaces;
import com.baeldung.innerinterfaces.CommaSeparatedCustomers;
import com.baeldung.innerinterfaces.Customer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import static org.junit.Assert.assertEquals;
@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

@@ -0,0 +1,26 @@
package com.baeldung.markerinterface;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class MarkerInterfaceUnitTest {
@Test
public void givenDeletableObjectThenTrueReturned() {
ShapeDao shapeDao = new ShapeDao();
Object rectangle = new Rectangle(2, 3);
boolean result = shapeDao.delete(rectangle);
assertEquals(true, result);
}
@Test
public void givenNonDeletableObjectThenFalseReturned() {
ShapeDao shapeDao = new ShapeDao();
Object object = new Object();
boolean result = shapeDao.delete(object);
assertEquals(false, result);
}
}

View File

@@ -0,0 +1,20 @@
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

@@ -0,0 +1,21 @@
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() {
StaticNested nested = new StaticNested();
nested.run();
}
}

View File

@@ -0,0 +1,22 @@
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

@@ -0,0 +1,30 @@
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();
InnerClass inner = outer.new InnerClass();
inner.run();
}
}

View File

@@ -0,0 +1,20 @@
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();
Inner inner = outer.new Inner();
inner.run();
}
}

View File

@@ -0,0 +1,38 @@
package com.baeldung.objects;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class ObjectsUnitTest {
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());
}
}

View File

@@ -19,5 +19,6 @@
<module>core-java-lang-oop-patterns</module>
<module>core-java-lang-oop-generics</module>
<module>core-java-lang-oop-modifiers</module>
<module>core-java-lang-oop-types</module>
</modules>
</project>