11package com .baeldung .regexp .optmization ;
22
3- import org .junit .After ;
43import org .junit .Before ;
54import org .junit .Test ;
65
1413
1514public class OptimizedMatcherUnitTest {
1615
17- private long time ;
18- private long mstimePreCompiled ;
19- private long mstimeNotPreCompiled ;
20-
2116 private String action ;
2217
2318 private List <String > items ;
19+
20+ private class TimeWrapper {
21+ private long time ;
22+ private long mstimePreCompiled ;
23+ private long mstimeNotPreCompiled ;
24+ }
2425
2526 @ Before
2627 public void setup () {
@@ -48,27 +49,27 @@ public void setup() {
4849
4950 @ Test
5051 public void givenANotPreCompiledAndAPreCompiledPatternA_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled () {
51-
52- testPatterns ("A*" );
53- assertTrue (mstimePreCompiled < mstimeNotPreCompiled );
52+ TimeWrapper timeObj = new TimeWrapper ();
53+ testPatterns ("A*" , timeObj );
54+ assertTrue (timeObj . mstimePreCompiled < timeObj . mstimeNotPreCompiled );
5455 }
5556
5657 @ Test
5758 public void givenANotPreCompiledAndAPreCompiledPatternABC_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled () {
58-
59- testPatterns ("A*B*C*" );
60- assertTrue (mstimePreCompiled < mstimeNotPreCompiled );
59+ TimeWrapper timeObj = new TimeWrapper ();
60+ testPatterns ("A*B*C*" , timeObj );
61+ assertTrue (timeObj . mstimePreCompiled < timeObj . mstimeNotPreCompiled );
6162 }
6263
6364 @ Test
6465 public void givenANotPreCompiledAndAPreCompiledPatternECWF_whenMatcheItems_thenPreCompiledFasterThanNotPreCompiled () {
65-
66- testPatterns ("E*C*W*F*" );
67- assertTrue (mstimePreCompiled < mstimeNotPreCompiled );
66+ TimeWrapper timeObj = new TimeWrapper ();
67+ testPatterns ("E*C*W*F*" , timeObj );
68+ assertTrue (timeObj . mstimePreCompiled < timeObj . mstimeNotPreCompiled );
6869 }
6970
70- private void testPatterns (String regex ) {
71- time = System .nanoTime ();
71+ private void testPatterns (String regex , TimeWrapper timeObj ) {
72+ timeObj . time = System .nanoTime ();
7273 int matched = 0 ;
7374 int unmatched = 0 ;
7475
@@ -83,10 +84,10 @@ private void testPatterns(String regex) {
8384
8485 this .action = "uncompiled: regex=" + regex + " matched=" + matched + " unmatched=" + unmatched ;
8586
86- this .mstimeNotPreCompiled = (System .nanoTime () - time ) / 1000000 ;
87- System .out .println (this .action + ": " + mstimeNotPreCompiled + "ms" );
87+ timeObj .mstimeNotPreCompiled = (System .nanoTime () - timeObj . time ) / 1000000 ;
88+ System .out .println (this .action + ": " + timeObj . mstimeNotPreCompiled + "ms" );
8889
89- time = System .nanoTime ();
90+ timeObj . time = System .nanoTime ();
9091
9192 Matcher matcher = Pattern .compile (regex ).matcher ("" );
9293 matched = 0 ;
@@ -103,7 +104,7 @@ private void testPatterns(String regex) {
103104
104105 this .action = "compiled: regex=" + regex + " matched=" + matched + " unmatched=" + unmatched ;
105106
106- this .mstimePreCompiled = (System .nanoTime () - time ) / 1000000 ;
107- System .out .println (this .action + ": " + mstimePreCompiled + "ms" );
107+ timeObj .mstimePreCompiled = (System .nanoTime () - timeObj . time ) / 1000000 ;
108+ System .out .println (this .action + ": " + timeObj . mstimePreCompiled + "ms" );
108109 }
109110}
0 commit comments