Merge all StringUtil into a single class.
This commit is contained in:
@@ -19,6 +19,7 @@ import java.util.TreeMap;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.springframework.ide.vscode.util.FuzzyMatcher;
|
||||
import org.springframework.ide.vscode.util.StringUtil;
|
||||
|
||||
/**
|
||||
* A collection of data that can be searched with a simple 'fuzzy' string
|
||||
|
||||
@@ -1,132 +0,0 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2015 Pivotal, Inc.
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* which accompanies this distribution, and is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
*
|
||||
* Contributors:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.boot.properties.util;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class StringUtil {
|
||||
|
||||
public static boolean hasText(String name) {
|
||||
return name!=null && !name.trim().equals("");
|
||||
}
|
||||
|
||||
public static String trim(String s) {
|
||||
if (s!=null) {
|
||||
return s.trim();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String trimEnd(String s) {
|
||||
if (s!=null) {
|
||||
return s.replaceAll("\\s+\\z", "");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static int commonPrefixLength(CharSequence s, CharSequence t) {
|
||||
int shortestStringLen = Math.min(s.length(), t.length());
|
||||
for (int i = 0; i < shortestStringLen; i++) {
|
||||
if (s.charAt(i)!=t.charAt(i)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
//no difference found upto entire length of shortest string.
|
||||
return shortestStringLen;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return longest string which is a prefix of both argument Strings.
|
||||
*/
|
||||
public static String commonPrefix(CharSequence s, CharSequence t) {
|
||||
int len = commonPrefixLength(s, t);
|
||||
if (len>0) {
|
||||
return s.subSequence(0,len).toString();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String camelCaseToHyphens(String value) {
|
||||
Matcher matcher = CAMEL_CASE_PATTERN.matcher(value);
|
||||
StringBuffer result = new StringBuffer();
|
||||
while (matcher.find()) {
|
||||
matcher.appendReplacement(result, matcher.group(1) + '-'
|
||||
+ matcher.group(2).toLowerCase());
|
||||
}
|
||||
matcher.appendTail(result);
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
private static final Pattern CAMEL_CASE_PATTERN = Pattern.compile("([^A-Z-])([A-Z])");
|
||||
|
||||
public static String arrayToCommaDelimitedString(Object[] array) {
|
||||
return collectionToCommaDelimitedString(Arrays.asList(array));
|
||||
}
|
||||
|
||||
public static String collectionToCommaDelimitedString(Collection<?> items) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
boolean first = true;
|
||||
for (Object item : items) {
|
||||
if (!first) {
|
||||
buf.append(",");
|
||||
}
|
||||
buf.append(item);
|
||||
first = false;
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public static String upperCaseToHyphens(String v) {
|
||||
if (v!=null) {
|
||||
return v.toLowerCase().replace('_', '-');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String hyphensToUpperCase(String v) {
|
||||
if (v!=null) {
|
||||
return v.toUpperCase().replace('-', '_');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String hyphensToCamelCase(String propName, boolean startWithUpperCase) {
|
||||
String [] parts = propName.split("-");
|
||||
if (startWithUpperCase) {
|
||||
parts[0] = upCaseFirstChar(parts[0]);
|
||||
}
|
||||
StringBuilder camelCased = new StringBuilder(parts[0]);
|
||||
for (int i = 1; i < parts.length; i++) {
|
||||
camelCased.append(upCaseFirstChar(parts[i]));
|
||||
}
|
||||
return camelCased.toString();
|
||||
}
|
||||
|
||||
|
||||
public static String upCaseFirstChar(String string) {
|
||||
if (StringUtil.hasText(string)) {
|
||||
return Character.toUpperCase(string.charAt(0)) + string.substring(1);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String datestamp() {
|
||||
Date d = new Date();
|
||||
SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
|
||||
return f.format(d);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,12 @@
|
||||
package org.springframework.ide.vscode.util;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class StringUtil {
|
||||
public static boolean hasText(String name) {
|
||||
return name!=null && !name.trim().equals("");
|
||||
@@ -18,6 +25,13 @@ public class StringUtil {
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
public static String trim(String s) {
|
||||
if (s!=null) {
|
||||
return s.trim();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String trimEnd(String s) {
|
||||
if (s!=null) {
|
||||
return s.replaceAll("\\s+\\z", "");
|
||||
@@ -25,4 +39,96 @@ public class StringUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static int commonPrefixLength(CharSequence s, CharSequence t) {
|
||||
int shortestStringLen = Math.min(s.length(), t.length());
|
||||
for (int i = 0; i < shortestStringLen; i++) {
|
||||
if (s.charAt(i)!=t.charAt(i)) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
//no difference found upto entire length of shortest string.
|
||||
return shortestStringLen;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return longest string which is a prefix of both argument Strings.
|
||||
*/
|
||||
public static String commonPrefix(CharSequence s, CharSequence t) {
|
||||
int len = commonPrefixLength(s, t);
|
||||
if (len>0) {
|
||||
return s.subSequence(0,len).toString();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String camelCaseToHyphens(String value) {
|
||||
Matcher matcher = CAMEL_CASE_PATTERN.matcher(value);
|
||||
StringBuffer result = new StringBuffer();
|
||||
while (matcher.find()) {
|
||||
matcher.appendReplacement(result, matcher.group(1) + '-'
|
||||
+ matcher.group(2).toLowerCase());
|
||||
}
|
||||
matcher.appendTail(result);
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
private static final Pattern CAMEL_CASE_PATTERN = Pattern.compile("([^A-Z-])([A-Z])");
|
||||
|
||||
public static String arrayToCommaDelimitedString(Object[] array) {
|
||||
return collectionToCommaDelimitedString(Arrays.asList(array));
|
||||
}
|
||||
|
||||
public static String collectionToCommaDelimitedString(Collection<?> items) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
boolean first = true;
|
||||
for (Object item : items) {
|
||||
if (!first) {
|
||||
buf.append(",");
|
||||
}
|
||||
buf.append(item);
|
||||
first = false;
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public static String upperCaseToHyphens(String v) {
|
||||
if (v!=null) {
|
||||
return v.toLowerCase().replace('_', '-');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String hyphensToUpperCase(String v) {
|
||||
if (v!=null) {
|
||||
return v.toUpperCase().replace('-', '_');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String hyphensToCamelCase(String propName, boolean startWithUpperCase) {
|
||||
String [] parts = propName.split("-");
|
||||
if (startWithUpperCase) {
|
||||
parts[0] = upCaseFirstChar(parts[0]);
|
||||
}
|
||||
StringBuilder camelCased = new StringBuilder(parts[0]);
|
||||
for (int i = 1; i < parts.length; i++) {
|
||||
camelCased.append(upCaseFirstChar(parts[i]));
|
||||
}
|
||||
return camelCased.toString();
|
||||
}
|
||||
|
||||
|
||||
public static String upCaseFirstChar(String string) {
|
||||
if (StringUtil.hasText(string)) {
|
||||
return Character.toUpperCase(string.charAt(0)) + string.substring(1);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String datestamp() {
|
||||
Date d = new Date();
|
||||
SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
|
||||
return f.format(d);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,12 +8,15 @@
|
||||
* Contributors:
|
||||
* Pivotal, Inc. - initial API and implementation
|
||||
*******************************************************************************/
|
||||
package org.springframework.ide.vscode.boot.properties.metadata;
|
||||
package org.springframework.ide.vscode.util;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.ide.vscode.boot.properties.util.StringUtil;
|
||||
import org.springframework.ide.vscode.util.StringUtil;
|
||||
|
||||
/**
|
||||
* @author Kris De Volder
|
||||
Reference in New Issue
Block a user