-
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.
started to add flexibility for more things than just ToIntFunction
- Loading branch information
Showing
9 changed files
with
116 additions
and
30 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
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
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
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
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
17 changes: 17 additions & 0 deletions
17
src/main/java/net/zomis/fight/statextract/types/StatCollector.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,17 @@ | ||
package net.zomis.fight.statextract.types; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.stream.Collector; | ||
|
||
/** | ||
* Created by Simon on 3/20/2015. | ||
*/ | ||
public interface StatCollector { | ||
|
||
Collector<?,?,?> createCollector(Object fieldValue); | ||
|
||
Object getIndexValue(Object finish); | ||
|
||
Class<?> postedType(Field field); | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/net/zomis/fight/statextract/types/StatCollectors.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,21 @@ | ||
package net.zomis.fight.statextract.types; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.function.ToIntFunction; | ||
|
||
/** | ||
* Created by Simon on 3/20/2015. | ||
*/ | ||
public class StatCollectors { | ||
|
||
private Map<Class<?>, StatCollector> collectorTypes = new HashMap<>(); | ||
|
||
public StatCollectors() { | ||
this.collectorTypes.put(ToIntFunction.class, new ToIntFunctionSumCollector()); | ||
} | ||
|
||
public StatCollector get(Class<?> type) { | ||
return collectorTypes.get(type); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/net/zomis/fight/statextract/types/ToIntFunctionSumCollector.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,30 @@ | ||
package net.zomis.fight.statextract.types; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.IntSummaryStatistics; | ||
import java.util.function.ToIntFunction; | ||
import java.util.stream.Collector; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Created by Simon on 3/20/2015. | ||
*/ | ||
public class ToIntFunctionSumCollector implements StatCollector { | ||
|
||
@Override | ||
public Collector<?, ?, ?> createCollector(Object fieldValue) { | ||
return Collectors.summarizingInt((ToIntFunction) fieldValue); | ||
} | ||
|
||
@Override | ||
public Object getIndexValue(Object finish) { | ||
IntSummaryStatistics statistics = (IntSummaryStatistics) finish; | ||
return statistics.getSum(); | ||
} | ||
|
||
@Override | ||
public Class<?> postedType(Field field) { | ||
return TypeHelp.genericType(field, 0); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/net/zomis/fight/statextract/types/TypeHelp.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 net.zomis.fight.statextract.types; | ||
|
||
import java.lang.reflect.Field; | ||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
|
||
/** | ||
* Created by Simon on 3/20/2015. | ||
*/ | ||
public class TypeHelp { | ||
|
||
public static Class<?> genericType(Field field, int i) { | ||
Type genericFieldType = field.getGenericType(); | ||
if (!(genericFieldType instanceof ParameterizedType)) { | ||
throw new IllegalArgumentException("Cannot deserialize a Map without generics types"); | ||
} | ||
ParameterizedType aType = (ParameterizedType) genericFieldType; | ||
Type[] fieldArgTypes = aType.getActualTypeArguments(); | ||
return (Class<?>) fieldArgTypes[i]; | ||
} | ||
|
||
} |