This commit is contained in:
Looly 2023-04-19 23:28:28 +08:00
parent 64d49ff87b
commit ffade772b1
4 changed files with 311 additions and 111 deletions

View File

@ -12,6 +12,7 @@
package org.dromara.hutool.core.map;
import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.lang.tuple.Triple;
@ -33,9 +34,9 @@ import java.util.List;
public class TripleTable<L, M, R> implements Serializable {
private static final long serialVersionUID = 1L;
private final List<L> lList;
private final List<M> mList;
private final List<R> rList;
private final List<L> lefts;
private final List<M> middles;
private final List<R> rights;
/**
* 构造
@ -59,22 +60,22 @@ public class TripleTable<L, M, R> implements Serializable {
}
/**
* @param lList 左列表
* @param mList 中列表
* @param rList 右列表
* @param lefts 左列表
* @param middles 中列表
* @param rights 右列表
*/
public TripleTable(final List<L> lList, final List<M> mList, final List<R> rList) {
Assert.notNull(lList);
Assert.notNull(mList);
Assert.notNull(rList);
final int size = lList.size();
if (size != mList.size() || size != rList.size()) {
public TripleTable(final List<L> lefts, final List<M> middles, final List<R> rights) {
Assert.notNull(lefts);
Assert.notNull(middles);
Assert.notNull(rights);
final int size = lefts.size();
if (size != middles.size() || size != rights.size()) {
throw new IllegalArgumentException("List size must be equals!");
}
this.lList = lList;
this.mList = mList;
this.rList = rList;
this.lefts = lefts;
this.middles = middles;
this.rights = rights;
}
// region ----- getLeft
@ -87,9 +88,9 @@ public class TripleTable<L, M, R> implements Serializable {
* @return 左边值未找到返回{@code null}
*/
public L getLeftByMiddle(final M mValue) {
final int index = this.mList.indexOf(mValue);
final int index = this.middles.indexOf(mValue);
if (index > -1) {
return this.lList.get(index);
return this.lefts.get(index);
}
return null;
}
@ -102,9 +103,9 @@ public class TripleTable<L, M, R> implements Serializable {
* @return 左边值未找到返回{@code null}
*/
public L getLeftByRight(final R rValue) {
final int index = this.rList.indexOf(rValue);
final int index = this.rights.indexOf(rValue);
if (index > -1) {
return this.lList.get(index);
return this.lefts.get(index);
}
return null;
}
@ -120,9 +121,9 @@ public class TripleTable<L, M, R> implements Serializable {
* @return 中值未找到返回{@code null}
*/
public M getMiddleByLeft(final L lValue) {
final int index = this.lList.indexOf(lValue);
final int index = this.lefts.indexOf(lValue);
if (index > -1) {
return this.mList.get(index);
return this.middles.get(index);
}
return null;
}
@ -135,14 +136,48 @@ public class TripleTable<L, M, R> implements Serializable {
* @return 中值未找到返回{@code null}
*/
public M getMiddleByRight(final R rValue) {
final int index = this.rList.indexOf(rValue);
final int index = this.rights.indexOf(rValue);
if (index > -1) {
return this.mList.get(index);
return this.middles.get(index);
}
return null;
}
// endregion
// region ----- get
/**
* 获取指定index对应的左值
*
* @param index 索引
* @return 左值
*/
public L getLeft(final int index){
return this.lefts.get(index);
}
/**
* 获取指定index对应的中值
*
* @param index 索引
* @return 中值
*/
public M getMiddle(final int index){
return this.middles.get(index);
}
/**
* 获取指定index对应的右值
*
* @param index 索引
* @return 右值
*/
public R getRight(final int index){
return this.rights.get(index);
}
// endregion
// region ----- getRight
/**
@ -153,9 +188,9 @@ public class TripleTable<L, M, R> implements Serializable {
* @return 右值未找到返回{@code null}
*/
public R getRightByLeft(final L lValue) {
final int index = this.lList.indexOf(lValue);
final int index = this.lefts.indexOf(lValue);
if (index > -1) {
return this.rList.get(index);
return this.rights.get(index);
}
return null;
}
@ -168,14 +203,80 @@ public class TripleTable<L, M, R> implements Serializable {
* @return 右值未找到返回{@code null}
*/
public R getRightByMiddle(final M mValue) {
final int index = this.mList.indexOf(mValue);
final int index = this.middles.indexOf(mValue);
if (index > -1) {
return this.rList.get(index);
return this.rights.get(index);
}
return null;
}
// endregion
// region ----- contains
/**
* 是否含有指定左元素
*
* @param left 左元素
* @return 是否含有
*/
public boolean containLeft(final L left) {
return this.lefts.contains(left);
}
/**
* 是否含有指定中元素
*
* @param middle 中元素
* @return 是否含有
*/
public boolean containMiddle(final M middle) {
return this.middles.contains(middle);
}
/**
* 是否含有指定右元素
*
* @param right 右元素
* @return 是否含有
*/
public boolean containRight(final R right) {
return this.rights.contains(right);
}
// endregion
// region ----- indexOf
/**
* 获取指定左元素的索引
*
* @param left 左元素
* @return 索引未找到返回-1
*/
public int indexOfLeft(final L left) {
return this.lefts.indexOf(left);
}
/**
* 获取指定中元素的索引
*
* @param middle 中元素
* @return 索引未找到返回-1
*/
public int indexOfMiddle(final M middle) {
return this.middles.indexOf(middle);
}
/**
* 获取指定右元素的索引
*
* @param right 右元素
* @return 索引未找到返回-1
*/
public int indexOfRight(final R right) {
return this.rights.indexOf(right);
}
// endregion
// region ----- getBy
/**
@ -185,12 +286,12 @@ public class TripleTable<L, M, R> implements Serializable {
* @return 三元组所有值
*/
public Triple<L, M, R> getByLeft(final L lValue) {
final int index = this.lList.indexOf(lValue);
final int index = this.lefts.indexOf(lValue);
if (index > -1) {
return new Triple<>(
lList.get(index),
mList.get(index),
rList.get(index)
lefts.get(index),
middles.get(index),
rights.get(index)
);
}
return null;
@ -203,12 +304,12 @@ public class TripleTable<L, M, R> implements Serializable {
* @return 三元组所有值
*/
public Triple<L, M, R> getByMiddle(final M mValue) {
final int index = this.mList.indexOf(mValue);
final int index = this.middles.indexOf(mValue);
if (index > -1) {
return new Triple<>(
lList.get(index),
mList.get(index),
rList.get(index)
lefts.get(index),
middles.get(index),
rights.get(index)
);
}
return null;
@ -221,25 +322,56 @@ public class TripleTable<L, M, R> implements Serializable {
* @return 三元组所有值
*/
public Triple<L, M, R> getByRight(final R rValue) {
final int index = this.rList.indexOf(rValue);
final int index = this.rights.indexOf(rValue);
if (index > -1) {
return new Triple<>(
lList.get(index),
mList.get(index),
rList.get(index)
lefts.get(index),
middles.get(index),
rights.get(index)
);
}
return null;
}
// endregion
// region ----- getList
/**
* 获取左列表不可修改
*
* @return 左列表
*/
public List<L> getLefts() {
return ListUtil.view(this.lefts);
}
/**
* 获取中列表不可修改
*
* @return 中列表
*/
public List<M> getMiddles() {
return ListUtil.view(this.middles);
}
/**
* 获取右列表不可修改
*
* @return 右列表
*/
public List<R> getRights() {
return ListUtil.view(this.rights);
}
// endregion
/**
* 长度
*
* @return this
*/
public int size() {
return this.lList.size();
return this.lefts.size();
}
/**
@ -251,21 +383,60 @@ public class TripleTable<L, M, R> implements Serializable {
* @return this
*/
public TripleTable<L, M, R> put(final L lValue, final M mValue, final R rValue) {
this.lList.add(lValue);
this.mList.add(mValue);
this.rList.add(rValue);
this.lefts.add(lValue);
this.middles.add(mValue);
this.rights.add(rValue);
return this;
}
// region ----- set
/**
* 修改指定index对应的左值
*
* @param index 索引
* @param lValue 左值
* @return this
*/
public TripleTable<L, M, R> setLeft(final int index, final L lValue) {
this.lefts.set(index, lValue);
return this;
}
/**
* 修改指定index对应的中值
*
* @param index 索引
* @param mValue 中值
* @return this
*/
public TripleTable<L, M, R> setMiddle(final int index, final M mValue) {
this.middles.set(index, mValue);
return this;
}
/**
* 修改指定index对应的右值
*
* @param index 索引
* @param rValue 左值
* @return this
*/
public TripleTable<L, M, R> setRight(final int index, final R rValue) {
this.rights.set(index, rValue);
return this;
}
// endregion
/**
* 清空
*
* @return this
*/
public TripleTable<L, M, R> clear() {
this.lList.clear();
this.mList.clear();
this.rList.clear();
this.lefts.clear();
this.middles.clear();
this.rights.clear();
return this;
}
@ -276,9 +447,9 @@ public class TripleTable<L, M, R> implements Serializable {
* @return this
*/
public TripleTable<L, M, R> remove(final int index) {
this.lList.remove(index);
this.mList.remove(index);
this.rList.remove(index);
this.lefts.remove(index);
this.middles.remove(index);
this.rights.remove(index);
return this;
}
}

View File

@ -12,14 +12,13 @@
package org.dromara.hutool.cron;
import org.dromara.hutool.core.map.TripleTable;
import org.dromara.hutool.core.text.StrUtil;
import org.dromara.hutool.cron.pattern.CronPattern;
import org.dromara.hutool.cron.task.CronTask;
import org.dromara.hutool.cron.task.Task;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
@ -35,14 +34,14 @@ import java.util.concurrent.locks.ReentrantReadWriteLock;
public class TaskTable implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 默认任务表大小10
*/
public static final int DEFAULT_CAPACITY = 10;
private final ReadWriteLock lock;
private final List<String> ids;
private final List<CronPattern> patterns;
private final List<Task> tasks;
private int size;
private final TripleTable<String, CronPattern, Task> table;
/**
* 构造
@ -59,9 +58,7 @@ public class TaskTable implements Serializable {
public TaskTable(final int initialCapacity) {
lock = new ReentrantReadWriteLock();
ids = new ArrayList<>(initialCapacity);
patterns = new ArrayList<>(initialCapacity);
tasks = new ArrayList<>(initialCapacity);
this.table = new TripleTable<>(initialCapacity);
}
/**
@ -76,13 +73,10 @@ public class TaskTable implements Serializable {
final Lock writeLock = lock.writeLock();
writeLock.lock();
try {
if (ids.contains(id)) {
if (this.table.containLeft(id)) {
throw new CronException("Id [{}] has been existed!", id);
}
ids.add(id);
patterns.add(pattern);
tasks.add(task);
size++;
this.table.put(id, pattern, task);
} finally {
writeLock.unlock();
}
@ -99,7 +93,7 @@ public class TaskTable implements Serializable {
final Lock readLock = lock.readLock();
readLock.lock();
try {
return Collections.unmodifiableList(this.ids);
return this.table.getLefts();
} finally {
readLock.unlock();
}
@ -115,7 +109,7 @@ public class TaskTable implements Serializable {
final Lock readLock = lock.readLock();
readLock.lock();
try {
return Collections.unmodifiableList(this.patterns);
return this.table.getMiddles();
} finally {
readLock.unlock();
}
@ -131,7 +125,7 @@ public class TaskTable implements Serializable {
final Lock readLock = lock.readLock();
readLock.lock();
try {
return Collections.unmodifiableList(this.tasks);
return this.table.getRights();
} finally {
readLock.unlock();
}
@ -147,18 +141,15 @@ public class TaskTable implements Serializable {
final Lock writeLock = lock.writeLock();
writeLock.lock();
try {
final int index = ids.indexOf(id);
if (index < 0) {
return false;
final int index = this.table.indexOfLeft(id);
if (index > -1) {
this.table.remove(index);
return true;
}
tasks.remove(index);
patterns.remove(index);
ids.remove(index);
size--;
} finally {
writeLock.unlock();
}
return true;
return false;
}
/**
@ -173,9 +164,9 @@ public class TaskTable implements Serializable {
final Lock writeLock = lock.writeLock();
writeLock.lock();
try {
final int index = ids.indexOf(id);
final int index = this.table.indexOfLeft(id);
if (index > -1) {
patterns.set(index, pattern);
this.table.setMiddle(index, pattern);
return true;
}
} finally {
@ -195,7 +186,7 @@ public class TaskTable implements Serializable {
final Lock readLock = lock.readLock();
readLock.lock();
try {
return tasks.get(index);
return this.table.getRight(index);
} finally {
readLock.unlock();
}
@ -209,11 +200,30 @@ public class TaskTable implements Serializable {
* @since 3.1.1
*/
public Task getTask(final String id) {
final int index = ids.indexOf(id);
if (index > -1) {
return getTask(index);
final Lock readLock = lock.readLock();
readLock.lock();
try {
return table.getRightByLeft(id);
} finally {
readLock.unlock();
}
}
/**
* 获得指定id的{@link CronPattern}
*
* @param id ID
* @return {@link CronPattern}
* @since 3.1.1
*/
public CronPattern getPattern(final String id) {
final Lock readLock = lock.readLock();
readLock.lock();
try {
return table.getMiddleByLeft(id);
} finally {
readLock.unlock();
}
return null;
}
/**
@ -227,7 +237,7 @@ public class TaskTable implements Serializable {
final Lock readLock = lock.readLock();
readLock.lock();
try {
return patterns.get(index);
return table.getMiddle(index);
} finally {
readLock.unlock();
}
@ -240,7 +250,7 @@ public class TaskTable implements Serializable {
* @since 4.0.2
*/
public int size() {
return this.size;
return this.table.size();
}
/**
@ -250,22 +260,7 @@ public class TaskTable implements Serializable {
* @since 4.0.2
*/
public boolean isEmpty() {
return this.size < 1;
}
/**
* 获得指定id的{@link CronPattern}
*
* @param id ID
* @return {@link CronPattern}
* @since 3.1.1
*/
public CronPattern getPattern(final String id) {
final int index = ids.indexOf(id);
if (index > -1) {
return getPattern(index);
}
return null;
return size() < 1;
}
/**
@ -286,10 +281,11 @@ public class TaskTable implements Serializable {
@Override
public String toString() {
final int size = this.size();
final StringBuilder builder = StrUtil.builder();
for (int i = 0; i < size; i++) {
builder.append(StrUtil.format("[{}] [{}] [{}]\n",
ids.get(i), patterns.get(i), tasks.get(i)));
this.table.getLeft(i), this.table.getMiddle(i), this.table.getRight(i)));
}
return builder.toString();
}
@ -302,9 +298,11 @@ public class TaskTable implements Serializable {
* @since 3.1.1
*/
protected void executeTaskIfMatchInternal(final Scheduler scheduler, final long millis) {
final int size = size();
for (int i = 0; i < size; i++) {
if (patterns.get(i).match(scheduler.config.timezone, millis, scheduler.config.matchSecond)) {
scheduler.taskExecutorManager.spawnExecutor(new CronTask(ids.get(i), patterns.get(i), tasks.get(i)));
if (this.table.getMiddle(i).match(scheduler.config.timezone, millis, scheduler.config.matchSecond)) {
scheduler.taskExecutorManager.spawnExecutor(
new CronTask(this.table.getLeft(i), this.table.getMiddle(i), this.table.getRight(i)));
}
}
}

View File

@ -18,11 +18,7 @@ import org.dromara.hutool.cron.pattern.matcher.PatternMatcher;
import org.dromara.hutool.cron.pattern.parser.PatternParser;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.TimeZone;
import java.util.*;
/**
* 定时任务表达式<br>
@ -170,6 +166,23 @@ public class CronPattern {
return next;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final CronPattern that = (CronPattern) o;
return Objects.equals(pattern, that.pattern);
}
@Override
public int hashCode() {
return Objects.hash(pattern);
}
@Override
public String toString() {
return this.pattern;

View File

@ -3,19 +3,37 @@ package org.dromara.hutool.cron;
import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.lang.id.IdUtil;
import org.dromara.hutool.cron.pattern.CronPattern;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.List;
public class TaskTableTest {
@Test
@Disabled
public void toStringTest(){
public void taskTableTest(){
final TaskTable taskTable = new TaskTable();
taskTable.add(IdUtil.fastUUID(), new CronPattern("*/10 * * * * *"), ()-> Console.log("Task 1"));
taskTable.add(IdUtil.fastUUID(), new CronPattern("*/20 * * * * *"), ()-> Console.log("Task 2"));
taskTable.add(IdUtil.fastUUID(), new CronPattern("*/30 * * * * *"), ()-> Console.log("Task 3"));
Console.log(taskTable);
Assertions.assertEquals(3, taskTable.size());
final List<String> ids = taskTable.getIds();
Assertions.assertEquals(3, ids.size());
// getById
Assertions.assertEquals(new CronPattern("*/10 * * * * *"), taskTable.getPattern(ids.get(0)));
Assertions.assertEquals(new CronPattern("*/20 * * * * *"), taskTable.getPattern(ids.get(1)));
Assertions.assertEquals(new CronPattern("*/30 * * * * *"), taskTable.getPattern(ids.get(2)));
// set test
taskTable.updatePattern(ids.get(2), new CronPattern("*/40 * * * * *"));
Assertions.assertEquals(new CronPattern("*/40 * * * * *"), taskTable.getPattern(ids.get(2)));
// getTask
Assertions.assertEquals(
taskTable.getTask(1),
taskTable.getTask(ids.get(1))
);
}
}