Skip to content

Commit

Permalink
refactor: use generic types in the relevant Parser and Serializer met…
Browse files Browse the repository at this point in the history
…hods, as to make their use more strict and safe
  • Loading branch information
DaubieMatthieu committed May 7, 2023
1 parent f035dcf commit 37c516c
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/main/java/io/github/mdaubie/subtitlesparser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public SF parse(String text) throws UnexpectedException {
return dynamicParse(matcher, format.baseClass());
}

private <T> T dynamicParse(Matcher matcher, Class<T> type) throws UnexpectedException {
private <T extends PatternedObject> T dynamicParse(Matcher matcher, Class<T> type) throws UnexpectedException {
try {
T object = type.getConstructor().newInstance();
for (Field field : type.getFields()) {
Expand All @@ -57,13 +57,13 @@ private Object parseObject(String value, Field field) throws UnexpectedException
throw new UnexpectedException(String.format("Type %s is not handled by parser", type));
}

private List<Object> parseList(String value, ParameterizedType type) throws UnexpectedException {
private <T extends PatternedObject> List<T> parseList(String value, ParameterizedType type) throws UnexpectedException {
Type elementType = type.getActualTypeArguments()[0];
//TODO might need to implement basic types handling for some of the formats
Class<? extends PatternedObject> elementClass = (Class<? extends PatternedObject>) elementType;
@SuppressWarnings("unchecked") Class<T> elementClass = (Class<T>) elementType;
Pattern pattern = PatternHolder.getPattern(elementClass);
Matcher matcher = pattern.matcher(value);
List<Object> list = new ArrayList<>();
List<T> list = new ArrayList<>();
while (matcher.find())
list.add(dynamicParse(matcher, elementClass));
return list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
import java.util.regex.Pattern;

public record Serializer<SF extends SubtitlesFile>(Format<SF> format) {
public void writeToFile(SubtitlesFile subtitlesFile, File file) throws IOException {
public void writeToFile(SF subtitlesFile, File file) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.append(serialize(subtitlesFile));
writer.close();
}

public String serialize(SubtitlesFile sf) throws UnexpectedException {
public String serialize(SF sf) throws UnexpectedException {
return dynamicSerialize(sf);
}

Expand All @@ -43,7 +43,7 @@ private String dynamicSerialize(PatternedObject object) throws UnexpectedExcepti
}
}

private String serializeAttribute(Object object, Field field) throws UnexpectedException, IllegalAccessException {
private String serializeAttribute(PatternedObject object, Field field) throws UnexpectedException, IllegalAccessException {
Class<?> type = field.getType();
if (type == String.class) return String.valueOf(field.get(object));
if (type == Integer.class) return String.valueOf(field.get(object));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class ParserTest {
@ParameterizedTest
@MethodSource("parse")
void parse(Format<? extends SubtitlesFile> format, String text, Supplier<SubtitlesFile> expectedResult) throws Exception {
<SF extends SubtitlesFile> void parse(Format<SF> format, String text, Supplier<SF> expectedResult) throws Exception {
SubtitlesFile actualResult = new Parser<>(format).parse(text);
assertThat(actualResult).usingRecursiveComparison().isEqualTo(expectedResult.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class SerializerTest {
@ParameterizedTest
@MethodSource("serialize")
void serialize(Format<? extends SubtitlesFile> format, Supplier<SubtitlesFile> input, String expectedResult) throws Exception {
<SF extends SubtitlesFile> void serialize(Format<SF> format, Supplier<SF> input, String expectedResult) throws Exception {
String actualResult = new Serializer<>(format).serialize(input.get());
assertEquals(expectedResult, actualResult);
}
Expand Down

0 comments on commit 37c516c

Please sign in to comment.