Archiviert
13
0

Made structure modifiers more overridable.

Dieser Commit ist enthalten in:
Kristian S. Stangeland 2012-09-19 19:25:11 +02:00
Ursprung bc7b395889
Commit 54242debaa
3 geänderte Dateien mit 53 neuen und 22 gelöschten Zeilen

Datei anzeigen

@ -343,8 +343,6 @@ public final class PacketFilterManager implements ProtocolManager {
@Override @Override
public void updateEntity(Entity entity, List<Player> observers) throws FieldAccessException { public void updateEntity(Entity entity, List<Player> observers) throws FieldAccessException {
EntityUtilities.updateEntity(entity, observers); EntityUtilities.updateEntity(entity, observers);
} }

Datei anzeigen

@ -34,21 +34,21 @@ import com.google.common.collect.ImmutableList;
public class StructureModifier<TField> { public class StructureModifier<TField> {
// Object and its type // Object and its type
private Class targetType; protected Class targetType;
private Object target; protected Object target;
// Converter. May be NULL. // Converter. May be NULL.
private EquivalentConverter<TField> converter; protected EquivalentConverter<TField> converter;
// The fields to read in order // The fields to read in order
private Class fieldType; protected Class fieldType;
private List<Field> data = new ArrayList<Field>(); protected List<Field> data = new ArrayList<Field>();
// Improved default values // Improved default values
private Set<Field> defaultFields; protected Set<Field> defaultFields;
// Cache of previous types // Cache of previous types
private Map<Class, StructureModifier> subtypeCache; protected Map<Class, StructureModifier> subtypeCache;
/** /**
* Creates a structure modifier. * Creates a structure modifier.
@ -63,16 +63,20 @@ public class StructureModifier<TField> {
initialize(targetType, Object.class, fields, defaults, null, new HashMap<Class, StructureModifier>()); initialize(targetType, Object.class, fields, defaults, null, new HashMap<Class, StructureModifier>());
} }
private StructureModifier(StructureModifier<TField> other, Object target) {
initialize(other.targetType, other.fieldType, other.data, other.defaultFields, other.converter, other.subtypeCache);
this.target = target;
}
private StructureModifier() { private StructureModifier() {
// Consumers of this method should call "initialize" // Consumers of this method should call "initialize"
} }
private void initialize(Class targetType, Class fieldType, /**
* Initialize every field of this class.
* @param targetType - type of the object we're reading and writing from.
* @param fieldType - the common type of the fields we're modifying.
* @param data - list of fields to modify.
* @param defaultFields - list of fields that will be automatically initialized.
* @param converter - converts between the common field type and the actual type the consumer expects.
* @param subTypeCache - a structure modifier cache.
*/
protected void initialize(Class targetType, Class fieldType,
List<Field> data, Set<Field> defaultFields, List<Field> data, Set<Field> defaultFields,
EquivalentConverter<TField> converter, Map<Class, StructureModifier> subTypeCache) { EquivalentConverter<TField> converter, Map<Class, StructureModifier> subTypeCache) {
this.targetType = targetType; this.targetType = targetType;
@ -235,9 +239,7 @@ public class StructureModifier<TField> {
} }
// Cache structure modifiers // Cache structure modifiers
result = new StructureModifier<T>(); result = withFieldType(fieldType, filtered, defaults, converter);
result.initialize(targetType, fieldType, filtered, defaults,
converter, new HashMap<Class, StructureModifier>());
if (fieldType != null) if (fieldType != null)
subtypeCache.put(fieldType, result); subtypeCache.put(fieldType, result);
@ -296,13 +298,36 @@ public class StructureModifier<TField> {
return data.size(); return data.size();
} }
/**
* Create a new structure modifier for the new field type.
* @param fieldType - common type of each field.
* @param filtered - list of fields after filtering the original modifier.
* @param defaults - list of default values after filtering the original.
* @param converter - the new converter.
* @return A new structure modifier.
*/
protected <T> StructureModifier<T> withFieldType(
Class fieldType, List<Field> filtered,
Set<Field> defaults, EquivalentConverter<T> converter) {
StructureModifier<T> result = new StructureModifier<T>();
result.initialize(targetType, fieldType, filtered, defaults,
converter, new HashMap<Class, StructureModifier>());
return result;
}
/** /**
* Retrieves a structure modifier of the same type for a different object target. * Retrieves a structure modifier of the same type for a different object target.
* @param target - different target of the same type. * @param target - different target of the same type.
* @return Structure modifier with the new target. * @return Structure modifier with the new target.
*/ */
public StructureModifier<TField> withTarget(Object target) { public StructureModifier<TField> withTarget(Object target) {
return new StructureModifier<TField>(this, target); StructureModifier<TField> copy = new StructureModifier<TField>();
// Create a new instance
copy.initialize(targetType, fieldType, data, defaultFields, converter, subtypeCache);
copy.target = target;
return copy;
} }
/** /**
@ -312,12 +337,20 @@ public class StructureModifier<TField> {
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private <T> StructureModifier<T> withConverter(EquivalentConverter<T> converter) { private <T> StructureModifier<T> withConverter(EquivalentConverter<T> converter) {
StructureModifier copy = new StructureModifier(this, target); StructureModifier copy = withTarget(target);
copy.converter = converter; copy.setConverter(converter);
return copy; return copy;
} }
/**
* Set the current object converter. Should only be called during construction.
* @param converter - current object converter.
*/
protected void setConverter(EquivalentConverter<TField> converter) {
this.converter = converter;
}
/** /**
* Retrieves a list of the fields matching the constraints of this structure modifier. * Retrieves a list of the fields matching the constraints of this structure modifier.
* @return List of fields. * @return List of fields.