summaryrefslogtreecommitdiff
path: root/jws/kafka/src/main/java/fr/epita/assistants/StringInfoProcessor.java
blob: b4196238a8e20076196d24e60c04f780b210cd26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package fr.epita.assistants;

import fr.epita.assistants.utils.StringInfo;
import io.smallrye.reactive.messaging.annotations.Broadcast;
import org.eclipse.microprofile.reactive.messaging.Incoming;
import org.eclipse.microprofile.reactive.messaging.Outgoing;

public class StringInfoProcessor {
    private boolean isPalindrome(String word) {
        if (word == null) return false;
        word = word.toLowerCase().strip();
        if (word.isEmpty()) return true;
        for (int i = 0, j = word.length() - 1; i < j; i++, j--) {
            while (word.charAt(i) == ' ') {
                i++;
            }
            while (word.charAt(j) == ' ') {
                j--;
            }
            if (word.charAt(i) != word.charAt(j)) return false;
        }
        return true;
    }

    @Incoming("string-info-command")
    @Outgoing("string-info-aggregate")
    @Broadcast
    public StringInfo process(String s) {
        String tmp = s.toLowerCase();
        int v = 0;
        int c = 0;
        for (int i = 0; i < tmp.length(); i++) {
            if (tmp.charAt(i) == 'a' || tmp.charAt(i) == 'e' || tmp.charAt(i) == 'i' || tmp.charAt(i) == 'o' || tmp.charAt(i) == 'u' || tmp.charAt(i) == 'y')
                v++;
            else if (tmp.charAt(i) <= 'z' && tmp.charAt(i) >= 'a')
                c++;
        }
        return new StringInfo(s, v, c, isPalindrome(s));
    }

}