Skip to content

Commit

Permalink
feat: 添加新方法; upgrade version to 0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
KangSpace committed Oct 4, 2024
1 parent 5c4caa7 commit f835f65
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.kangspace.devhelper</groupId>
<artifactId>dev-helper</artifactId>
<version>0.0.3-SNAPSHOT</version>
<version>0.0.3</version>
<packaging>jar</packaging>

<name>dev-helper</name>
Expand Down Expand Up @@ -75,7 +75,7 @@
<log4j-to-slf4j.version>2.13.3</log4j-to-slf4j.version>
<slf4j.api.version>1.7.30</slf4j.api.version>
<gson.version>2.9.0</gson.version>
<jackson.version>2.12.5</jackson.version>
<jackson.version>2.15.2</jackson.version>
<junit.version>4.13.2</junit.version>
<netty-buffer.version>4.1.79.Final</netty-buffer.version>
</properties>
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/kangspace/devhelper/CollectionUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@ public static boolean isEmpty(Collection<?> collection) {
return collection == null || collection.isEmpty();
}

public static boolean isEmpty(Map map) {
public static boolean isEmpty(Map<?,?> map) {
return map == null || map.isEmpty();
}

public static boolean isNotEmpty(Collection<?> collection) {
return !isEmpty(collection);
}

public static boolean isNotEmpty(Map<?,?> map) {
return !isEmpty(map);
}

public static <T> T first(Collection<T> collection) {
return isNotEmpty(collection) ? collection.stream().findFirst().orElse(null) : null;
}
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/org/kangspace/devhelper/http/UrlHelper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.kangspace.devhelper.http;

import org.kangspace.devhelper.str.StringHelper;
import org.kangspace.devhelper.str.StringLiteral;

import javax.annotation.Nonnull;
import java.io.UnsupportedEncodingException;
Expand Down Expand Up @@ -73,4 +74,27 @@ public static String url(String urlPattern, String... params) {
}
return MessageFormat.format(urlPattern, vars);
}

/**
* url参数替换
* @param url url
* @param name 参数名
* @param value 参数值
*/
public static String urlReplaceParam(String url, String name, String value) {
// 如何url中包含name参数,则删除并替换为新的value,如果不存在name,则添加该参数到url中
String regex = "([?&])" + name + "=.*?(&|$)";
url = url.replaceAll(regex, "$1" + name + "=" + value + "$2");
String prefix = name + StringLiteral.EQUALS;
if(!url.contains(prefix)){
url += (url.contains(StringLiteral.QUESTION_MARK) ? StringLiteral.AND : StringLiteral.QUESTION_MARK) + prefix + value;
}
return url;
}

public static void main(String[] args) {
System.out.println(urlReplaceParam("http://example.com/?a=1&b=2", "b", "3"));
System.out.println(urlReplaceParam("http://example.com/", "b", "3"));
System.out.println(urlReplaceParam("http://example.com/?b=2", "b", "3"));
}
}
14 changes: 14 additions & 0 deletions src/main/java/org/kangspace/devhelper/thread/ThreadUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,25 @@
*/
public class ThreadUtil {

/**
* 线程Sleep
*
* @param baseMillis 基于
*/
@SuppressWarnings("all")
public static void sleep(int baseMillis) {
try {
Thread.sleep(baseMillis);
} catch (InterruptedException e) {
}
}

/**
* 线程随机Sleep
*
* @param baseMillis 基于
*/
@SuppressWarnings("all")
public static void randomSleep(int baseMillis) {
long sleepMillis = new Random().nextInt(baseMillis);
try {
Expand Down

0 comments on commit f835f65

Please sign in to comment.