Last active
September 28, 2022 17:45
-
-
Save matheusmv/c0290d7f4808335ff1b7646c99875f70 to your computer and use it in GitHub Desktop.
sealed java class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Objects; | |
import java.util.function.Predicate; | |
import java.util.function.Supplier; | |
public class Contact { | |
private final String name; | |
private final String number; | |
private final String cpf; | |
private final String address; | |
private final Integer age; | |
protected Contact(String name, String number, String cpf, String address, Integer age) { | |
this.name = name; | |
this.number = number; | |
this.cpf = cpf; | |
this.address = address; | |
this.age = age; | |
} | |
public static Contact of(String name, String number, String cpf, String address, Integer age) { | |
return new Contact( | |
Objects.requireNonNull(name, "name must not be null"), | |
Objects.requireNonNull(number, "number must not be null"), | |
Objects.requireNonNull(cpf, "cpf must not be null"), | |
Objects.requireNonNull(address, "address must not be null"), | |
passRequirementsOrElseThrows(age, v -> Objects.nonNull(v) && v > 0, () -> "age must be greater than zero") | |
); | |
} | |
private static <V> V passRequirementsOrElseThrows( | |
final V value, | |
Predicate<? super V> predicate, | |
Supplier<String> message | |
) { | |
if (predicate.negate().test(value)) { | |
throw new RuntimeException(message.get()); | |
} | |
return value; | |
} | |
public String getName() { | |
return name; | |
} | |
public String getNumber() { | |
return number; | |
} | |
public String getCpf() { | |
return cpf; | |
} | |
public String getAddress() { | |
return address; | |
} | |
public int getAge() { | |
return age; | |
} | |
@Override | |
public String toString() { | |
String template = "Contact{name=%s, number=%s, cpf=%s, address=%s, age=%s}"; | |
return template.formatted(name, number, cpf, address, age); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public final class ContactCSVParser extends FileParser<Contact> { | |
private static final String CSV_TEMPLATE = "%s,%s,%s,%s,%d"; | |
private static final Pattern CSVPattern = Pattern.compile("^([\\w\\s\\d]+),([\\W\\s\\d]+),([\\W\\s\\d]{11}),([\\w\\s\\d]+),(\\d+)$"); | |
@Override | |
protected String toFileFormat(Contact object) { | |
return CSV_TEMPLATE.formatted( | |
object.getName(), | |
object.getNumber(), | |
object.getCpf(), | |
object.getAddress(), | |
object.getAge()); | |
} | |
@Override | |
protected Contact stringFormatToObject(String line) { | |
Matcher contactCSVPattern = CSVPattern.matcher(line); | |
if (contactCSVPattern.matches()) { | |
String contactName = contactCSVPattern.group(1); | |
String phoneNumber = contactCSVPattern.group(2); | |
String cpf = contactCSVPattern.group(3); | |
String address = contactCSVPattern.group(4); | |
Integer age = Integer.parseInt(contactCSVPattern.group(5)); | |
return Contact.of(contactName, phoneNumber, cpf, address, age); | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public final class ContactFileParser extends FileParser<Contact> { | |
private static final String FILE_TEMPLATE = "Name: %s Number: %s CPF: %s Address: %s Age: %d"; | |
private static final Pattern filePattern = Pattern.compile("^\\w+: ([\\w\\s]+) \\w+: ([\\W\\s\\d]+) \\w+: ([\\d]{11}) \\w+: ([\\w\\s]+) \\w+: (\\d+)$"); | |
@Override | |
protected String toFileFormat(Contact object) { | |
return FILE_TEMPLATE.formatted( | |
object.getName(), | |
object.getNumber(), | |
object.getCpf(), | |
object.getAddress(), | |
object.getAge()); | |
} | |
@Override | |
protected Contact stringFormatToObject(String line) { | |
Matcher contactFilePattern = filePattern.matcher(line); | |
if (contactFilePattern.matches()) { | |
String contactName = contactFilePattern.group(1); | |
String phoneNumber = contactFilePattern.group(2); | |
String cpf = contactFilePattern.group(3); | |
String address = contactFilePattern.group(4); | |
Integer age = Integer.parseInt(contactFilePattern.group(5)); | |
return Contact.of(contactName, phoneNumber, cpf, address, age); | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.nio.charset.StandardCharsets; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.nio.file.StandardOpenOption; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.Objects; | |
import java.util.stream.Stream; | |
public abstract sealed class FileParser<O> | |
permits ContactCSVParser, ContactFileParser { | |
protected abstract String toFileFormat(O object); | |
protected abstract O stringFormatToObject(String line); | |
public void write(String path, List<O> objects) { | |
Path pathToWrite = Paths.get(Objects.requireNonNull(path, "path must not be null")); | |
List<String> formattedObjects = Objects.requireNonNull(objects, "list must not be null") | |
.stream() | |
.map(this::toFileFormat) | |
.toList(); | |
try { | |
Files.write(pathToWrite, formattedObjects, | |
StandardCharsets.UTF_8, | |
StandardOpenOption.CREATE, | |
StandardOpenOption.APPEND); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
public List<O> read(String path) { | |
Path pathToRead = Paths.get(Objects.requireNonNull(path, "path must not be null")); | |
try (Stream<String> lines = Files.lines(pathToRead, StandardCharsets.UTF_8)) { | |
return lines.map(this::stringFormatToObject) | |
.filter(Objects::nonNull) | |
.toList(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return Collections.emptyList(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
public class Main { | |
public static void main(String[] args) { | |
var contacts = List.of( | |
Contact.of("contact 1", "+55 (88) 88888-8888", "88888888888", "address 1", 22), | |
Contact.of("contact 2", "+55 (77) 77777-7777", "77777777777", "address 2", 29), | |
Contact.of("contact 3", "+55 (66) 66666-6666", "66666666666", "address 3", 55), | |
Contact.of("contact 4", "+55 (55) 55555-5555", "55555555555", "address 4", 72)); | |
final String contactFilePath = "./contacts.txt"; | |
writeWithFileParser(contactFilePath, new ContactFileParser(), contacts); | |
log("Reading from " + contactFilePath); | |
readWithFileParser(contactFilePath, new ContactFileParser()) | |
.forEach(contact -> log(contact.toString())); | |
final String contactCSVPath = "./contacts.csv"; | |
writeWithFileParser(contactCSVPath, new ContactCSVParser(), contacts); | |
log("Reading from " + contactCSVPath); | |
readWithFileParser(contactCSVPath, new ContactCSVParser()) | |
.forEach(contact -> log(contact.toString())); | |
} | |
static <O> void writeWithFileParser(final String path, | |
final FileParser<O> fileParser, | |
final List<O> objects) { | |
fileParser.write(path, objects); | |
} | |
static <O> List<O> readWithFileParser(final String path, | |
final FileParser<O> fileParser) { | |
return fileParser.read(path); | |
} | |
static void log(final String message) { | |
System.out.println(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment