1 /*
  2  * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 package hat.util;
 26 
 27 import java.util.function.BiFunction;
 28 import java.util.function.Predicate;
 29 import java.util.regex.Matcher;
 30 import java.util.regex.Pattern;
 31 
 32 public record Regex(Pattern pattern) {
 33     public interface Match {
 34         boolean matched();
 35     }
 36 
 37     public interface OK extends Match {
 38         Regex regex();
 39         Matcher matcher();
 40         default  String string(int idx) {
 41             return matcher().group(idx);
 42         }
 43 
 44         default float asFloat(int idx) {
 45             return Float.parseFloat(string(idx));
 46         }
 47 
 48         default  int asInt(int idx) {
 49             return Integer.parseInt(string(idx));
 50         }
 51         default  int[] asInts(int from, int count) {
 52             int[] ints = new int[count];
 53             for (int i = 0; i<count; i++) {
 54                 ints[i]= Integer.parseInt(string(from + i));
 55             }
 56             return ints;
 57         }
 58         default  float[] asFloats(int from, int count) {
 59             float[] floats = new float[count];
 60             for (int i = 0; i<count; i++) {
 61                 floats[i]=Float.parseFloat(string(from + i));
 62             }
 63             return floats;
 64         }
 65     }
 66 
 67 
 68     public record DefaultOk(Regex regex, Matcher matcher, boolean matched) implements OK {
 69         public static OK of(Regex regex, Matcher matcher) {
 70             return new DefaultOk(regex, matcher, true);
 71         }
 72     }
 73 
 74     record FAIL(boolean matched) implements Match {
 75         public static FAIL of() {
 76             return new FAIL(false);
 77         }
 78     }
 79 
 80     public static Regex of(String... strings) {
 81         return new Regex(Pattern.compile(String.join("", strings)));
 82     }
 83 
 84     public static Match any(String line, Regex... regexes) {
 85         for (Regex r : regexes) {
 86             if (r.is(line) instanceof OK ok) {
 87                 return ok;
 88             }
 89         }
 90         return FAIL.of();
 91     }
 92     public Match is(String s, Predicate<Matcher> matcherPredicate, BiFunction<Regex,Matcher,OK> factory) {
 93         if (pattern.matcher(s) instanceof Matcher matcher && matcher.matches() && matcherPredicate.test(matcher)) {
 94             return factory.apply(this, matcher);
 95         } else {
 96             return FAIL.of();
 97         }
 98     }
 99     public Match is(String s, BiFunction<Regex,Matcher,OK> factory) {
100         return is(s, _->true,factory);
101     }
102     public Match is(String s, Predicate<Matcher> matcherPredicate) {
103         return is(s, matcherPredicate,(r,m)->new DefaultOk(r,m, true));
104     }
105     public Match is(String s) {
106         return is(s, _->true);
107     }
108     public boolean matches(String s, Predicate<Matcher> matcherPredicate) {
109         return is(s, matcherPredicate).matched();
110     }
111     public boolean matches(String s) {
112         return is(s).matched();
113     }
114     public boolean matchesOrThrow(String s) {
115         if(!is(s).matched()){
116             throw new RuntimeException("failed expected match");
117         }else{
118             return true;
119         }
120     }
121 }