8546f9a9290646929c7580e016fbf6a65ab0cdf7
[controller.git] / opendaylight / md-sal / messagebus-util / src / main / java / org / opendaylight / controller / messagebus / app / util / Util.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
9 package org.opendaylight.controller.messagebus.app.util;
10
11 import com.google.common.util.concurrent.Futures;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.regex.Pattern;
16 import org.opendaylight.yangtools.yang.common.RpcResult;
17 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
19
20 @Deprecated(forRemoval = true)
21 public final class Util {
22     private Util() {
23     }
24
25     public static <T> ListenableFuture<RpcResult<T>> resultRpcSuccessFor(final T output) {
26         return Futures.immediateFuture(RpcResultBuilder.success(output).build());
27     }
28
29     /**
30      * Method filters qnames based on wildcard strings.
31      *
32      * @param list list of SchemaPaths
33      * @param pattern matching pattern
34      * @return list of filtered qnames
35      */
36     public static List<SchemaPath> expandQname(final List<SchemaPath> list, final Pattern pattern) {
37         final List<SchemaPath> matchingQnames = new ArrayList<>();
38
39         for (final SchemaPath notification : list) {
40             final String namespace = notification.getLastComponent().getNamespace().toString();
41             if (pattern.matcher(namespace).matches()) {
42                 matchingQnames.add(notification);
43             }
44         }
45         return matchingQnames;
46     }
47
48     /**
49      * CREDIT to http://www.rgagnon.com/javadetails/java-0515.html.
50      */
51     public static String wildcardToRegex(final String wildcard) {
52         final StringBuilder s = new StringBuilder(wildcard.length());
53         s.append('^');
54         for (final char c : wildcard.toCharArray()) {
55             switch (c) {
56                 case '*':
57                     s.append(".*");
58                     break;
59                 case '?':
60                     s.append('.');
61                     break;
62                 // escape special regexp-characters
63                 case '(':
64                 case ')':
65                 case '[':
66                 case ']':
67                 case '$':
68                 case '^':
69                 case '.':
70                 case '{':
71                 case '}':
72                 case '|':
73                 case '\\':
74                     s.append("\\");
75                     s.append(c);
76                     break;
77                 default:
78                     s.append(c);
79                     break;
80             }
81         }
82         s.append('$');
83         return s.toString();
84     }
85 }