View Javadoc
1   /*
2    * #%L
3    * PatternUtils.java - mongodb-async-driver - Allanbank Consulting, Inc.
4    * %%
5    * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.allanbank.mongodb.util;
21  
22  import java.util.regex.Pattern;
23  import java.util.regex.PatternSyntaxException;
24  
25  /**
26   * PatternUtils provides utilities for handling patterns.
27   * 
28   * @api.no This class is <b>NOT</b> part of the drivers API. This class may be
29   *         mutated in incompatible ways between any two releases of the driver.
30   * @copyright 2012-2013, Allanbank Consulting, Inc., All Rights Reserved
31   */
32  public final class PatternUtils {
33      /** The regular expression matching all input. */
34      public static final String ALL = ".*";
35  
36      /** The pattern for the regular expression matching all input. */
37      public static final Pattern ALL_PATTERN = Pattern.compile(ALL);
38  
39      /**
40       * Converts the Regular Expression string into a {@link Pattern}.
41       * 
42       * @param regex
43       *            The regular expression to convert.
44       * @return The {@link Pattern} for the regular expression.
45       * @throws PatternSyntaxException
46       *             If the expression's syntax is invalid.
47       */
48      public static Pattern toPattern(final String regex)
49              throws PatternSyntaxException {
50          if (ALL.equals(regex)) {
51              return ALL_PATTERN;
52          }
53          return Pattern.compile(regex);
54      }
55  
56      /**
57       * Creates a new PatternUtils.
58       */
59      private PatternUtils() {
60          // Nothing.
61      }
62  }