| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package be.objectify.led.factory.object; |
| 17 | |
|
| 18 | |
import be.objectify.led.FactoryResolver; |
| 19 | |
import be.objectify.led.ObjectFactory; |
| 20 | |
import org.slf4j.Logger; |
| 21 | |
import org.slf4j.LoggerFactory; |
| 22 | |
|
| 23 | |
import java.lang.reflect.Field; |
| 24 | |
import java.util.Collection; |
| 25 | |
import java.util.Set; |
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | 4 | public abstract class SetFactory<T> extends AbstractObjectFactory<Set> |
| 31 | |
{ |
| 32 | 1 | private static final Logger LOGGER = LoggerFactory.getLogger(SetFactory.class); |
| 33 | |
|
| 34 | |
private final Class clazz; |
| 35 | |
|
| 36 | |
private final Field field; |
| 37 | |
|
| 38 | |
private final FactoryResolver factoryResolver; |
| 39 | |
|
| 40 | |
public SetFactory(Class clazz, |
| 41 | |
Field field, |
| 42 | |
FactoryResolver factoryResolver) |
| 43 | 4 | { |
| 44 | 4 | this.clazz = clazz; |
| 45 | 4 | this.field = field; |
| 46 | 4 | this.factoryResolver = factoryResolver; |
| 47 | 4 | } |
| 48 | |
|
| 49 | |
public Set<T> createObject(String propertyName, |
| 50 | |
String propertyValue) |
| 51 | |
{ |
| 52 | 4 | Set<T> set = createSet(); |
| 53 | 4 | ObjectFactory objectFactory = factoryResolver.resolveFactory(clazz, |
| 54 | |
field); |
| 55 | 4 | if (objectFactory == null) |
| 56 | |
{ |
| 57 | 0 | LOGGER.info("No factory available for type [{}]", |
| 58 | |
clazz.getCanonicalName()); |
| 59 | |
} |
| 60 | |
else |
| 61 | |
{ |
| 62 | 4 | Collection<T> values = parse(propertyName, |
| 63 | |
propertyValue, |
| 64 | |
objectFactory); |
| 65 | 4 | if (values != null) |
| 66 | |
{ |
| 67 | 4 | set.addAll(values); |
| 68 | |
} |
| 69 | |
else |
| 70 | |
{ |
| 71 | 0 | LOGGER.info("Property value [{}] did not parse into collection of type [{}]", |
| 72 | |
propertyValue, |
| 73 | |
clazz.getCanonicalName()); |
| 74 | |
} |
| 75 | |
} |
| 76 | |
|
| 77 | 4 | return set; |
| 78 | |
} |
| 79 | |
|
| 80 | |
public Class<Set> getBoundClass() |
| 81 | |
{ |
| 82 | 0 | return Set.class; |
| 83 | |
} |
| 84 | |
|
| 85 | |
protected abstract Collection<T> parse(String propertyName, |
| 86 | |
String propertyValue, |
| 87 | |
ObjectFactory<T> objectFactory); |
| 88 | |
|
| 89 | |
protected abstract Set<T> createSet(); |
| 90 | |
} |