Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 70a8ada582010457601caa979b58085217c07aea (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * Copyright (c) 2010-2013 Eike Stepper (Berlin, Germany) and others.
 * 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:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.spi.common.commit;

import org.eclipse.emf.cdo.common.branch.CDOBranchPoint;
import org.eclipse.emf.cdo.common.commit.CDOChangeSetData;
import org.eclipse.emf.cdo.common.commit.CDOCommitData;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.model.CDOPackageUnit;
import org.eclipse.emf.cdo.common.revision.CDOIDAndVersion;
import org.eclipse.emf.cdo.common.revision.CDORevisionKey;
import org.eclipse.emf.cdo.common.revision.delta.CDOFeatureDelta;
import org.eclipse.emf.cdo.common.revision.delta.CDOListFeatureDelta;
import org.eclipse.emf.cdo.common.revision.delta.CDORevisionDelta;
import org.eclipse.emf.cdo.internal.common.commit.CDOCommitDataImpl;
import org.eclipse.emf.cdo.internal.common.commit.CDOCommitInfoManagerImpl;
import org.eclipse.emf.cdo.spi.common.revision.InternalCDORevision;

import org.eclipse.emf.ecore.EStructuralFeature;

import java.io.PrintStream;
import java.text.MessageFormat;
import java.util.List;

/**
 * If the meaning of this type isn't clear, there really should be more of a description here...
 *
 * @author Eike Stepper
 * @since 3.0
 */
public final class CDOCommitInfoUtil
{
  private CDOCommitInfoUtil()
  {
  }

  /**
   * @since 4.2
   */
  public static InternalCDOCommitInfoManager createCommitInfoManager(boolean caching)
  {
    return new CDOCommitInfoManagerImpl(caching);
  }

  public static InternalCDOCommitInfoManager createCommitInfoManager()
  {
    return createCommitInfoManager(false);
  }

  /**
   * @since 4.2
   */
  public static CDOCommitData createCommitData(List<CDOPackageUnit> newPackageUnits, List<CDOIDAndVersion> newObjects, List<CDORevisionKey> changedObjects,
      List<CDOIDAndVersion> detachedObjects)
  {
    return new CDOCommitDataImpl(newPackageUnits, newObjects, changedObjects, detachedObjects);
  }

  /**
   * @since 4.2
   */
  public static long encodeCount(int count)
  {
    long timeStamp = count;
    if (count < 0)
    {
      timeStamp = -timeStamp + Integer.MAX_VALUE;
    }

    return -timeStamp;
  }

  /**
   * @since 4.2
   */
  public static int decodeCount(long timeStamp)
  {
    if (timeStamp < CDOBranchPoint.UNSPECIFIED_DATE)
    {
      timeStamp = -timeStamp;
      if (timeStamp > Integer.MAX_VALUE)
      {
        return (int)(Integer.MAX_VALUE - timeStamp);
      }

      return (int)timeStamp;
    }

    return Integer.MAX_VALUE;
  }

  /**
   * @since 4.0
   */
  public static void dump(PrintStream out, CDOChangeSetData changeSetData)
  {
    for (CDOIDAndVersion key : changeSetData.getNewObjects())
    {
      out.println("  + " + key);
      if (key instanceof InternalCDORevision)
      {
        InternalCDORevision revision = (InternalCDORevision)key;
        for (EStructuralFeature feature : revision.getClassInfo().getAllPersistentFeatures())
        {
          Object value = revision.getValue(feature);
          dumpFeature(out, feature, value);
        }
      }
    }

    for (CDORevisionKey key : changeSetData.getChangedObjects())
    {
      CDORevisionDelta delta = (CDORevisionDelta)key;

      String name = delta.getEClass().getName();
      CDOID id = delta.getID();
      int branch = delta.getBranch().getID();
      int version = delta.getVersion();

      out.println(MessageFormat.format("  * {0}@{1}:{2}v{3}", name, id, branch, version));
      dumpFeatureDeltas(out, delta.getFeatureDeltas());
    }

    for (CDOIDAndVersion key : changeSetData.getDetachedObjects())
    {
      out.println("  - " + key);
    }
  }

  private static void dumpFeatureDeltas(PrintStream out, List<CDOFeatureDelta> featureDeltas)
  {
    for (CDOFeatureDelta featureDelta : featureDeltas)
    {
      if (featureDelta instanceof CDOListFeatureDelta)
      {
        CDOListFeatureDelta list = (CDOListFeatureDelta)featureDelta;
        dumpFeatureDeltas(out, list.getListChanges());
      }
      else
      {
        dumpFeature(out, featureDelta.getFeature(), featureDelta);
      }
    }
  }

  private static void dumpFeature(PrintStream out, EStructuralFeature feature, Object value)
  {
    out.println("    " + feature.getName() + " = " + value);
  }
}

Back to the top