1 /*
2 * CSVeed (https://github.com/42BV/CSVeed)
3 *
4 * Copyright 2013-2023 CSVeed.
5 *
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of The Apache Software License,
8 * Version 2.0 which accompanies this distribution, and is available at
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 */
11 package org.csveed.bean.conversion;
12
13 import java.util.regex.Pattern;
14
15 /**
16 * The Class PatternConverter.
17 */
18 public class PatternConverter extends AbstractConverter<Pattern> {
19
20 /** The flags. */
21 private final int flags;
22
23 /**
24 * Instantiates a new pattern converter.
25 */
26 public PatternConverter() {
27 this(0);
28 }
29
30 /**
31 * Instantiates a new pattern converter.
32 *
33 * @param flags
34 * the flags
35 */
36 public PatternConverter(int flags) {
37 super(Pattern.class);
38 this.flags = flags;
39 }
40
41 @Override
42 public Pattern fromString(String text) throws Exception {
43 return text != null ? Pattern.compile(text, this.flags) : null;
44 }
45
46 @Override
47 public String toString(Pattern value) throws Exception {
48 return value != null ? value.pattern() : "";
49 }
50 }