-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ replace ThreadLocalProxy with SPI #22
- Loading branch information
Showing
12 changed files
with
241 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package pro.fessional.mirana.dync; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.Iterator; | ||
import java.util.ServiceLoader; | ||
|
||
/** | ||
* order and get 1st service | ||
* | ||
* @author trydofor | ||
* @since 2024-02-16 | ||
*/ | ||
public interface OrderedSpi { | ||
|
||
@Nullable | ||
static <S> S first(@NotNull Class<S> service) { | ||
return first(service, (Comparator<S>) null); | ||
} | ||
|
||
@Nullable | ||
static <S> S first(@NotNull Class<S> service, @NotNull ClassLoader loader) { | ||
return first(service, loader, null); | ||
} | ||
|
||
@Nullable | ||
static <S> S first(@NotNull Class<S> service, @Nullable Comparator<S> comparator) { | ||
return first(ServiceLoader.load(service), comparator); | ||
} | ||
|
||
@Nullable | ||
static <S> S first(@NotNull Class<S> service, @NotNull ClassLoader loader, @Nullable Comparator<S> comparator) { | ||
return first(ServiceLoader.load(service, loader), comparator); | ||
} | ||
|
||
@Nullable | ||
static <S> S first(@NotNull ServiceLoader<S> loader, @Nullable Comparator<S> comparator) { | ||
S cur = null; | ||
if (comparator == null) { | ||
Iterator<S> it = loader.iterator(); | ||
if (it.hasNext()) { | ||
cur = it.next(); | ||
} | ||
|
||
if (cur instanceof Comparable) { // not null, not empty | ||
ArrayList<S> arr = new ArrayList<>(); | ||
arr.add(cur); | ||
while (it.hasNext()) { | ||
arr.add(it.next()); | ||
} | ||
|
||
arr.sort(null); | ||
cur = arr.get(0); | ||
} | ||
} | ||
else { | ||
ArrayList<S> arr = new ArrayList<>(); | ||
for (S s : loader) { | ||
arr.add(s); | ||
} | ||
|
||
if (!arr.isEmpty()) { | ||
arr.sort(comparator); | ||
cur = arr.get(0); | ||
} | ||
} | ||
|
||
return cur; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/pro/fessional/mirana/evil/ThreadLocalProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package pro.fessional.mirana.evil; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* ThreadLocal without init value. | ||
* | ||
* @author trydofor | ||
* @since 2024-02-16 | ||
*/ | ||
public interface ThreadLocalProvider extends Comparable<ThreadLocalProvider> { | ||
default int getOrder() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
default int compareTo(@NotNull ThreadLocalProvider o) { | ||
return Integer.compare(this.getOrder(), o.getOrder()); | ||
} | ||
|
||
@NotNull ThreadLocal<?> get(); | ||
} |
108 changes: 0 additions & 108 deletions
108
src/main/java/pro/fessional/mirana/evil/ThreadLocalProxy.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/test/java/pro/fessional/mirana/dync/OrderedSpiTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package pro.fessional.mirana.dync; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
|
||
/** | ||
* @author trydofor | ||
* @since 2024-02-17 | ||
*/ | ||
class OrderedSpiTest { | ||
|
||
@Test | ||
void orderNull() { | ||
ArrayList<Integer> arr = new ArrayList<>(); | ||
arr.add(2); | ||
arr.add(1); | ||
arr.sort(null); | ||
Assertions.assertEquals(Arrays.asList(1,2), arr); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/test/java/pro/fessional/mirana/evil/TestThreadLocalProvider1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package pro.fessional.mirana.evil; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* @author trydofor | ||
* @since 2024-02-16 | ||
*/ | ||
public class TestThreadLocalProvider1 implements ThreadLocalProvider { | ||
|
||
@Override | ||
public int getOrder() { | ||
return 1; | ||
} | ||
|
||
@Override | ||
public @NotNull ThreadLocal<?> get() { | ||
return new ThreadLocal<>(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/test/java/pro/fessional/mirana/evil/TestThreadLocalProvider2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package pro.fessional.mirana.evil; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* @author trydofor | ||
* @since 2024-02-16 | ||
*/ | ||
public class TestThreadLocalProvider2 implements ThreadLocalProvider { | ||
@Override | ||
public int getOrder() { | ||
return 2; | ||
} | ||
|
||
@Override | ||
public @NotNull ThreadLocal<?> get() { | ||
return new ThreadLocal<>(); | ||
} | ||
} |
Oops, something went wrong.