summaryrefslogtreecommitdiff
path: root/graphs/java/myKitten/src
diff options
context:
space:
mode:
Diffstat (limited to 'graphs/java/myKitten/src')
-rw-r--r--graphs/java/myKitten/src/main/java/fr/epita/assistants/mykitten/MyKitten.java64
1 files changed, 64 insertions, 0 deletions
diff --git a/graphs/java/myKitten/src/main/java/fr/epita/assistants/mykitten/MyKitten.java b/graphs/java/myKitten/src/main/java/fr/epita/assistants/mykitten/MyKitten.java
new file mode 100644
index 0000000..e63c2f7
--- /dev/null
+++ b/graphs/java/myKitten/src/main/java/fr/epita/assistants/mykitten/MyKitten.java
@@ -0,0 +1,64 @@
+package fr.epita.assistants.mykitten;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.stream.Stream;
+
+public class MyKitten {
+ /**
+ * Initializer.
+ *
+ * @param srcPath Source file path.
+ */
+ public MyKitten(String srcPath) {
+ try {
+ this.streamContent = Files.lines(Paths.get(srcPath));
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * Use the streamContent to replace `wordToReplace` with "miaou". Don't forget
+ * to add the line number beforehand for each line. Store the new
+ * result directly in the streamContent field.
+ *
+ * @param wordToReplace The word to replace
+ */
+ public void replaceByMiaou(String wordToReplace) {
+ final int[] line = {1};
+ this.streamContent = this.streamContent.map(i -> line[0]++ + " " + i).map(i -> i.replace(wordToReplace, "miaou"));
+ }
+
+ /**
+ * Use the streamContent to write the content into the destination file.
+ *
+ * @param destPath Destination file path.
+ */
+ public void toFile(String destPath) {
+ try {
+ Files.write(Paths.get(destPath), this.streamContent.toList(), StandardCharsets.UTF_8);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * Creates an instance of MyKitten and calls the above methods to do it
+ * straightforwardly.
+ *
+ * @param srcPath Source file path
+ * @param destPath Destination file path
+ * @param wordToReplace Word to replace
+ */
+ public static void miaou(String srcPath, String destPath,
+ String wordToReplace) {
+ MyKitten chatteDeTaDaronne = new MyKitten(srcPath);
+ chatteDeTaDaronne.replaceByMiaou(wordToReplace);
+ chatteDeTaDaronne.toFile(destPath);
+ }
+
+ public Stream<String> streamContent;
+}