StatechartEqualityHelper.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * Copyright (c) 2013 committers of YAKINDU and others.
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * http://www.eclipse.org/legal/epl-v10.html
  7. * Contributors:
  8. * committers of YAKINDU - initial API and implementation
  9. *
  10. */
  11. package org.yakindu.sct.refactoring.refactor.util;
  12. import org.eclipse.emf.ecore.EAttribute;
  13. import org.eclipse.emf.ecore.EObject;
  14. import org.eclipse.emf.ecore.util.EcoreUtil;
  15. import org.eclipse.emf.ecore.util.EcoreUtil.EqualityHelper;
  16. import org.eclipse.emf.ecore.util.FeatureMap;
  17. import org.eclipse.emf.ecore.util.FeatureMapUtil;
  18. /**
  19. * Modified {@link EqualityHelper} for test cases to ignore whitespaces in string comparisons and
  20. * resolve proxy elements when necessary.
  21. *
  22. * @author thomas kutz - Initial contribution and API
  23. *
  24. */
  25. @SuppressWarnings("serial")
  26. public class StatechartEqualityHelper extends EqualityHelper{
  27. @Override
  28. public boolean equals(EObject eObject1, EObject eObject2) {
  29. if (eObject1!=null && eObject1.eIsProxy()) {
  30. EcoreUtil.resolve(eObject1, eObject2.eResource());
  31. }
  32. if (eObject2!=null && eObject2.eIsProxy()) {
  33. EcoreUtil.resolve(eObject2, eObject1.eResource());
  34. }
  35. return super.equals(eObject1, eObject2);
  36. }
  37. @Override
  38. protected boolean haveEqualAttribute(EObject eObject1, EObject eObject2,
  39. EAttribute attribute) {
  40. Object value1 = eObject1.eGet(attribute);
  41. Object value2 = eObject2.eGet(attribute);
  42. // If the first value is null, the second value must be null.
  43. //
  44. if (value1 == null) {
  45. return value2 == null;
  46. }
  47. // Since the first value isn't null, if the second one is, they aren't equal.
  48. //
  49. if (value2 == null) {
  50. return false;
  51. }
  52. // If this is a feature map...
  53. //
  54. if (FeatureMapUtil.isFeatureMap(attribute)) {
  55. // The feature maps must be equal.
  56. //
  57. FeatureMap featureMap1 = (FeatureMap)value1;
  58. FeatureMap featureMap2 = (FeatureMap)value2;
  59. return equalFeatureMaps(featureMap1, featureMap2);
  60. }
  61. else {
  62. if (value1 instanceof String && value2 instanceof String) {
  63. String wsFreeValue1 = removeWhitespaces((String)value1);
  64. String wsFreeValue2 = removeWhitespaces((String)value2);
  65. return wsFreeValue1.equals(wsFreeValue2);
  66. }
  67. return value1.equals(value2);
  68. }
  69. }
  70. private String removeWhitespaces(String input) {
  71. return input.replaceAll("\\s", "");
  72. }
  73. }