Skip to content

Commit

Permalink
refactor($Util): avoid PMD warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnymillergh committed Feb 27, 2020
1 parent 93927ba commit 151e16e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
import java.util.regex.Pattern;

/**
* <h1>CaseConverter</h1>
* <h1>CaseConversionUtil</h1>
* The text conversion class helps changing the cases of an existing text. Get
* more information and online tools for this implementation on: <a href=
* "https://en.toolpage.org/cat/text-conversion">https://en.toolpage.org/cat/text-conversion</a>
*
* @author Yves Sorge <yves.sorge@toolpage.org>
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com
* @date 1/16/20 8:57 AM
* @author Johnny Miller (鍾俊), e-mail: johnnysviva@outlook.com
* @date 2/27/20 9:45 AM
* @see
* <a href='https://github.com/toolpage/java-case-converter/blob/master/src/org/toolpage/util/text/CaseConverter.java'>Source</a>
*/
@SuppressWarnings("unused")
public class CaseConverter {
public class CaseConversionUtil {
/**
* Filter non-alphabet and non-number string.
*
* @param value the value
* @return filtered string.
*/
public static String filterNonAlphabetAndNonNumber(String value) {
public static String filterNonAlphabetAndNonNumber(final String value) {
return value.replaceAll("[^0-9a-zA-Z]", "");
}

Expand All @@ -34,7 +34,7 @@ public static String filterNonAlphabetAndNonNumber(String value) {
* @param value the value
* @return converted string.
*/
public static String convertToUpperCase(String value) {
public static String convertToUpperCase(final String value) {
return value.toUpperCase();
}

Expand All @@ -45,7 +45,7 @@ public static String convertToUpperCase(String value) {
* @param value the value
* @return converted string.
*/
public static String convertToLowerCase(String value) {
public static String convertToLowerCase(final String value) {
return value.toLowerCase();
}

Expand All @@ -55,11 +55,11 @@ public static String convertToLowerCase(String value) {
* @param value the value
* @return converted string.
*/
public static String convertToStartCase(String value) {
public static String convertToStartCase(final String value) {
StringBuilder returnValue = new StringBuilder();
value = value.toLowerCase();
var lowerCaseValue = value.toLowerCase();
boolean makeNextUppercase = true;
for (char c : value.toCharArray()) {
for (char c : lowerCaseValue.toCharArray()) {
if (Character.isSpaceChar(c) || Character.isWhitespace(c) || "()[]{}\\/".indexOf(c) != -1) {
makeNextUppercase = true;
} else if (makeNextUppercase) {
Expand All @@ -79,8 +79,8 @@ public static String convertToStartCase(String value) {
* @param value the value
* @return converted string.
*/
public static String convertToAlternatingCase(String value) {
return CaseConverter.convertToAlternatingCase(value, true);
public static String convertToAlternatingCase(final String value) {
return CaseConversionUtil.convertToAlternatingCase(value, true);
}

/**
Expand All @@ -92,19 +92,19 @@ public static String convertToAlternatingCase(String value) {
* @param startWithCapitalLetter the start with capital letter
* @return converted string.
*/
public static String convertToAlternatingCase(String value, boolean startWithCapitalLetter) {
public static String convertToAlternatingCase(final String value, boolean startWithCapitalLetter) {
StringBuilder returnValue = new StringBuilder();
value = value.toLowerCase();
int i = 0;
var lowerCaseValue = value.toLowerCase();
int index = 0;
if (!startWithCapitalLetter) {
i = 1;
index = 1;
}
for (char c : value.toCharArray()) {
for (char c : lowerCaseValue.toCharArray()) {
if (Character.isAlphabetic(c)) {
if (i % 2 == 0) {
if (index % 2 == 0) {
c = Character.toUpperCase(c);
}
i++;
index++;
}
returnValue.append(c);
}
Expand All @@ -118,11 +118,11 @@ public static String convertToAlternatingCase(String value, boolean startWithCap
* @param value the value
* @return converted string.
*/
public static String convertToCamelCase(String value) {
public static String convertToCamelCase(final String value) {
String throwAwayChars = "()[]{}=?!.:,-_+\\\"#~/";
value = value.replaceAll("[" + Pattern.quote(throwAwayChars) + "]", " ");
value = CaseConverter.convertToStartCase(value);
return value.replaceAll("\\s+", "");
var replacedValue = value.replaceAll("[" + Pattern.quote(throwAwayChars) + "]", " ");
replacedValue = CaseConversionUtil.convertToStartCase(replacedValue);
return replacedValue.replaceAll("\\s+", "");
}

/**
Expand All @@ -132,11 +132,11 @@ public static String convertToCamelCase(String value) {
* @param value the value
* @return converted string.
*/
public static String convertToSnakeCase(String value) {
public static String convertToSnakeCase(final String value) {
String throwAwayChars = "()[]{}=?!.:,-_+\\\"#~/";
value = value.replaceAll("[" + Pattern.quote(throwAwayChars) + "]", " ");
value = CaseConverter.convertToStartCase(value);
return value.trim().replaceAll("\\s+", "_");
var replacedValue = value.replaceAll("[" + Pattern.quote(throwAwayChars) + "]", " ");
replacedValue = CaseConversionUtil.convertToStartCase(replacedValue);
return replacedValue.trim().replaceAll("\\s+", "_");
}

/**
Expand All @@ -146,11 +146,11 @@ public static String convertToSnakeCase(String value) {
* @param value the value
* @return converted string.
*/
public static String convertToKebabCase(String value) {
public static String convertToKebabCase(final String value) {
String throwAwayChars = "()[]{}=?!.:,-_+\\\"#~/";
value = value.replaceAll("[" + Pattern.quote(throwAwayChars) + "]", " ");
value = value.toLowerCase();
return value.trim().replaceAll("\\s+", "-");
var replacedValue = value.replaceAll("[" + Pattern.quote(throwAwayChars) + "]", " ");
replacedValue = replacedValue.toLowerCase();
return replacedValue.trim().replaceAll("\\s+", "-");
}

/**
Expand All @@ -161,14 +161,14 @@ public static String convertToKebabCase(String value) {
* @param value the value
* @return converted string.
*/
public static String convertToStudlyCaps(String value) {
public static String convertToStudlyCaps(final String value) {
StringBuilder returnValue = new StringBuilder();
value = value.toLowerCase();
var lowerCaseValue = value.toLowerCase();
int numOfFollowingUppercase = 0;
int numOfFollowingLowercase = 0;
boolean doCapitalLetter;
Random randomizer = new Random();
for (char c : value.toCharArray()) {
for (char c : lowerCaseValue.toCharArray()) {
if (Character.isAlphabetic(c)) {
if (numOfFollowingUppercase < 2) {
if (randomizer.nextInt(100) % 2 == 0) {
Expand Down Expand Up @@ -208,7 +208,7 @@ public static String convertToStudlyCaps(String value) {
* @param value the value
* @return converted string.
*/
public static String invertCase(String value) {
public static String invertCase(final String value) {
StringBuilder returnValue = new StringBuilder();
for (char c : value.toCharArray()) {
if (Character.isAlphabetic(c)) {
Expand Down
31 changes: 16 additions & 15 deletions common/src/main/java/com/jmsoftware/common/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
* <h1>FileUtil</h1>
* <p>Change description here</p>
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com
* @date 2019-07-04 20:12
* @author Johnny Miller (鍾俊), e-mail: johnnysviva@outlook.com
* @date 2/27/20 9:45 AM
**/
@Slf4j
@SuppressWarnings("unused")
public class FileUtil {
/**
* Convert multipart file to file
Expand All @@ -26,10 +27,10 @@ public class FileUtil {
* @return file
* @throws IOException IO exception
*/
public static File convertFrom(MultipartFile multipartFile) throws IOException {
public static File convertFrom(final MultipartFile multipartFile) throws IOException {
log.info("Converting multipart file, original multipart file name: {}", multipartFile.getOriginalFilename());
File convertFile = new File(Objects.requireNonNull(multipartFile.getOriginalFilename()));
FileOutputStream fos = new FileOutputStream(convertFile);
var convertFile = new File(Objects.requireNonNull(multipartFile.getOriginalFilename()));
var fos = new FileOutputStream(convertFile);
fos.write(multipartFile.getBytes());
fos.close();
return convertFile;
Expand All @@ -42,9 +43,9 @@ public static File convertFrom(MultipartFile multipartFile) throws IOException {
* @param savePath path for saving file
* @return file
*/
public static File convertFrom(InputStream inputStream, String savePath) {
public static File convertFrom(final InputStream inputStream, final String savePath) {
log.info("Converting InputStream to file, save path: {}", savePath);
File file = new File(savePath);
var file = new File(savePath);
try (OutputStream outputStream = new FileOutputStream(file)) {
int read;
byte[] bytes = new byte[1024];
Expand All @@ -71,11 +72,11 @@ public static File convertFrom(InputStream inputStream, String savePath) {
* @param sftpSubDirectory SFTP server's sub directory
* @return full storage path (absolute path). Null if file name is empty.
*/
public static String generateDateFormatStoragePath(String sftpSubDirectory) {
Date today = new Date();
String year = DateUtil.format(today, "yyyy");
String month = DateUtil.format(today, "MM");
String day = DateUtil.format(today, "dd");
public static String generateDateFormatStoragePath(final String sftpSubDirectory) {
var today = new Date();
var year = DateUtil.format(today, "yyyy");
var month = DateUtil.format(today, "MM");
var day = DateUtil.format(today, "dd");
return sftpSubDirectory
+ year
+ "/" + month
Expand All @@ -89,12 +90,12 @@ public static String generateDateFormatStoragePath(String sftpSubDirectory) {
* @param file file
* @return unique file name (UUID.fileExtension)
*/
public static String generateUniqueFileName(File file) {
String fileName = file.getName();
public static String generateUniqueFileName(final File file) {
var fileName = file.getName();
if (StringUtils.isBlank(fileName)) {
return null;
}
String fileExtension = fileName.substring(fileName.lastIndexOf("."));
var fileExtension = fileName.substring(fileName.lastIndexOf("."));
return UUID.randomUUID() + "." + fileExtension;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* <p>
* Request util.
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com
* @date 2019-03-02 20:00
* @author Johnny Miller (鍾俊), e-mail: johnnysviva@outlook.com
* @date 2/27/20 9:45 AM
**/
public class RequestUtil {
private static final String UNKNOWN = "unknown";
Expand All @@ -23,9 +23,9 @@ public class RequestUtil {
* @param request HTTP request.
* @return user's IP and port information.
*/
public static String getRequestIpAndPort(HttpServletRequest request) {
public static String getRequestIpAndPort(final HttpServletRequest request) {
var ip = request.getHeader("X-Real-IP");
var port = request.getRemotePort();
final var port = request.getRemotePort();
if (!StringUtils.isBlank(ip) && !"".equalsIgnoreCase(ip)) {
return ip;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
* <p>
* Response util
*
* @author Johnny Miller (鍾俊), email: johnnysviva@outlook.com
* @date 2019-03-23 18:02
* @author Johnny Miller (鍾俊), e-mail: johnnysviva@outlook.com
* @date 2/27/20 9:45 AM
**/
@Slf4j
@SuppressWarnings("unused")
public class ResponseUtil {
private static final ObjectMapper MAPPER = new ObjectMapper();

Expand All @@ -28,7 +29,7 @@ public class ResponseUtil {
* @param httpStatus HTTP status
* @param data Data
*/
public static void renderJson(HttpServletResponse response, HttpStatus httpStatus, Object data) {
public static void renderJson(final HttpServletResponse response, final HttpStatus httpStatus, final Object data) {
try {
ResponseBodyBean<Object> responseBodyBean = ResponseBodyBean.ofStatus(httpStatus.getCode(),
httpStatus.getMessage(),
Expand All @@ -49,7 +50,7 @@ public static void renderJson(HttpServletResponse response, HttpStatus httpStatu
* @param response Response
* @param exception Exception
*/
public static void renderJson(HttpServletResponse response, BaseException exception) {
public static void renderJson(final HttpServletResponse response, final BaseException exception) {
try {
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "*");
Expand Down

0 comments on commit 151e16e

Please sign in to comment.