Skip to content

Review changes to deal with cycles #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/main/java/de/danielbechler/diff/BeanDiffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package de.danielbechler.diff;

import java.util.HashSet;
import java.util.Set;

import de.danielbechler.diff.accessor.*;
import de.danielbechler.diff.introspect.*;
import de.danielbechler.diff.node.*;
Expand All @@ -29,6 +32,7 @@
final class BeanDiffer extends AbstractDiffer
{
private Introspector introspector = new StandardIntrospector();
private Set<InstanceOutline> visitedInstances = new HashSet<InstanceOutline>();

BeanDiffer()
{
Expand Down Expand Up @@ -64,6 +68,8 @@ else if (instances.getType() == null)
}
else
{
if(checkAlreadyVisited(instances))
return node;
return compareBean(parentNode, instances);
}
return node;
Expand Down Expand Up @@ -143,4 +149,18 @@ void setIntrospector(final Introspector introspector)
Assert.notNull(introspector, "introspector");
this.introspector = introspector;
}

private boolean checkAlreadyVisited(Instances instances)
{
InstanceOutline outline = InstanceOutline.from(instances);
if(outline != null) {
if(visitedInstances.contains(outline)) {
// if line below is commented in test case bidirectionalGraphStackOverflow creates a stack overflow
// visitedInstances.remove(outline);
return true;
}
visitedInstances.add(outline);
}
return false;
}
}
69 changes: 69 additions & 0 deletions src/main/java/de/danielbechler/diff/InstanceOutline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package de.danielbechler.diff;

public class InstanceOutline {

private Object comparisonObject;
private Object working;
private Object base;

public InstanceOutline() {
super();
}

public InstanceOutline(Object comparisonObject, Object working, Object base) {
super();
this.comparisonObject = comparisonObject;
this.working = working;
this.base = base;
}


public static InstanceOutline from(Instances instances) {
if(instances.getWorking() == null)
return null;
if(instances.getBase() == null)
return null;
Object comparisonObject = instances.getComparisonObject();
if(comparisonObject == null)
return null;
return new InstanceOutline(comparisonObject, instances.getWorking(), instances.getBase());
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((base == null) ? 0 : base.hashCode());
result = prime * result + ((comparisonObject == null) ? 0 : comparisonObject.hashCode());
result = prime * result + ((working == null) ? 0 : working.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
InstanceOutline other = (InstanceOutline) obj;
if (base == null) {
if (other.base != null)
return false;
} else if (!base.equals(other.base))
return false;
if (comparisonObject == null) {
if (other.comparisonObject != null)
return false;
} else if (!comparisonObject.equals(other.comparisonObject))
return false;
if (working == null) {
if (other.working != null)
return false;
} else if (!working.equals(other.working))
return false;
return true;
}

}
7 changes: 7 additions & 0 deletions src/main/java/de/danielbechler/diff/Instances.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,11 @@ public PropertyPath getPropertyPath(final Node parentNode)
.withElement(sourceAccessor.getPathElement())
.build();
}

public Object getComparisonObject()
{
if(sourceAccessor == null)
return null;
return sourceAccessor.getComparisonObject();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ public void setIgnored(final boolean ignored)
{
this.ignored = ignored;
}

public Object getComparisonObject() {
// answer the default, redefine in subclass as appropriate
return null;
}
}
2 changes: 2 additions & 0 deletions src/main/java/de/danielbechler/diff/accessor/Accessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ public interface Accessor extends PropertyDescriptor
void set(Object target, Object value);

void unset(Object target);

Object getComparisonObject();
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,9 @@ public void unset(final Object target)
targetCollection.remove(referenceItem);
}
}

@Override
public Object getComparisonObject() {
return referenceItem;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,9 @@ public void unset(final Object target)
targetMap.remove(getReferenceKey());
}
}

@Override
public Object getComparisonObject() {
return getReferenceKey();
}
}
10 changes: 10 additions & 0 deletions src/main/java/de/danielbechler/diff/accessor/PropertyAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import de.danielbechler.diff.accessor.exception.*;
import de.danielbechler.diff.path.*;
import de.danielbechler.util.*;

import org.slf4j.*;

import java.lang.reflect.*;
Expand Down Expand Up @@ -189,4 +190,13 @@ public Element getPathElement()
{
return new NamedPropertyElement(this.propertyName);
}

@Override
public Object getComparisonObject() {
if(Classes.isSimpleType(getType())) {
// ignore, because no comparison on the level of user-defined objects
return null;
}
return getPropertyName();
}
}
6 changes: 6 additions & 0 deletions src/main/java/de/danielbechler/diff/node/DefaultNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -387,4 +387,10 @@ else if (getChildren().size() > 1)
sb.append(" }");
return sb.toString();
}

@Override
public Object getComparisonObject() {
// not needed here, but has to be implemented, because of the Accessor interface
throw new UnsupportedOperationException();
}
}
Loading