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