NoddeWithValue is generic
[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 com.google.common.base.Preconditions;
11 import com.google.common.base.Strings;
12 import java.util.regex.Pattern;
13 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
14
15 class CompiledPatternContext {
16
17     private final Pattern pattern;
18     private final String errorMessage;
19
20     CompiledPatternContext(final PatternConstraint yangConstraint) {
21         pattern = Pattern.compile("^" + yangConstraint.getRegularExpression() + "$");
22         final String yangMessage = yangConstraint.getErrorMessage();
23         if (Strings.isNullOrEmpty(yangMessage)) {
24             errorMessage = "Value %s does not match regular expression <" + pattern.pattern() + ">";
25         } else {
26             errorMessage = yangMessage;
27         }
28     }
29
30     public void validate(final String s) {
31         Preconditions.checkArgument(pattern.matcher(s).matches(), errorMessage, s);
32     }
33
34 }