-
Notifications
You must be signed in to change notification settings - Fork 489
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from sanliangitch/fix-array
修复对象diff不支持数组
- Loading branch information
Showing
8 changed files
with
428 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
bizlog-sdk/src/main/java/com/mzt/logapi/util/diff/ArrayDiffer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package com.mzt.logapi.util.diff; | ||
|
||
import de.danielbechler.diff.access.Accessor; | ||
import de.danielbechler.diff.access.Instances; | ||
import de.danielbechler.diff.comparison.ComparisonStrategy; | ||
import de.danielbechler.diff.comparison.ComparisonStrategyResolver; | ||
import de.danielbechler.diff.differ.Differ; | ||
import de.danielbechler.diff.differ.DifferDispatcher; | ||
import de.danielbechler.diff.identity.IdentityStrategy; | ||
import de.danielbechler.diff.identity.IdentityStrategyResolver; | ||
import de.danielbechler.diff.node.DiffNode; | ||
import de.danielbechler.util.Assert; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* @author: wulang | ||
* @date: 2022-08-27 20:26 | ||
**/ | ||
public class ArrayDiffer implements Differ { | ||
private final DifferDispatcher differDispatcher; | ||
private final ComparisonStrategyResolver comparisonStrategyResolver; | ||
private final IdentityStrategyResolver identityStrategyResolver; | ||
|
||
public ArrayDiffer(DifferDispatcher differDispatcher, ComparisonStrategyResolver comparisonStrategyResolver, IdentityStrategyResolver identityStrategyResolver) { | ||
Assert.notNull(differDispatcher, "differDispatcher"); | ||
this.differDispatcher = differDispatcher; | ||
Assert.notNull(comparisonStrategyResolver, "comparisonStrategyResolver"); | ||
this.comparisonStrategyResolver = comparisonStrategyResolver; | ||
Assert.notNull(identityStrategyResolver, "identityStrategyResolver"); | ||
this.identityStrategyResolver = identityStrategyResolver; | ||
} | ||
|
||
@Override | ||
public boolean accepts(final Class<?> type) { | ||
return !type.isPrimitive() && type.isArray(); | ||
} | ||
|
||
@Override | ||
public final DiffNode compare(final DiffNode parentNode, final Instances collectionInstances) { | ||
final DiffNode collectionNode = newNode(parentNode, collectionInstances); | ||
final IdentityStrategy identityStrategy = identityStrategyResolver.resolveIdentityStrategy(collectionNode); | ||
if (identityStrategy != null) { | ||
collectionNode.setChildIdentityStrategy(identityStrategy); | ||
} | ||
if (collectionInstances.hasBeenAdded()) { | ||
final Collection<?> addedItems = findCollection(collectionInstances.getWorking()); | ||
compareItems(collectionNode, collectionInstances, addedItems, identityStrategy); | ||
collectionNode.setState(DiffNode.State.ADDED); | ||
} else if (collectionInstances.hasBeenRemoved()) { | ||
final Collection<?> removedItems = findCollection(collectionInstances.getBase()); | ||
compareItems(collectionNode, collectionInstances, removedItems, identityStrategy); | ||
collectionNode.setState(DiffNode.State.REMOVED); | ||
} else if (collectionInstances.areSame()) { | ||
collectionNode.setState(DiffNode.State.UNTOUCHED); | ||
} else { | ||
final ComparisonStrategy comparisonStrategy = comparisonStrategyResolver.resolveComparisonStrategy(collectionNode); | ||
if (comparisonStrategy == null) { | ||
compareInternally(collectionNode, collectionInstances, identityStrategy); | ||
} else { | ||
compareUsingComparisonStrategy(collectionNode, collectionInstances, comparisonStrategy); | ||
} | ||
} | ||
return collectionNode; | ||
} | ||
|
||
private Collection<?> findCollection(Object source) { | ||
return source == null ? new ArrayList<>() : new LinkedList<>(Arrays.asList((Object[]) source)); | ||
} | ||
|
||
private static DiffNode newNode(final DiffNode parentNode, | ||
final Instances collectionInstances) { | ||
final Accessor accessor = collectionInstances.getSourceAccessor(); | ||
final Class<?> type = collectionInstances.getType(); | ||
return new DiffNode(parentNode, accessor, type); | ||
} | ||
|
||
private void compareItems(final DiffNode collectionNode, | ||
final Instances collectionInstances, | ||
final Iterable<?> items, | ||
final IdentityStrategy identityStrategy) { | ||
for (final Object item : items) { | ||
final Accessor itemAccessor = new ArrayItemAccessor(item, identityStrategy); | ||
differDispatcher.dispatch(collectionNode, collectionInstances, itemAccessor); | ||
} | ||
} | ||
|
||
private void compareInternally(final DiffNode collectionNode, | ||
final Instances collectionInstances, | ||
final IdentityStrategy identityStrategy) { | ||
final Collection<?> working = Arrays.asList((Object[]) collectionInstances.getWorking()); | ||
final Collection<?> base = Arrays.asList((Object[]) collectionInstances.getBase()); | ||
|
||
final Iterable<?> added = new LinkedList<Object>(working); | ||
final Iterable<?> removed = new LinkedList<Object>(base); | ||
final Iterable<?> known = new LinkedList<Object>(base); | ||
|
||
remove(added, base, identityStrategy); | ||
remove(removed, working, identityStrategy); | ||
remove(known, added, identityStrategy); | ||
remove(known, removed, identityStrategy); | ||
|
||
compareItems(collectionNode, collectionInstances, added, identityStrategy); | ||
compareItems(collectionNode, collectionInstances, removed, identityStrategy); | ||
compareItems(collectionNode, collectionInstances, known, identityStrategy); | ||
} | ||
|
||
private static void compareUsingComparisonStrategy(final DiffNode collectionNode, | ||
final Instances collectionInstances, | ||
final ComparisonStrategy comparisonStrategy) { | ||
comparisonStrategy.compare(collectionNode, | ||
collectionInstances.getType(), | ||
collectionInstances.getWorking(Collection.class), | ||
collectionInstances.getBase(Collection.class)); | ||
} | ||
|
||
private void remove(final Iterable<?> from, final Iterable<?> these, final IdentityStrategy identityStrategy) { | ||
final Iterator<?> iterator = from.iterator(); | ||
while (iterator.hasNext()) { | ||
final Object item = iterator.next(); | ||
if (contains(these, item, identityStrategy)) { | ||
iterator.remove(); | ||
} | ||
} | ||
} | ||
|
||
private boolean contains(final Iterable<?> haystack, final Object needle, final IdentityStrategy identityStrategy) { | ||
for (final Object item : haystack) { | ||
if (identityStrategy.equals(needle, item)) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
bizlog-sdk/src/main/java/com/mzt/logapi/util/diff/ArrayItemAccessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package com.mzt.logapi.util.diff; | ||
|
||
import de.danielbechler.diff.access.Accessor; | ||
import de.danielbechler.diff.access.TypeAwareAccessor; | ||
import de.danielbechler.diff.identity.EqualsIdentityStrategy; | ||
import de.danielbechler.diff.identity.IdentityStrategy; | ||
import de.danielbechler.diff.selector.CollectionItemElementSelector; | ||
import de.danielbechler.diff.selector.ElementSelector; | ||
import org.springframework.util.Assert; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
|
||
/** | ||
* @author: wulang | ||
* @date: 2022-08-27 20:34 | ||
**/ | ||
public class ArrayItemAccessor implements TypeAwareAccessor, Accessor { | ||
|
||
private final Object referenceItem; | ||
private final IdentityStrategy identityStrategy; | ||
|
||
/** | ||
* Default implementation uses IdentityService.EQUALS_IDENTITY_STRATEGY. | ||
* | ||
* @param referenceItem | ||
*/ | ||
public ArrayItemAccessor(final Object referenceItem) { | ||
this(referenceItem, EqualsIdentityStrategy.getInstance()); | ||
} | ||
|
||
/** | ||
* Allows for custom IdentityStrategy. | ||
* | ||
* @param referenceItem | ||
* @param identityStrategy | ||
*/ | ||
public ArrayItemAccessor(final Object referenceItem, | ||
final IdentityStrategy identityStrategy) { | ||
Assert.notNull(identityStrategy, "identityStrategy"); | ||
this.referenceItem = referenceItem; | ||
this.identityStrategy = identityStrategy; | ||
} | ||
|
||
@Override | ||
public Class<?> getType() { | ||
return referenceItem != null ? referenceItem.getClass() : null; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "collection item " + getElementSelector(); | ||
} | ||
|
||
@Override | ||
public ElementSelector getElementSelector() { | ||
final CollectionItemElementSelector selector = new CollectionItemElementSelector(referenceItem); | ||
return identityStrategy == null ? selector : selector.copyWithIdentityStrategy(identityStrategy); | ||
} | ||
|
||
@Override | ||
public Object get(final Object target) { | ||
final Collection targetCollection = objectAsCollection(target); | ||
if (targetCollection == null) { | ||
return null; | ||
} | ||
for (final Object item : targetCollection) { | ||
if (item != null && identityStrategy.equals(item, referenceItem)) { | ||
return item; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Override | ||
public void set(final Object target, final Object value) { | ||
final Collection<Object> targetCollection = objectAsCollection(target); | ||
if (targetCollection == null) { | ||
return; | ||
} | ||
final Object previous = get(target); | ||
if (previous != null) { | ||
unset(target); | ||
} | ||
targetCollection.add(value); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private static Collection<Object> objectAsCollection(final Object object) { | ||
if (object == null) { | ||
return null; | ||
} else if (object.getClass().isArray()) { | ||
return new ArrayList<>(Arrays.asList((Object[]) object)); | ||
} | ||
throw new IllegalArgumentException(object.getClass().toString()); | ||
} | ||
|
||
@Override | ||
public void unset(final Object target) { | ||
final Collection<?> targetCollection = objectAsCollection(target); | ||
if (targetCollection == null) { | ||
return; | ||
} | ||
final Iterator<?> iterator = targetCollection.iterator(); | ||
while (iterator.hasNext()) { | ||
final Object item = iterator.next(); | ||
if (item != null && identityStrategy.equals(item, referenceItem)) { | ||
iterator.remove(); | ||
break; | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
bizlog-server/src/main/java/com/mzt/logserver/function/ExtInfoParseFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.mzt.logserver.function; | ||
|
||
import com.mzt.logapi.service.IParseFunction; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* @author: wulang | ||
* @date: 2022-08-27 21:01 | ||
**/ | ||
@Slf4j | ||
@Component | ||
public class ExtInfoParseFunction implements IParseFunction { | ||
@Override | ||
public boolean executeBefore() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public String functionName() { | ||
return "extInfo"; | ||
} | ||
|
||
@Override | ||
public String apply(Object value) { | ||
log.info("==========="); | ||
if (StringUtils.isEmpty(value)) { | ||
return ""; | ||
} | ||
log.info("当前拓展信息值为,{}", value); | ||
return value.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.