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); + } +}