Skip to content

Commit

Permalink
started to add flexibility for more things than just ToIntFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Mar 20, 2015
1 parent 40fdfa9 commit a827f49
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 30 deletions.
6 changes: 4 additions & 2 deletions src/main/java/net/zomis/fight/statextract/ClassExtractor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.zomis.fight.statextract;

import net.zomis.fight.statextract.types.StatCollector;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand All @@ -23,8 +25,8 @@ void add(Poster extractor, Object object) {
}
}

public void addCollector(String name, Supplier<Collector<?, ?, ?>> fieldValue) {
collectors.add(new CollectorInfo(name, fieldValue));
public void addCollector(String name, StatCollector statCollector, Supplier<Collector<?, ?, ?>> fieldValue) {
collectors.add(new CollectorInfo(name, statCollector, fieldValue));
}

public Map<String, Object> finish() {
Expand Down
16 changes: 12 additions & 4 deletions src/main/java/net/zomis/fight/statextract/CollectorInfo.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.zomis.fight.statextract;

import net.zomis.fight.statextract.types.StatCollector;

import java.util.function.Supplier;
import java.util.stream.Collector;

Expand All @@ -11,11 +13,13 @@ public class CollectorInfo {
private final String name;
private Object collectingObject;
private final Collector<Object, Object, Object> collector;
private final Supplier<Collector<?, ?, ?>> collectorSupplier;
private final Supplier<Collector<?, ?, ?>> supplier;
private final StatCollector statCollector;

public CollectorInfo(String name, Supplier<Collector<?, ?, ?>> supplier) {
public CollectorInfo(String name, StatCollector statCollector, Supplier<Collector<?, ?, ?>> supplier) {
this.name = name;
this.collectorSupplier = supplier;
this.statCollector = statCollector;
this.supplier = supplier;
Collector<?, ?, ?> collector = supplier.get();
this.collector = (Collector<Object, Object, Object>) collector;
this.collectingObject = collector.supplier().get();
Expand All @@ -41,6 +45,10 @@ public Object finish() {
}

public CollectorInfo copy() {
return new CollectorInfo(name, collectorSupplier);
return new CollectorInfo(name, statCollector, supplier);
}

public Object getIndexValue() {
return statCollector.getIndexValue(finish());
}
}
24 changes: 9 additions & 15 deletions src/main/java/net/zomis/fight/statextract/Extractor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package net.zomis.fight.statextract;

import net.zomis.fight.statextract.types.StatCollector;
import net.zomis.fight.statextract.types.StatCollectors;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
Expand Down Expand Up @@ -29,15 +32,16 @@ public IndexableResults collectIndexable() {

public static Extractor extractor(Object target) {
Extractor extractor = new Extractor(target);
StatCollectors collectorTypes = new StatCollectors();
for (Field field : target.getClass().getDeclaredFields()) {
field.setAccessible(true);
final Object fieldValue;
try {
fieldValue = field.get(target);
Objects.requireNonNull(fieldValue, "Field cannot be null: " + field.getName());
if (field.getType() == ToIntFunction.class) {
extractor.addExtractor(field.getName(), genericType(field, 0),
() -> Collectors.summarizingInt((ToIntFunction) fieldValue));
StatCollector statCollector = collectorTypes.get(field.getType());
if (statCollector != null) {
extractor.addExtractor(field.getName(), statCollector.postedType(field), statCollector, () -> statCollector.createCollector(fieldValue));
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
Expand All @@ -46,25 +50,15 @@ public static Extractor extractor(Object target) {
return extractor;
}

private void addExtractor(String name, Class<?> aClass, Supplier<Collector<?, ?, ?>> fieldValue) {
extractFor(aClass).addCollector(name, fieldValue);
private void addExtractor(String name, Class<?> aClass, StatCollector statCollector, Supplier<Collector<?, ?, ?>> fieldValue) {
extractFor(aClass).addCollector(name, statCollector, fieldValue);
}

private ClassExtractor extractFor(Class<?> aClass) {
classExtractorMap.putIfAbsent(aClass, new ClassExtractor());
return classExtractorMap.get(aClass);
}

private 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];
}

public <T> void addPreHandler(Class<T> clazz, BiConsumer<Poster, ? super T> preHandler) {
extractFor(clazz).addPreHandler(preHandler);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,8 @@ public Map<Object, Object> indexBy(String... fields) {
// if index is not last, put into a map of value-for-field, more-values
// if index is last, put the real actual values into the map
CollectorInfo indexCollector = instance.get(indexKey);
Object indexValue = indexCollector.finish();
Object indexValue = indexCollector.getIndexValue();

// the collector creates a special kind of object, extract the index value from this
IntSummaryStatistics value = (IntSummaryStatistics) indexValue;
indexValue = value.getSum();
if (index < fields.length - 1) {
throw new UnsupportedOperationException();
} else { // final part
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/net/zomis/fight/statextract/InstancePoster.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,4 @@ public Poster post(Object object) {
return this;
}

public InstancePoster combine(InstancePoster other) {

throw new UnsupportedOperationException();
}

}
17 changes: 17 additions & 0 deletions src/main/java/net/zomis/fight/statextract/types/StatCollector.java
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);

}
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);
}
}
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 src/main/java/net/zomis/fight/statextract/types/TypeHelp.java
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];
}

}

0 comments on commit a827f49

Please sign in to comment.