Skip to main content
summaryrefslogtreecommitdiffstats
blob: ff7c6a7053a030b7bff9fbbaba7890e06caae399 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*******************************************************************************
 * Copyright (c) 2016-2017, Thales Global Services S.A.S.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Abel Hegedus, Tamas Borbas, Peter Lunk, Daniel Segesdi (IncQuery Labs Ltd.) - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.diffmerge.patch.api

import org.eclipse.emf.diffmerge.patch.AttributeEntry
import org.eclipse.emf.diffmerge.patch.ElementEntry
import org.eclipse.emf.diffmerge.patch.ReferenceEntry

class ModelPatchReverser {
  public static val INSTANCE = new ModelPatchReverser()

  public def ModelPatch reverse(ModelPatch originalPatch) throws ModelPatchException {
    val reverseBuilder = ModelPatchBuilder.create
    originalPatch.entries.reverseView.forEach[ originalEntry |
      reverseBuilder.addEntry(originalEntry.reverseEntry)
    ]
    return reverseBuilder.build
  }



  public dispatch def ModelPatchEntry reverseEntry(ModelPatchEntry originalEntry) throws ModelPatchException {
    throw new ModelPatchException("Unhandled entry type")
  }

  public dispatch def ModelPatchEntry reverseEntry(AttributeEntry originalEntry) {
    val reverseEntryBuilder = originalEntry.reverseEntryBuilder => [
      it.feature = originalEntry.feature.cloneIdentifiable
      it.value = originalEntry.value
      it.index = originalEntry.index
    ]
    return reverseEntryBuilder.buildAttributeEntry
  }

  public dispatch def ModelPatchEntry reverseEntry(ElementEntry originalEntry) {
    val reverseEntryBuilder = originalEntry.reverseEntryBuilder => [
      it.type = originalEntry.type.cloneIdentifiable
    ]
    return reverseEntryBuilder.buildElementEntry
  }

  public dispatch def ModelPatchEntry reverseEntry(ReferenceEntry originalEntry) {
    val reverseEntryBuilder = originalEntry.reverseEntryBuilder => [
      it.feature = originalEntry.feature.cloneIdentifiable
      it.target = originalEntry.target.cloneIdentifiable
      it.index = originalEntry.index
    ]
    return reverseEntryBuilder.buildReferenceEntry
  }



  public def ChangeDirection reverseDirection(ChangeDirection original) {
    if(ChangeDirection.ADD==original) {
      return ChangeDirection.REMOVE
    } else {
      return ChangeDirection.ADD
    }
  }



  private def reverseEntryBuilder(ModelPatchEntry originalEntry) {
    return ModelPatchBuilder.entryBuilder(
      originalEntry.context.cloneIdentifiable,
      originalEntry.direction.reverseDirection
    )
  }

  private def Identifiable cloneIdentifiable(Identifiable original) {
    return new Identifiable(original.identifier)
  }
}

Back to the top