|
| 1 | +package com.code402; |
| 2 | + |
| 3 | +import com.amazonaws.services.lambda.runtime.Context; |
| 4 | +import com.amazonaws.services.lambda.runtime.RequestHandler; |
| 5 | +import dk.brics.automaton.*; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.Arrays; |
| 8 | + |
| 9 | +public class App implements RequestHandler<Request, Response> { |
| 10 | + public Response match(String haystack, String needle) { |
| 11 | + ArrayList<String> hits = new ArrayList<String>(); |
| 12 | + RegExp r; |
| 13 | + try { |
| 14 | + r = new RegExp(needle, RegExp.NONE); |
| 15 | + } catch (IllegalArgumentException iae) { |
| 16 | + return new Response(null, iae.getMessage()); |
| 17 | + } |
| 18 | + |
| 19 | + RunAutomaton a = new RunAutomaton(r.toAutomaton()); |
| 20 | + |
| 21 | + AutomatonMatcher m = a.newMatcher(haystack); |
| 22 | + while (m.find()) { |
| 23 | + hits.add(m.group()); |
| 24 | + } |
| 25 | + |
| 26 | + int len = hits.size(); |
| 27 | + if (len > 10) len = 10; |
| 28 | + String[] strings = Arrays.copyOf(hits.toArray(), len, String[].class); |
| 29 | + return new Response(strings, null); |
| 30 | + } |
| 31 | + |
| 32 | + public Response handleRequest(Request request, Context context) { |
| 33 | + System.out.println( |
| 34 | + "Received request: needle=" + request.getNeedle() + ", haystack=" + request.getHaystack()); |
| 35 | + return match(request.haystack, request.needle); |
| 36 | + } |
| 37 | + |
| 38 | + public static void main(String[] args) { |
| 39 | + String haystack = args[0]; |
| 40 | + String needle = args[1]; |
| 41 | + Response rv = (new App()).match(haystack, needle); |
| 42 | + |
| 43 | + if (rv.error != null) { |
| 44 | + System.err.println(rv.error); |
| 45 | + System.exit(1); |
| 46 | + } |
| 47 | + |
| 48 | + for (int i = 0; i < rv.getHits().length; i++) { |
| 49 | + System.out.println(rv.getHits()[i]); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments