Cleanup use of Guava library
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / codec / CompiledPatternContext.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.data.impl.codec;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.base.Strings;
13 import java.util.regex.Pattern;
14 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
15
16 class CompiledPatternContext {
17
18     private final Pattern pattern;
19     private final String errorMessage;
20
21     CompiledPatternContext(final PatternConstraint yangConstraint) {
22         pattern = Pattern.compile("^" + yangConstraint.getRegularExpression() + "$");
23         final String yangMessage = yangConstraint.getErrorMessage();
24         if (Strings.isNullOrEmpty(yangMessage)) {
25             errorMessage = "Value %s does not match regular expression <" + pattern.pattern() + ">";
26         } else {
27             errorMessage = yangMessage;
28         }
29     }
30
31     public void validate(final String str) {
32         checkArgument(pattern.matcher(str).matches(), errorMessage, str);
33     }
34 }