Tboy
5 years ago
committed by
qiaozhanwei
20 changed files with 170 additions and 31 deletions
@ -0,0 +1,33 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||||
|
* (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.apache.dolphinscheduler.common.utils; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public class EnumUtils { |
||||||
|
|
||||||
|
public static <E extends Enum<E>> E getEnum(final Class<E> enumClass, final String enumName) { |
||||||
|
if (enumName == null) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
try { |
||||||
|
return Enum.valueOf(enumClass, enumName); |
||||||
|
} catch (final IllegalArgumentException ex) { |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,125 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
||||||
|
* (the "License"); you may not use this file except in compliance with |
||||||
|
* the License. You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
*/ |
||||||
|
package org.apache.dolphinscheduler.common.utils; |
||||||
|
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets; |
||||||
|
import java.util.regex.Pattern; |
||||||
|
|
||||||
|
|
||||||
|
public class StringUtils { |
||||||
|
|
||||||
|
public static final int INDEX_NOT_FOUND = -1; |
||||||
|
|
||||||
|
public static final String EMPTY = ""; |
||||||
|
|
||||||
|
public static boolean isEmpty(final CharSequence cs) { |
||||||
|
return cs == null || cs.length() == 0; |
||||||
|
} |
||||||
|
|
||||||
|
public static boolean isNotEmpty(final CharSequence cs) { |
||||||
|
return !isEmpty(cs); |
||||||
|
} |
||||||
|
|
||||||
|
public static boolean isBlank(CharSequence cs){ |
||||||
|
int strLen; |
||||||
|
if (cs == null || (strLen = cs.length()) == 0) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
for (int i = 0; i < strLen; i++) { |
||||||
|
if (Character.isWhitespace(cs.charAt(i)) == false) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
public static boolean isNotBlank(CharSequence str){ |
||||||
|
return !isBlank(str); |
||||||
|
} |
||||||
|
|
||||||
|
public static String substringBefore(final String str, final String separator) { |
||||||
|
if (isBlank(str) || separator == null) { |
||||||
|
return str; |
||||||
|
} |
||||||
|
if (separator.isEmpty()) { |
||||||
|
return EMPTY; |
||||||
|
} |
||||||
|
final int pos = str.indexOf(separator); |
||||||
|
if (pos == INDEX_NOT_FOUND) { |
||||||
|
return str; |
||||||
|
} |
||||||
|
return str.substring(0, pos); |
||||||
|
} |
||||||
|
|
||||||
|
public static String substringAfter(final String str, final String separator) { |
||||||
|
if (isBlank(str)) { |
||||||
|
return str; |
||||||
|
} |
||||||
|
if (separator == null) { |
||||||
|
return EMPTY; |
||||||
|
} |
||||||
|
final int pos = str.indexOf(separator); |
||||||
|
if (pos == INDEX_NOT_FOUND) { |
||||||
|
return EMPTY; |
||||||
|
} |
||||||
|
return str.substring(pos + separator.length()); |
||||||
|
} |
||||||
|
|
||||||
|
public static String substringAfterLast(final String str, final String separator) { |
||||||
|
if (isEmpty(str)) { |
||||||
|
return str; |
||||||
|
} |
||||||
|
if (isEmpty(separator)) { |
||||||
|
return EMPTY; |
||||||
|
} |
||||||
|
final int pos = str.lastIndexOf(separator); |
||||||
|
if (pos == INDEX_NOT_FOUND || pos == str.length() - separator.length()) { |
||||||
|
return EMPTY; |
||||||
|
} |
||||||
|
return str.substring(pos + separator.length()); |
||||||
|
} |
||||||
|
|
||||||
|
public static String getUtf8String(byte[] bytes){ |
||||||
|
return new String(bytes, StandardCharsets.UTF_8); |
||||||
|
} |
||||||
|
|
||||||
|
public static byte[] getUtf8Bytes(String str){ |
||||||
|
return str.getBytes(StandardCharsets.UTF_8); |
||||||
|
} |
||||||
|
|
||||||
|
public static boolean hasChinese(String str) { |
||||||
|
if (str == null) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
Pattern pattern = Pattern.compile("[\\u4E00-\\u9FBF]+"); |
||||||
|
return pattern.matcher(str).find(); |
||||||
|
} |
||||||
|
|
||||||
|
public static boolean hasSpace(String str) { |
||||||
|
if (str == null) { |
||||||
|
return false; |
||||||
|
} |
||||||
|
int len = str.length(); |
||||||
|
for (int i = 0; i < len; i++) { |
||||||
|
if (str.charAt(i) == ' ') { |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue