package lan.dk.podcastserver.utils;

import javaslang.collection.List;
import javaslang.collection.Stream;
import javaslang.control.Option;
import javaslang.control.Try;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by kevin on 04/03/2017
 */
public class MatcherExtractor {

    private final Matcher matcher;
    private final Boolean isFind;

    private MatcherExtractor(Matcher matcher) {
        this.matcher = matcher;
        this.isFind = matcher.find();
    }

    public static PatternExtractor from(Pattern pattern) {
        return new PatternExtractor(pattern);
    }

    public Option<String> group(Integer group) {
        if (!isFind) {
            return Option.none();
        }

        return Try.of(() -> matcher.group(group)).toOption();
    }

    public Option<List<String>> groups() {
        if (!isFind) {
            return Option.none();
        }

        return Option.of(
                Stream.from(1)
                        .take(matcher.groupCount())
                        .map(matcher::group)
                        .toList()
        );
    }

    public static class PatternExtractor {

        private final Pattern pattern;

        private PatternExtractor(Pattern pattern) { this.pattern = pattern; }

        public MatcherExtractor on(String value) { return new MatcherExtractor(pattern.matcher(value)); }

    }

}

@@ -927,35 +927,8 @@
 urn 
-Option.of(%0A                
 Stre
@@ -938,24 +938,16 @@
 from(1)%0A
-        
         
@@ -998,24 +998,16 @@
         
-     
-   
 .map(mat
@@ -1035,24 +1035,16 @@
         
-        
 .toList(
@@ -1053,16 +1053,45 @@
         
+        .transform(Option::of
 );%0A    %7D

use transform for result of groups() method