|
1 | | -/* |
2 | | - * Copyright 2019 hbz |
| 1 | +/* Copyright 2019 Pascal Christoph (hbz) |
3 | 2 | * |
4 | 3 | * Licensed under the Apache License, Version 2.0 the "License"; |
5 | 4 | * you may not use this file except in compliance with the License. |
|
13 | 12 | * See the License for the specific language governing permissions and |
14 | 13 | * limitations under the License. |
15 | 14 | */ |
| 15 | + |
16 | 16 | package org.metafacture.strings; |
17 | 17 |
|
18 | 18 | import org.metafacture.framework.FluxCommand; |
| 19 | +import org.metafacture.framework.ObjectPipe; |
19 | 20 | import org.metafacture.framework.ObjectReceiver; |
20 | 21 | import org.metafacture.framework.annotations.Description; |
21 | 22 | import org.metafacture.framework.annotations.In; |
22 | 23 | import org.metafacture.framework.annotations.Out; |
23 | | -import org.metafacture.framework.helpers.DefaultObjectPipe; |
24 | 24 |
|
25 | 25 | /** |
26 | 26 | * Collects strings and emits them as records when a line matches the pattern. |
27 | | - * Appends to every incoming line a line feed so that the original structure is |
28 | | - * preserved. |
| 27 | + * Appends to every incoming line a line feed so that the original structure |
| 28 | + * is preserved. |
29 | 29 | * |
30 | 30 | * @author Pascal Christoph (dr0i). |
31 | 31 | * |
32 | 32 | */ |
33 | | -@Description("Collects strings and emits them as records when a line matches the pattern.") |
34 | | -@In(String.class) |
35 | | -@Out(String.class) |
36 | | -@FluxCommand("lines-to-records") |
| 33 | +@Description ( "Collects strings and emits them as records when a" |
| 34 | + +" line matches the pattern or the stream is closed." ) |
| 35 | +@In ( String.class ) |
| 36 | +@Out ( String.class ) |
| 37 | +@FluxCommand ( "lines-to-records" ) |
37 | 38 | public final class LineRecorder |
38 | | - extends DefaultObjectPipe<String, ObjectReceiver<String>> { |
| 39 | + implements ObjectPipe<String,ObjectReceiver<String>> { |
| 40 | + |
| 41 | + private final static int SB_CAPACITY=4096*7; |
| 42 | + // empty line is the default |
| 43 | + private String recordMarkerRegexp="^\\s*$"; |
| 44 | + StringBuilder record=new StringBuilder( |
| 45 | + SB_CAPACITY); |
| 46 | + ObjectReceiver<String> receiver; |
| 47 | + |
| 48 | + public void setRecordMarkerRegexp ( final String regexp ) { |
| 49 | + recordMarkerRegexp=regexp; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void process ( final String line ) { |
| 54 | + if(line.matches( |
| 55 | + recordMarkerRegexp)){ |
| 56 | + getReceiver().process( |
| 57 | + record.toString()); |
| 58 | + record=new StringBuilder( |
| 59 | + SB_CAPACITY); |
| 60 | + }else |
| 61 | + record.append( |
| 62 | + line+"\n"); |
| 63 | + } |
39 | 64 |
|
40 | | - private final int SB_CAPACITY = 4096 * 7; |
41 | | - private String recordMarkerRegexp = "^\\s*$"; // empty line is default |
42 | | - StringBuilder record = new StringBuilder(SB_CAPACITY); |
| 65 | + @Override |
| 66 | + public void resetStream ( ) { |
| 67 | + record=new StringBuilder( |
| 68 | + SB_CAPACITY); |
| 69 | + } |
43 | 70 |
|
44 | | - public void setRecordMarkerRegexp(final String regexp) { |
45 | | - this.recordMarkerRegexp = regexp; |
| 71 | + @Override |
| 72 | + public void closeStream ( ) { |
| 73 | + getReceiver().process( |
| 74 | + record.toString()); |
46 | 75 | } |
47 | 76 |
|
48 | 77 | @Override |
49 | | - public void process(final String line) { |
50 | | - assert !isClosed(); |
51 | | - if (line.matches(recordMarkerRegexp)) { |
52 | | - getReceiver().process(record.toString()); |
53 | | - record = new StringBuilder(SB_CAPACITY); |
54 | | - } else |
55 | | - record.append(line + "\n"); |
| 78 | + public <R extends ObjectReceiver<String>> R setReceiver ( R receiver ) { |
| 79 | + this.receiver=receiver; |
| 80 | + return receiver; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Returns a reference to the downstream module. |
| 85 | + * |
| 86 | + * @return reference to the downstream module |
| 87 | + */ |
| 88 | + protected final ObjectReceiver<String> getReceiver ( ) { |
| 89 | + return receiver; |
56 | 90 | } |
57 | 91 |
|
58 | 92 | } |
0 commit comments