From 8b12bf0d5ffa7cefeed2572f2fbba8de113d446f Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Sat, 26 Feb 2022 16:01:47 +0100 Subject: [PATCH] =?UTF-8?q?ProtocolRepository=20impl=C3=A9ment=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../interfaces/ProtocolRepository.java | 15 +++++++ .../repository/ProtocolRepositoryImpl.java | 44 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 app/src/main/java/lightcontainer/interfaces/ProtocolRepository.java create mode 100644 app/src/main/java/lightcontainer/repository/ProtocolRepositoryImpl.java diff --git a/app/src/main/java/lightcontainer/interfaces/ProtocolRepository.java b/app/src/main/java/lightcontainer/interfaces/ProtocolRepository.java new file mode 100644 index 0000000..2ee0743 --- /dev/null +++ b/app/src/main/java/lightcontainer/interfaces/ProtocolRepository.java @@ -0,0 +1,15 @@ +package lightcontainer.interfaces; + +import lightcontainer.protocol.ProtocolReader; +import lightcontainer.protocol.ProtocolWriter; + +public interface ProtocolRepository { + + boolean executeReader(String data); + + String executeWriter(String... datas); + + void addReader(ProtocolReader reader); + + void addWriter(ProtocolWriter writer); +} diff --git a/app/src/main/java/lightcontainer/repository/ProtocolRepositoryImpl.java b/app/src/main/java/lightcontainer/repository/ProtocolRepositoryImpl.java new file mode 100644 index 0000000..52071f6 --- /dev/null +++ b/app/src/main/java/lightcontainer/repository/ProtocolRepositoryImpl.java @@ -0,0 +1,44 @@ +package lightcontainer.repository; + +import lightcontainer.interfaces.ProtocolRepository; +import lightcontainer.protocol.ProtocolReader; +import lightcontainer.protocol.ProtocolWriter; + +import java.util.HashSet; +import java.util.Set; + +public class ProtocolRepositoryImpl implements ProtocolRepository { + private Set readers = new HashSet<>(); + private Set writers = new HashSet<>(); + + @Override + public boolean executeReader(String data) { + for (ProtocolReader reader : readers) { + if (reader.execute(data)) { + return true; + } + } + return false; + } + + @Override + public String executeWriter(String... datas) { + for (ProtocolWriter writer : writers) { + String command = writer.execute(datas); + if (command != null) { + return command; + } + } + return null; + } + + @Override + public void addReader(ProtocolReader reader) { + this.readers.add(reader); + } + + @Override + public void addWriter(ProtocolWriter writer) { + this.writers.add(writer); + } +}