[BAEL-15993] - Move articles out of core-java pt 3
This commit is contained in:
@@ -15,4 +15,5 @@ This module contains articles about Object-oriented programming (OOP) in Java
|
||||
- [Composition, Aggregation, and Association in Java](https://www.baeldung.com/java-composition-aggregation-association)
|
||||
- [Static and Default Methods in Interfaces in Java](https://www.baeldung.com/java-static-default-methods)
|
||||
- [Java Copy Constructor](https://www.baeldung.com/java-copy-constructor)
|
||||
- [Abstract Classes in Java](https://www.baeldung.com/java-abstract-class)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-lang-oop)[[More -->]](/core-java-modules/core-java-lang-oop-3)
|
||||
|
||||
@@ -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());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user