Import atomix/{storage,utils}
[controller.git] / third-party / atomix / utils / src / main / java / io / atomix / utils / misc / StringUtils.java
1 /*
2  * Copyright 2019-present Open Networking Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package io.atomix.utils.misc;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 /**
22  * Collection of various helper methods to manipulate strings.
23  */
24 public final class StringUtils {
25
26   private StringUtils() {
27   }
28
29   /**
30    * Splits the input string with the given regex and filters empty strings.
31    *
32    * @param input the string to split.
33    * @return the array of strings computed by splitting this string
34    */
35   public static String[] split(String input, String regex) {
36     if (input == null) {
37       return null;
38     }
39     String[] arr = input.split(regex);
40     List<String> results = new ArrayList<>(arr.length);
41     for (String a : arr) {
42       if (!a.trim().isEmpty()) {
43         results.add(a);
44       }
45     }
46     return results.toArray(new String[0]);
47   }
48 }