Skip to main content
summaryrefslogtreecommitdiffstats
blob: cb5020703e0ed5ee6b94c016230d21d3a236a5c1 (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
82
83
/***************************************************************************
 * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
 * 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:
 *    Simon McDuff - initial API and implementation
 **************************************************************************/

package org.eclipse.emf.internal.cdo.util;

import org.eclipse.emf.cdo.internal.protocol.revision.CDOIDProvider;
import org.eclipse.emf.cdo.internal.protocol.revision.InternalCDORevision;
import org.eclipse.emf.cdo.internal.protocol.revision.delta.CDOFeatureDeltaVisitorImpl;
import org.eclipse.emf.cdo.internal.protocol.revision.delta.CDOListFeatureDeltaImpl;
import org.eclipse.emf.cdo.protocol.model.CDOFeature;
import org.eclipse.emf.cdo.protocol.revision.CDOReferenceProxy;
import org.eclipse.emf.cdo.protocol.revision.delta.CDOAddFeatureDelta;
import org.eclipse.emf.cdo.protocol.revision.delta.CDOListFeatureDelta;
import org.eclipse.emf.cdo.protocol.revision.delta.CDORevisionDelta;
import org.eclipse.emf.cdo.protocol.revision.delta.CDOSetFeatureDelta;

import java.util.List;

/**
 * @author Simon McDuff
 */
public class RevisionAdjuster extends CDOFeatureDeltaVisitorImpl
{
  private CDOIDProvider idProvider;

  private InternalCDORevision revision;

  public RevisionAdjuster(CDOIDProvider idProvider)
  {
    this.idProvider = idProvider;
  }

  public void adjustRevision(InternalCDORevision revision, CDORevisionDelta revisionDelta)
  {
    this.revision = revision;
    revisionDelta.accept(this);
  }

  @Override
  public void visit(CDOAddFeatureDelta delta)
  {
    // Delta value must have been adjusted before!
    revision.setValue(delta.getFeature(), delta.getValue());
  }

  @Override
  public void visit(CDOSetFeatureDelta delta)
  {
    CDOFeature feature = delta.getFeature();
    Object value = delta.getValue();
    if (value != null && feature.isReference() && !(value instanceof CDOReferenceProxy))
    {
      revision.setValue(feature, idProvider.provideCDOID(value));
    }
  }

  @Override
  @SuppressWarnings("unchecked")
  public void visit(CDOListFeatureDelta deltas)
  {
    CDOFeature feature = deltas.getFeature();
    List<Object> list = (List<Object>)revision.getValue(feature);
    int[] indices = ((CDOListFeatureDeltaImpl)deltas).reconstructAddedIndices();
    for (int i = 1; i <= indices[0]; i++)
    {
      int index = indices[i];
      Object value = list.get(index);
      if (value != null && feature.isReference() && !(value instanceof CDOReferenceProxy))
      {
        value = idProvider.provideCDOID(value);
        list.set(index, value);
      }
    }
  }
}

Back to the top