forked from matsim-org/matsim-libs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add attributes comparison
- Loading branch information
Showing
3 changed files
with
110 additions
and
10 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
41 changes: 41 additions & 0 deletions
41
...im/src/main/java/org/matsim/utils/objectattributes/attributable/AttributesComparison.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,41 @@ | ||
package org.matsim.utils.objectattributes.attributable; | ||
|
||
import com.google.common.base.Equivalence; | ||
import com.google.common.collect.Maps; | ||
import com.google.common.collect.Sets; | ||
import org.matsim.vehicles.PersonVehicles; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @author nkuehnel / MOIA | ||
*/ | ||
public final class AttributesComparison { | ||
|
||
private AttributesComparison(){} | ||
|
||
public static boolean equals(Attributes a1, Attributes a2) { | ||
if(a1.size() != a2.size()) { | ||
return false; | ||
} | ||
|
||
return Maps.difference(a1.getAsMap(), a2.getAsMap(), new CustomEquivalence()).areEqual(); | ||
} | ||
|
||
private static class CustomEquivalence extends Equivalence<Object> { | ||
@Override | ||
protected boolean doEquivalent(Object a, Object b) { | ||
if (a instanceof Map<?, ?> mapA && b instanceof Map<?, ?> mapB) { | ||
return Maps.difference(mapA, mapB, new CustomEquivalence()).areEqual(); | ||
} else if (a instanceof PersonVehicles vehiclesA && b instanceof PersonVehicles vehiclesB) { | ||
return Maps.difference(vehiclesA.getModeVehicles(), vehiclesB.getModeVehicles(), new CustomEquivalence()).areEqual(); | ||
} | ||
return a.equals(b); | ||
} | ||
|
||
@Override | ||
protected int doHash(Object o) { | ||
return o.hashCode(); | ||
} | ||
} | ||
} |
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