2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.codec;
10 import static com.google.common.base.Preconditions.checkArgument;
12 import com.google.common.base.Strings;
13 import java.util.regex.Pattern;
14 import org.opendaylight.yangtools.yang.model.api.type.PatternConstraint;
16 class CompiledPatternContext {
18 private final Pattern pattern;
19 private final String errorMessage;
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() + ">";
27 errorMessage = yangMessage;
31 public void validate(final String str) {
32 checkArgument(pattern.matcher(str).matches(), errorMessage, str);