bcea351e30eaf308cb6ff0da5839caddc0b5c656
[netvirt.git] / qosservice / impl / src / main / java / org / opendaylight / netvirt / qosservice / UuidUtil.java
1 /*
2  * Copyright (c) 2017 Red Hat, 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.netvirt.qosservice;
9
10 import com.google.common.base.Optional;
11 import com.google.common.base.Preconditions;
12 import java.util.regex.Pattern;
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
14
15 /**
16  * Utility for {@link Uuid}.
17  * This class is ThreadSafe.
18  * @author Michael Vorburger.ch
19  */
20 class UuidUtil {
21
22     private Pattern uuidPattern;
23
24     Optional<Uuid> newUuidIfValidPattern(String possibleUuid) {
25         Preconditions.checkNotNull(possibleUuid, "possibleUuid == null");
26
27         if (uuidPattern == null) {
28             // Thread safe because it really doesn't matter even if we were to do this initialization more than once
29             if (Uuid.PATTERN_CONSTANTS.size() != 1) {
30                 throw new IllegalStateException("Uuid.PATTERN_CONSTANTS.size() != 1");
31             }
32             uuidPattern = Pattern.compile(Uuid.PATTERN_CONSTANTS.get(0));
33         }
34
35         if (uuidPattern.matcher(possibleUuid).matches()) {
36             return Optional.of(new Uuid(possibleUuid));
37         } else {
38             return Optional.absent();
39         }
40     }
41 }