Skip to content

Commit

Permalink
Merge pull request #32 from pugwoo/develop-daily
Browse files Browse the repository at this point in the history
release 1.2.1
  • Loading branch information
pugwoo authored Sep 28, 2023
2 parents 0a80e28 + 73f4f9e commit 41044db
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 15 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2023年9月28日
v1.2.1 - [add] 新增DateUtils.diffDays(LocalDate,LocalDate)方法
- [fix] 修复IOUtils.readAll(InputStream, String)方法读取文件时,trim掉最后一个回车符的问题
- [add] 新增ThreadPoolUtils,代替ExecuteThem

2023年8月20日
v1.2.0 - [add] 新增algorithm包,增加DynamicBaseNumber,动态进制的数字
- [add] ListUtils增加distinct方法,去重
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<dependency>
<groupId>com.pugwoo</groupId>
<artifactId>woo-utils</artifactId>
<version>1.2.0</version>
<version>1.2.1</version>
</dependency>
```

Expand Down
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>com.pugwoo</groupId>
<artifactId>woo-utils</artifactId>
<packaging>jar</packaging>
<version>1.2.0</version>
<version>1.2.1</version>

<name>woo-utils</name>
<description>the common utils</description>
Expand Down Expand Up @@ -43,7 +43,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.7</version>
<version>2.0.9</version>
</dependency>

<!-- json相关功能 -->
Expand Down
29 changes: 19 additions & 10 deletions src/main/java/com/pugwoo/wooutils/io/IOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -83,15 +81,26 @@ public static byte[] readAllAndClose(InputStream in) throws IOException {
* @return
*/
public static String readAll(InputStream in, String charset) {
Scanner scanner = new Scanner(in, charset);

BufferedReader reader = null;
try {
String content = scanner.useDelimiter("\\Z").next();
return content;
} catch (NoSuchElementException e) {
return ""; // file is empty, ignore exception
InputStreamReader inputStreamReader = new InputStreamReader(in, charset);
reader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append("\n");
}
return stringBuilder.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
scanner.close();
if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {
}
}
}
}

Expand Down Expand Up @@ -154,7 +163,7 @@ public static InputStream readClasspathResourceInputStream(String path) {
if (StringTools.isEmpty(path)) {
return null;
}
// 分为以/开头和没有以/开头的path进行尝试,优先没有/开头的,以为classLoader的方式不需要/开头
// 分为以/开头和没有以/开头的path进行尝试,优先没有/开头的,因为classLoader的方式不需要/开头
boolean beginWithSlash = path.startsWith("/");
String noSlash = beginWithSlash ? path.substring(1) : path;
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(noSlash);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/pugwoo/wooutils/lang/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -544,6 +545,15 @@ public static int diffDays(Date date1, Date date2) {
return (int) (Math.abs(date1.getTime() - date2.getTime()) / (24 * 3600 * 1000));
}

/**
* 计算两个日期的天数差。同一天返回0,相隔1天返回1,以此类推。
* @return 返回值都大于等于0,不关心date1和date2的顺序
*/
public static int diffDays(LocalDate date1, LocalDate date2) {
long between = ChronoUnit.DAYS.between(date1, date2);
return between >= 0 ? ((int) between) : (-(int) between);
}

/**
* 计算两个日期的年份差,主要用于计算年龄。不足一年的不计,一年按365天计,不考虑闰年。
* @return 返回值都大于等于0,不关心date1和date2的顺序
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/pugwoo/wooutils/task/ExecuteThem.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
* 2. 调用waitAllTerminate方法等待所有任务执行完成
*
* 【注】每个ExecuteThem调用waitAllTerminate之后,就不能再add增加任务了。
*
* @deprecated 请使用ThreadPoolUtils代替
*/
@Deprecated
public class ExecuteThem {

/**
Expand Down
62 changes: 62 additions & 0 deletions src/main/java/com/pugwoo/wooutils/thread/ThreadPoolUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.pugwoo.wooutils.thread;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

public class ThreadPoolUtils {

/**
* 创建一个线程池,一些默认配置:
* 1)空闲线程存活时间为60秒
* 2)拒绝策略:用默认,抛出RejectedExecutionException异常
* @param coreSize
* @param queueSize 任务排队队列最大长度
* @param maxSize
* @param threadNamePrefix 线程前缀名称
*/
public static ThreadPoolExecutor createThreadPool(int coreSize, int queueSize, int maxSize, String threadNamePrefix) {
return new ThreadPoolExecutor(
coreSize, maxSize,
60, // 空闲线程存活时间
TimeUnit.SECONDS, // 存活时间单位
queueSize <= 0 ? new SynchronousQueue<>() : new LinkedBlockingQueue<>(queueSize),
new MyThreadFactory(threadNamePrefix)
);
}

/**
* 等待所有的future调用完成
*/
public static List<?> waitAllFuturesDone(List<Future<?>> futures) {
if (futures == null) {
return new ArrayList<>();
}
List<Object> result = new ArrayList<>();
for (Future<?> future : futures) {
try {
result.add(future.get());
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
return result;
}

private static class MyThreadFactory implements ThreadFactory {

private final AtomicInteger count = new AtomicInteger(1);
private final String threadNamePrefix;

public MyThreadFactory(String threadNamePrefix) {
this.threadNamePrefix = threadNamePrefix;
}

@Override
public Thread newThread(Runnable r) {
return new Thread(r, threadNamePrefix + "-" + count.getAndIncrement());
}
}

}
34 changes: 33 additions & 1 deletion src/test/java/com/pugwoo/wooutils/date/TestDate.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,37 @@ public void test2() {
assert DateUtils.getDay(DateUtils.parseLocalDate("2023-04-05")) == 5;

}


@Test
public void testDiffDays() {
{
LocalDate date1 = LocalDate.of(2019, 1, 1);
LocalDate date2 = LocalDate.of(2019, 1, 1);
assert DateUtils.diffDays(date1, date2) == 0;
assert DateUtils.diffDays(date2, date1) == 0;
}

{
LocalDate date1 = LocalDate.of(2019, 1, 1);
LocalDate date2 = LocalDate.of(2019, 1, 2);
assert DateUtils.diffDays(date1, date2) == 1;
assert DateUtils.diffDays(date2, date1) == 1;
}

{
LocalDate date1 = LocalDate.of(2019, 1, 1);
LocalDate date2 = LocalDate.of(2019, 2, 2);
assert DateUtils.diffDays(date1, date2) == 32;
assert DateUtils.diffDays(date2, date1) == 32;
}

{
LocalDate date1 = LocalDate.of(2019, 1, 1);
LocalDate date2 = LocalDate.of(2020, 2, 2);
assert DateUtils.diffDays(date1, date2) == 397;
assert DateUtils.diffDays(date2, date1) == 397;
}

}

}
3 changes: 2 additions & 1 deletion src/test/java/com/pugwoo/wooutils/io/TestIOUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ public void testListFiles() throws IOException {

@Test
public void testGetClasspathFile() throws IOException {
String content = IOUtils.readClasspathResourceAsString("applicationContext-context.xml");
String content = IOUtils.readClasspathResourceAsString("sql/my.sql");
assert StringTools.isNotBlank(content);
assert content.equals("select *\nfrom dual\n");
}

}
2 changes: 2 additions & 0 deletions src/test/resources/sql/my.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
select *
from dual

0 comments on commit 41044db

Please sign in to comment.