Skip to content

Commit

Permalink
Remove NodeField interface and add equivalent methods to NodeClass in…
Browse files Browse the repository at this point in the history
…stead
  • Loading branch information
woess committed May 3, 2016
1 parent 79fa52b commit c3556d9
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 188 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -488,19 +488,19 @@ private void copyDebugProperties(Node node) {

private void readNodeProperties(Node node) {
NodeClass nodeClass = NodeClass.get(node);
for (NodeClass.NodeField field : nodeClass.getNodeFields()) {
if (isDataField(field)) {
String key = field.getName();
for (Object field : nodeClass.getNodeFields()) {
if (isDataField(nodeClass, field)) {
String key = nodeClass.getFieldName(field);
if (!getElementByObject(node).getProperties().containsKey(key)) {
Object value = field.getValue(node);
Object value = nodeClass.getFieldValue(field, node);
setNodeProperty(node, key, value);
}
}
}
}

private static boolean isDataField(NodeClass.NodeField field) {
return !field.isChildField() && !field.isChildrenField();
private static boolean isDataField(NodeClass nodeClass, Object field) {
return !nodeClass.isChildField(field) && !nodeClass.isChildrenField(field);
}

final void connectNodes(Object a, Object b, String label) {
Expand Down Expand Up @@ -567,19 +567,19 @@ private static LinkedHashMap<String, Node> findNamedNodeChildren(Node node) {
LinkedHashMap<String, Node> nodes = new LinkedHashMap<>();
NodeClass nodeClass = NodeClass.get(node);

for (NodeClass.NodeField field : nodeClass.getNodeFields()) {
if (field.isChildField()) {
Object value = field.getObject(node);
for (Object field : nodeClass.getNodeFields()) {
if (nodeClass.isChildField(field)) {
Object value = nodeClass.getFieldObject(field, node);
if (value != null) {
nodes.put(field.getName(), (Node) value);
nodes.put(nodeClass.getFieldName(field), (Node) value);
}
} else if (field.isChildrenField()) {
Object value = field.getObject(node);
} else if (nodeClass.isChildrenField(field)) {
Object value = nodeClass.getFieldObject(field, node);
if (value != null) {
Object[] children = (Object[]) value;
for (int i = 0; i < children.length; i++) {
if (children[i] != null) {
nodes.put(field.getName() + "[" + i + "]", (Node) children[i]);
nodes.put(nodeClass.getFieldName(field) + "[" + i + "]", (Node) children[i]);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ public NodeClass(@SuppressWarnings("unused") Class<? extends Node> clazz) {
@Deprecated
public abstract NodeFieldAccessor[] getChildrenFields();

/** @since 0.14 */
protected abstract Iterable<? extends NodeField> getNodeFields();

/** @since 0.8 or earlier */
public abstract Iterator<Node> makeIterator(Node node);

Expand All @@ -107,6 +104,33 @@ public NodeClass(@SuppressWarnings("unused") Class<? extends Node> clazz) {
*/
public abstract Class<? extends Node> getType();

/** @since 0.14 */
protected abstract Iterable<? extends Object> getNodeFields();

/** @since 0.14 */
protected abstract void putFieldObject(Object field, Node receiver, Object value);

/** @since 0.14 */
protected abstract Object getFieldObject(Object field, Node receiver);

/** @since 0.14 */
protected abstract Object getFieldValue(Object field, Node receiver);

/** @since 0.14 */
protected abstract Class<?> getFieldType(Object field);

/** @since 0.14 */
protected abstract String getFieldName(Object field);

/** @since 0.14 */
protected abstract boolean isChildField(Object field);

/** @since 0.14 */
protected abstract boolean isChildrenField(Object field);

/** @since 0.14 */
protected abstract boolean isCloneableField(Object field);

/**
* If and only if this method returns {@code true}, {@link #getNodeFields()} adheres to the
* following iteration order.
Expand All @@ -120,31 +144,4 @@ public NodeClass(@SuppressWarnings("unused") Class<? extends Node> clazz) {
boolean nodeFieldsOrderedByKind() {
return false;
}

/** @since 0.14 */
protected interface NodeField {
/** @since 0.14 */
void putObject(Node receiver, Object value);

/** @since 0.14 */
Object getObject(Node receiver);

/** @since 0.14 */
Object getValue(Node receiver);

/** @since 0.14 */
Class<?> getFieldType();

/** @since 0.14 */
String getName();

/** @since 0.14 */
boolean isChildField();

/** @since 0.14 */
boolean isChildrenField();

/** @since 0.14 */
boolean isCloneableField();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@
*/
@SuppressWarnings("deprecation")
final class NodeClassImpl extends NodeClass {
private static final NodeField[] EMPTY_NODE_FIELD_ARRAY = new NodeField[0];
private static final NodeFieldAccessor[] EMPTY_NODE_FIELD_ARRAY = new NodeFieldAccessor[0];

// The comprehensive list of all fields.
private final NodeField[] fields;
private final NodeFieldAccessor[] fields;
private final NodeFieldAccessor parentField;
private final NodeFieldAccessor nodeClassField;

Expand All @@ -57,7 +57,7 @@ final class NodeClassImpl extends NodeClass {
throw new IllegalArgumentException();
}

List<NodeField> fieldsList = new ArrayList<>();
List<NodeFieldAccessor> fieldsList = new ArrayList<>();
NodeFieldAccessor parentFieldTmp = null;
NodeFieldAccessor nodeClassFieldTmp = null;

Expand All @@ -74,13 +74,13 @@ final class NodeClassImpl extends NodeClass {

collectInstanceFields(clazz, fieldsList);

Collections.sort(fieldsList, new Comparator<NodeField>() {
public int compare(NodeField o1, NodeField o2) {
Collections.sort(fieldsList, new Comparator<NodeFieldAccessor>() {
public int compare(NodeFieldAccessor o1, NodeFieldAccessor o2) {
return Integer.compare(order(o1), order(o2));
}

private int order(NodeField nodeField) {
return nodeField.isChildField() ? 0 : (nodeField.isChildrenField() ? 1 : (nodeField.isCloneableField() ? 2 : 3));
private int order(NodeFieldAccessor nodeField) {
return isChildField(nodeField) ? 0 : (isChildrenField(nodeField) ? 1 : (isCloneableField(nodeField) ? 2 : 3));
}
});

Expand All @@ -90,7 +90,7 @@ private int order(NodeField nodeField) {
this.clazz = clazz;
}

private static void collectInstanceFields(Class<? extends Object> clazz, List<NodeField> fieldsList) {
private static void collectInstanceFields(Class<? extends Object> clazz, List<NodeFieldAccessor> fieldsList) {
if (clazz.getSuperclass() != null) {
collectInstanceFields(clazz.getSuperclass(), fieldsList);
}
Expand All @@ -112,7 +112,7 @@ private static void collectInstanceFields(Class<? extends Object> clazz, List<No
} else {
nodeField = NodeFieldAccessor.create(NodeFieldAccessor.NodeFieldKind.DATA, field);
}
fieldsList.add((NodeField) nodeField);
fieldsList.add(nodeField);
}
}

Expand Down Expand Up @@ -174,29 +174,29 @@ public Class<? extends Node> getType() {
}

@Override
protected Iterable<NodeField> getNodeFields() {
protected Iterable<NodeFieldAccessor> getNodeFields() {
return getNodeFields(null);
}

/**
* Functional interface equivalent to {@code Predicate<NodeFieldAccessor>}.
*/
private interface NodeFieldFilter {
boolean test(NodeField field);
boolean test(NodeFieldAccessor field);
}

private Iterable<NodeField> getNodeFields(final NodeFieldFilter filter) {
return new Iterable<NodeField>() {
public Iterator<NodeField> iterator() {
return new Iterator<NodeField>() {
private Iterable<NodeFieldAccessor> getNodeFields(final NodeFieldFilter filter) {
return new Iterable<NodeFieldAccessor>() {
public Iterator<NodeFieldAccessor> iterator() {
return new Iterator<NodeFieldAccessor>() {
private int cursor = -1;
{
forward();
}

private void forward() {
for (int i = cursor + 1; i < fields.length; i++) {
NodeField field = fields[i];
NodeFieldAccessor field = fields[i];
if (filter == null || filter.test(field)) {
cursor = i;
return;
Expand All @@ -210,9 +210,9 @@ public boolean hasNext() {
return cursor < fields.length;
}

public NodeField next() {
public NodeFieldAccessor next() {
if (hasNext()) {
NodeField next = fields[cursor];
NodeFieldAccessor next = fields[cursor];
forward();
return next;
} else {
Expand All @@ -236,45 +236,86 @@ public NodeFieldAccessor[] getFields() {
@Override
public NodeFieldAccessor[] getChildFields() {
return iterableToArray(getNodeFields(new NodeFieldFilter() {
public boolean test(NodeField field) {
return field.isChildField();
public boolean test(NodeFieldAccessor field) {
return isChildField(field);
}
}));
}

@Override
public NodeFieldAccessor[] getChildrenFields() {
return iterableToArray(getNodeFields(new NodeFieldFilter() {
public boolean test(NodeField field) {
return field.isChildrenField();
public boolean test(NodeFieldAccessor field) {
return isChildrenField(field);
}
}));
}

@Override
public NodeFieldAccessor[] getCloneableFields() {
return iterableToArray(getNodeFields(new NodeFieldFilter() {
public boolean test(NodeField field) {
return field.isCloneableField();
public boolean test(NodeFieldAccessor field) {
return isCloneableField(field);
}
}));
}

private static NodeFieldAccessor[] iterableToArray(Iterable<NodeField> fields) {
private static NodeFieldAccessor[] iterableToArray(Iterable<NodeFieldAccessor> fields) {
ArrayList<NodeFieldAccessor> fieldList = new ArrayList<>();
for (NodeField field : fields) {
fieldList.add((NodeFieldAccessor) field);
for (NodeFieldAccessor field : fields) {
fieldList.add(field);
}
return fieldList.toArray(new NodeFieldAccessor[0]);
}

@Override
protected void putFieldObject(Object field, Node receiver, Object value) {
((NodeFieldAccessor) field).putObject(receiver, value);
}

@Override
protected Object getFieldObject(Object field, Node receiver) {
return ((NodeFieldAccessor) field).getObject(receiver);
}

@Override
protected Object getFieldValue(Object field, Node receiver) {
return ((NodeFieldAccessor) field).loadValue(receiver);
}

@Override
protected Class<?> getFieldType(Object field) {
return ((NodeFieldAccessor) field).getType();
}

@Override
protected String getFieldName(Object field) {
return ((NodeFieldAccessor) field).getName();
}

@Override
protected boolean isChildField(Object field) {
return ((NodeFieldAccessor) field).getKind() == NodeFieldAccessor.NodeFieldKind.CHILD;
}

@Override
protected boolean isChildrenField(Object field) {
return ((NodeFieldAccessor) field).getKind() == NodeFieldAccessor.NodeFieldKind.CHILDREN;
}

@Override
protected boolean isCloneableField(Object field) {
return ((NodeFieldAccessor) field).getKind() == NodeFieldAccessor.NodeFieldKind.DATA && NodeCloneable.class.isAssignableFrom(((NodeFieldAccessor) field).getType());
}

@Override
boolean nodeFieldsOrderedByKind() {
return true;
}

private static final class NodeIterator implements Iterator<Node> {
private final NodeField[] fields;
private final NodeClassImpl nodeClass;
private final NodeFieldAccessor[] fields;
private final Node node;

private int fieldIndex;
Expand All @@ -283,6 +324,7 @@ private static final class NodeIterator implements Iterator<Node> {
private Object[] children;

protected NodeIterator(NodeClassImpl nodeClass, Node node) {
this.nodeClass = nodeClass;
this.fields = nodeClass.fields;
this.node = node;
advance();
Expand All @@ -293,17 +335,19 @@ private void advance() {
return;
}
while (fieldIndex < fields.length) {
NodeField field = fields[fieldIndex];
NodeFieldAccessor field = fields[fieldIndex];
fieldIndex++;
if (field.isChildField()) {
next = (Node) field.getObject(node);
if (nodeClass.isChildField(field)) {
next = (Node) nodeClass.getFieldObject(field, node);
return;
} else if (field.isChildrenField()) {
children = (Object[]) field.getObject(node);
} else if (nodeClass.isChildrenField(field)) {
children = (Object[]) nodeClass.getFieldObject(field, node);
childrenIndex = 0;
if (advanceChildren()) {
return;
}
} else if (nodeClass.nodeFieldsOrderedByKind()) {
break;
}
}
next = null;
Expand Down
Loading

0 comments on commit c3556d9

Please sign in to comment.