-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3efd83d
commit ec865b2
Showing
4 changed files
with
227 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
src/main/java/net/kunmc/lab/peyangpaperutils/collectors/ExCollectors.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,74 @@ | ||
package net.kunmc.lab.peyangpaperutils.collectors; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* つよつよなコレクターを提供します。 | ||
*/ | ||
public class ExCollectors | ||
{ | ||
/** | ||
* 逆順のリストを生成するコレクターを返します。 | ||
* | ||
* @param <T> 要素の型 | ||
* @return コレクター | ||
*/ | ||
public static <T> ReversingCollector<T> toReversedList() | ||
{ | ||
return new ReversingCollector<>(); | ||
} | ||
|
||
/** | ||
* {@link net.kunmc.lab.peyangpaperutils.lib.utils.Pair} の {@link Map} を生成するコレクターを返します。 | ||
* | ||
* @param mapSupplier {@link Map} のコンストラクター | ||
* @param <L> 左側の型 | ||
* @param <R> 右側の型 | ||
* @param <M> {@link Map} の型 | ||
* @return コレクター | ||
*/ | ||
public static <L, R, M extends Map<L, R>> MappingPairCollector<L, R, M> toPairMap(Supplier<M> mapSupplier) | ||
{ | ||
return new MappingPairCollector<>(mapSupplier); | ||
} | ||
|
||
/** | ||
* {@link net.kunmc.lab.peyangpaperutils.lib.utils.Pair} の {@link HashMap} を生成するコレクターを返します。 | ||
* | ||
* @param <L> 左側の型 | ||
* @param <R> 右側の型 | ||
* @return コレクター | ||
*/ | ||
public static <L, R> MappingPairCollector<L, R, HashMap<L, R>> toPairHashMap() | ||
{ | ||
return new MappingPairCollector<>(HashMap::new); | ||
} | ||
|
||
/** | ||
* {@link Map.Entry} を {@link Map} として生成するコレクターを返します。 | ||
* | ||
* @param mapSupplier {@link Map} のコンストラクター | ||
* @param <L> 左側の型 | ||
* @param <R> 右側の型 | ||
* @param <M> {@link Map} の型 | ||
* @return コレクター | ||
*/ | ||
public static <L, R, M extends Map<L, R>> MappingMapElementCollector<L, R, M> toMap(Supplier<M> mapSupplier) | ||
{ | ||
return new MappingMapElementCollector<>(mapSupplier); | ||
} | ||
|
||
/** | ||
* {@link Map.Entry} を {@link HashMap} として生成するコレクターを返します。 | ||
* | ||
* @param <L> 左側の型 | ||
* @param <R> 右側の型 | ||
* @return コレクター | ||
*/ | ||
public static <L, R> MappingMapElementCollector<L, R, HashMap<L, R>> toHashMap() | ||
{ | ||
return new MappingMapElementCollector<>(HashMap::new); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/net/kunmc/lab/peyangpaperutils/collectors/MappingMapElementCollector.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,52 @@ | ||
package net.kunmc.lab.peyangpaperutils.collectors; | ||
|
||
import lombok.AllArgsConstructor; | ||
|
||
import java.util.EnumSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.BinaryOperator; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collector; | ||
|
||
@AllArgsConstructor | ||
/* non-public */ class MappingMapElementCollector<L, R, M extends Map<L, R>> implements Collector<Map.Entry<L, R>, M, M> | ||
{ | ||
private final Supplier<M> supplier; | ||
|
||
@Override | ||
public Supplier<M> supplier() | ||
{ | ||
return this.supplier; | ||
} | ||
|
||
@Override | ||
public BiConsumer<M, Map.Entry<L, R>> accumulator() | ||
{ | ||
return (map, pair) -> map.put(pair.getKey(), pair.getValue()); | ||
} | ||
|
||
@Override | ||
public BinaryOperator<M> combiner() | ||
{ | ||
return (map1, map2) -> | ||
{ | ||
map1.putAll(map2); | ||
return map1; | ||
}; | ||
} | ||
|
||
@Override | ||
public Function<M, M> finisher() | ||
{ | ||
return map -> map; | ||
} | ||
|
||
@Override | ||
public Set<Characteristics> characteristics() | ||
{ | ||
return EnumSet.of(Characteristics.CONCURRENT); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/net/kunmc/lab/peyangpaperutils/collectors/MappingPairCollector.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,53 @@ | ||
package net.kunmc.lab.peyangpaperutils.collectors; | ||
|
||
import lombok.AllArgsConstructor; | ||
import net.kunmc.lab.peyangpaperutils.lib.utils.Pair; | ||
|
||
import java.util.EnumSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.BinaryOperator; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collector; | ||
|
||
@AllArgsConstructor | ||
/* non-public */ class MappingPairCollector<L, R, M extends Map<L, R>> implements Collector<Pair<L, R>, M, M> | ||
{ | ||
private final Supplier<M> supplier; | ||
|
||
@Override | ||
public Supplier<M> supplier() | ||
{ | ||
return this.supplier; | ||
} | ||
|
||
@Override | ||
public BiConsumer<M, Pair<L, R>> accumulator() | ||
{ | ||
return (map, pair) -> map.put(pair.getLeft(), pair.getRight()); | ||
} | ||
|
||
@Override | ||
public BinaryOperator<M> combiner() | ||
{ | ||
return (map1, map2) -> | ||
{ | ||
map1.putAll(map2); | ||
return map1; | ||
}; | ||
} | ||
|
||
@Override | ||
public Function<M, M> finisher() | ||
{ | ||
return map -> map; | ||
} | ||
|
||
@Override | ||
public Set<Characteristics> characteristics() | ||
{ | ||
return EnumSet.of(Characteristics.CONCURRENT); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/net/kunmc/lab/peyangpaperutils/collectors/ReversingCollector.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,48 @@ | ||
package net.kunmc.lab.peyangpaperutils.collectors; | ||
|
||
import java.util.ArrayList; | ||
import java.util.EnumSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.BiConsumer; | ||
import java.util.function.BinaryOperator; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
import java.util.stream.Collector; | ||
|
||
/* non-public */ class ReversingCollector<T> implements Collector<T, List<T>, List<T>> | ||
{ | ||
@Override | ||
public Supplier<List<T>> supplier() | ||
{ | ||
return ArrayList::new; | ||
} | ||
|
||
@Override | ||
public BiConsumer<List<T>, T> accumulator() | ||
{ | ||
return (list, t) -> list.add(0, t); | ||
} | ||
|
||
@Override | ||
public BinaryOperator<List<T>> combiner() | ||
{ | ||
return (list1, list2) -> | ||
{ | ||
list1.addAll(0, list2); | ||
return list1; | ||
}; | ||
} | ||
|
||
@Override | ||
public Function<List<T>, List<T>> finisher() | ||
{ | ||
return list -> list; | ||
} | ||
|
||
@Override | ||
public Set<Characteristics> characteristics() | ||
{ | ||
return EnumSet.of(Characteristics.CONCURRENT); | ||
} | ||
} |