DiffEntry.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. namespace Quadarax.Foundation.Core.Data.Diff
  2. {
  3. public abstract class DiffEntry
  4. {
  5. public enum CompareStateEnum
  6. {
  7. NotInitialized,
  8. BothNull,
  9. CurrentNull,
  10. OtherNull,
  11. CurrentSmaller,
  12. CurrentGrater,
  13. Equals,
  14. DifferentKey
  15. }
  16. public object Item { get; private set; }
  17. public CompareStateEnum CompareState { get; private set; }
  18. protected DiffEntry(object item)
  19. {
  20. SetItem(item);
  21. }
  22. protected DiffEntry()
  23. {
  24. CompareState = CompareStateEnum.NotInitialized;
  25. }
  26. public abstract object GetKey();
  27. internal void SetItem(object item)
  28. {
  29. Item = item;
  30. }
  31. /// <summary>
  32. ///
  33. /// </summary>
  34. /// <param name="other"></param>
  35. /// <returns>-1 = both null, 0 - current null, 1 - other null, 2 - current smaller, 3 - current bigger, 4 - equals, 5 - different key</returns>
  36. public CompareStateEnum Compare(DiffEntry other)
  37. {
  38. if (other == null && Item == null)
  39. return SetCompareState(CompareStateEnum.BothNull);
  40. if (Item == null)
  41. return SetCompareState(CompareStateEnum.CurrentNull);
  42. if (other == null)
  43. return SetCompareState(CompareStateEnum.OtherNull);
  44. if (!Equals(GetKey(), other.GetKey()))
  45. return SetCompareState(CompareStateEnum.DifferentKey);
  46. return OnCompareEquality(other);
  47. }
  48. /// <summary>
  49. ///
  50. /// </summary>
  51. /// <param name="other"></param>
  52. /// <returns>2 - current smaller, 3 - current bigger, 4 - equals</returns>
  53. protected abstract CompareStateEnum OnCompareEquality(DiffEntry other);
  54. internal CompareStateEnum SetCompareState(CompareStateEnum state)
  55. {
  56. CompareState = state;
  57. return state;
  58. }
  59. }
  60. }