io.github.nichetoolkit.rest.util.NameUtils Maven / Gradle / Ivy
Show all versions of rest-toolkit-utils Show documentation
package io.github.nichetoolkit.rest.util;
import java.util.HashMap;
import java.util.Map;
/**
* NameUtils
* The name utils class.
* @author Cyan ([email protected])
* @since Jdk1.8
*/
public class NameUtils {
/**
* UUID
* {@link java.lang.String} The constant UUID
field.
* @see java.lang.String
*/
public static final String UUID = "uuid";
/**
* NAME
* {@link java.lang.String} The constant NAME
field.
* @see java.lang.String
*/
public static final String NAME = "name";
/**
* EXT
* {@link java.lang.String} The constant EXT
field.
* @see java.lang.String
*/
public static final String EXT = "ext";
/**
* parseFileName
* The parse file name method.
* @param fileNameString {@link java.lang.String} The file name string parameter is String
type.
* @return {@link java.util.Map} The parse file name return object is Map
type.
* @see java.lang.String
* @see java.util.Map
*/
public static Map parseFileName(String fileNameString) {
Map fileNameMap = new HashMap<>();
String[] result = new String[2];
if (fileNameString.contains(".")) {
String[] stringTemp = fileNameString.split("\\.", -1);
/* 最后一个必是扩展名 */
result[1] = stringTemp[stringTemp.length - 1];
if (stringTemp.length > 2) {
StringBuilder nameBuffer = new StringBuilder();
for (int i = 0; i < stringTemp.length - 1; i++) {
nameBuffer.append(".").append(stringTemp[i]);
}
result[0] = nameBuffer.substring(1);
} else {
result[0] = stringTemp[0];
}
} else {
result[0] = fileNameString;
result[1] = null;
}
fileNameMap.put(NAME, result[0]);
fileNameMap.put(EXT, result[1]);
return fileNameMap;
}
/**
* buildFileName
* The build file name method.
* @param name {@link java.lang.String} The name parameter is String
type.
* @param ext {@link java.lang.String} The ext parameter is String
type.
* @return {@link java.lang.String} The build file name return object is String
type.
* @see java.lang.String
*/
public static String buildFileName(String name, String ext) {
return name + "." + ext;
}
/**
* buildFileName
* The build file name method.
* @param fileNameMap {@link java.util.Map} The file name map parameter is Map
type.
* @return {@link java.lang.String} The build file name return object is String
type.
* @see java.util.Map
* @see java.lang.String
*/
public static String buildFileName(Map fileNameMap) {
String name = fileNameMap.get(NAME);
String ext = fileNameMap.get(EXT);
return buildFileName(name, ext);
}
/**
* parseAliasName
* The parse alias name method.
* @param fileNameString {@link java.lang.String} The file name string parameter is String
type.
* @return {@link java.util.Map} The parse alias name return object is Map
type.
* @see java.lang.String
* @see java.util.Map
* @see java.lang.SuppressWarnings
*/
@SuppressWarnings("UnnecessaryParentheses")
public static Map parseAliasName(String fileNameString) {
Map aliasNameMap = new HashMap<>();
Map nameMap = parseFileName(fileNameString);
String fileName = nameMap.get(NAME);
String[] result = new String[2];
if (fileName.contains("_")) {
String[] stringTemp = fileName.split("_", -1);
/* 第一个必是也必须是ID */
result[0] = stringTemp[0];
if (stringTemp.length > 2) {
StringBuilder nameBuffer = new StringBuilder();
for (int i = 1; i < stringTemp.length; i++) {
nameBuffer.append("_").append(stringTemp[i]);
}
result[1] = nameBuffer.substring(1);
} else {
result[1] = stringTemp[1];
}
} else {
result[0] = null;
result[1] = fileName;
}
aliasNameMap.put(UUID, result[0]);
aliasNameMap.put(NAME, result[1]);
aliasNameMap.put(EXT, nameMap.get(EXT));
return aliasNameMap;
}
/**
* buildAliasName
* The build alias name method.
* @param name {@link java.lang.String} The name parameter is String
type.
* @param ext {@link java.lang.String} The ext parameter is String
type.
* @return {@link java.lang.String} The build alias name return object is String
type.
* @see java.lang.String
*/
public static String buildAliasName(String name, String ext) {
String uuid = GeneralUtils.uuid().replaceAll("-","");
return uuid + "_" + name + "." + ext;
}
/**
* parseSliceName
* The parse slice name method.
* @param sliceNameString {@link java.lang.String} The slice name string parameter is String
type.
* @return {@link java.util.Map} The parse slice name return object is Map
type.
* @see java.lang.String
* @see java.util.Map
*/
public static Map parseSliceName(String sliceNameString) {
Map sliceMap = new HashMap<>();
String[] result = new String[2];
if (sliceNameString.contains("_")) {
String[] stringTemp = sliceNameString.split("_", -1);
/* 最后一个必是也必须是uuid */
result[1] = stringTemp[stringTemp.length - 1];
if (stringTemp.length > 2) {
StringBuilder nameBuffer = new StringBuilder();
for (int i = 0; i < stringTemp.length - 1; i++) {
nameBuffer.append("_").append(stringTemp[i]);
}
result[0] = nameBuffer.substring(1);
} else {
result[0] = stringTemp[0];
result[1] = null;
}
}
sliceMap.put(UUID, result[0]);
sliceMap.put(NAME, result[1]);
return sliceMap;
}
/**
* parseCamelName
* The parse camel name method.
* @param baseString {@link java.lang.String} The base string parameter is String
type.
* @return {@link java.lang.String} The parse camel name return object is String
type.
* @see java.lang.String
*/
public static String parseCamelName(String baseString) {
StringBuilder caseBuilder = new StringBuilder();
boolean isUpper = false;
for (int i = 0; i < baseString.length(); i++) {
char character = baseString.charAt(i);
switch (character) {
case '_':
case '-':
case '@':
case '$':
case '#':
case ' ':
case '/':
case '&':
isUpper = caseBuilder.length() > 0;
break;
default:
if (isUpper) {
character = Character.toUpperCase(character);
isUpper = false;
}
caseBuilder.append(character);
break;
}
}
caseBuilder.setCharAt(0, Character.toUpperCase(caseBuilder.charAt(0)));
return caseBuilder.toString();
}
}