1 /******************************************************************************* 2 * Copyright (c) 2008, 2017 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 *******************************************************************************/ 14 package org.eclipse.core.internal.net; 15 16 import java.util.ArrayList; 17 18 import org.eclipse.core.text.StringMatcher; 19 20 public class StringUtil { 21 22 /** 23 * Splits a string into substrings using a delimiter array. 24 * 25 * @param value 26 * string to be tokenized 27 * @param delimiters 28 * array of delimiters 29 * @return array of tokens 30 */ split(String value, String[] delimiters)31 public static String[] split(String value, String[] delimiters) { 32 ArrayList<String> result = new ArrayList<>(); 33 int firstIndex = 0; 34 int separator = 0; 35 while (firstIndex != -1) { 36 firstIndex = -1; 37 for (int i = 0; i < delimiters.length; i++) { 38 int index = value.indexOf(delimiters[i]); 39 if (index != -1 && (index < firstIndex || firstIndex == -1)) { 40 firstIndex = index; 41 separator = i; 42 } 43 } 44 if (firstIndex != -1) { 45 if (firstIndex != 0) { 46 result.add(value.substring(0, firstIndex)); 47 } 48 int newStart = firstIndex + delimiters[separator].length(); 49 if (newStart <= value.length()) { 50 value = value.substring(newStart); 51 } 52 } else if (value.length() > 0) { 53 result.add(value); 54 } 55 } 56 return result.toArray(new String[0]); 57 } 58 59 /** 60 * Tests equality of the given strings. 61 * 62 * @param sequence1 63 * candidate 1, may be null 64 * @param sequence2 65 * candidate 2, may be null 66 * @return true if both sequences are null or the sequences are equal 67 */ equals(final CharSequence sequence1, final CharSequence sequence2)68 public static final boolean equals(final CharSequence sequence1, 69 final CharSequence sequence2) { 70 if (sequence1 == sequence2) { 71 return true; 72 } else if (sequence1 == null || sequence2 == null) { 73 return false; 74 } 75 return sequence1.equals(sequence2); 76 } 77 hostMatchesFilter(String host, String filter)78 public static boolean hostMatchesFilter(String host, String filter) { 79 String suffixMatchingFilter = "*" + filter; //$NON-NLS-1$ 80 StringMatcher matcher = new StringMatcher(suffixMatchingFilter, true, false); 81 return matcher.match(host); 82 } 83 84 }