Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e6559bdba23541a6fb8e1ba4b17df809a3da843b (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
 * Copyright (c) 2010-2013, 2015 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.branch;

import org.eclipse.emf.cdo.common.branch.CDOBranch;
import org.eclipse.emf.cdo.common.branch.CDOBranchManager;
import org.eclipse.emf.cdo.common.branch.CDOBranchPoint;
import org.eclipse.emf.cdo.common.branch.CDOBranchPointRange;
import org.eclipse.emf.cdo.common.branch.CDOBranchVersion;
import org.eclipse.emf.cdo.common.protocol.CDODataInput;
import org.eclipse.emf.cdo.common.protocol.CDODataOutput;
import org.eclipse.emf.cdo.common.util.CDOCommonUtil;
import org.eclipse.emf.cdo.internal.common.branch.CDOBranchManagerImpl;
import org.eclipse.emf.cdo.internal.common.branch.CDOBranchPointRangeImpl;

import java.io.IOException;
import java.util.ArrayList;
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 CDOBranchUtil
{
  /**
   * @since 4.6
   */
  public static final CDOBranchPoint AUTO_BRANCH_POINT = new AutoBranchPoint();

  private CDOBranchUtil()
  {
  }

  public static InternalCDOBranchManager createBranchManager()
  {
    return new CDOBranchManagerImpl();
  }

  public static CDOBranchPointRange createRange(CDOBranchPoint startPoint, CDOBranchPoint endPoint)
  {
    return new CDOBranchPointRangeImpl(startPoint, endPoint);
  }

  /**
   * @since 4.6
   */
  public static void writeRange(CDODataOutput out, CDOBranchPointRange range) throws IOException
  {
    out.writeCDOBranchPoint(range.getStartPoint());
    out.writeCDOBranchPoint(range.getEndPoint());
  }

  /**
   * @since 4.6
   */
  public static CDOBranchPointRange readRange(CDODataInput in) throws IOException
  {
    CDOBranchPoint startPoint = in.readCDOBranchPoint();
    CDOBranchPoint endPoint = in.readCDOBranchPoint();
    return createRange(startPoint, endPoint);
  }

  /**
   * @since 4.6
   */
  public static void writeRangeOrNull(CDODataOutput out, CDOBranchPointRange range) throws IOException
  {
    if (range != null)
    {
      out.writeBoolean(true);
      writeRange(out, range);
    }
    else
    {
      out.writeBoolean(false);
    }
  }

  /**
   * @since 4.6
   */
  public static CDOBranchPointRange readRangeOrNull(CDODataInput in) throws IOException
  {
    if (in.readBoolean())
    {
      return readRange(in);
    }

    return null;
  }

  /**
   * @since 4.6
   */
  public static void writeBranchPointOrNull(CDODataOutput out, CDOBranchPoint point) throws IOException
  {
    if (point != null)
    {
      out.writeBoolean(true);
      out.writeCDOBranchPoint(point);
    }
    else
    {
      out.writeBoolean(false);
    }
  }

  /**
   * @since 4.6
   */
  public static CDOBranchPoint readBranchPointOrNull(CDODataInput in) throws IOException
  {
    if (in.readBoolean())
    {
      return in.readCDOBranchPoint();
    }

    return null;
  }

  public static CDOBranchPoint copyBranchPoint(CDOBranchPoint source)
  {
    return source.getBranch().getPoint(source.getTimeStamp());
  }

  public static CDOBranchVersion copyBranchVersion(CDOBranchVersion source)
  {
    return source.getBranch().getVersion(source.getVersion());
  }

  /**
   * @since 4.4
   */
  public static CDOBranchPoint adjustBranchPoint(CDOBranchPoint branchPoint, CDOBranchManager branchManager)
  {
    CDOBranch branch = branchPoint.getBranch();
    if (branch.getBranchManager() != branchManager)
    {
      branch = branchManager.getBranch(branch.getID());
      branchPoint = branch.getPoint(branchPoint.getTimeStamp());
    }

    return branchPoint;
  }

  /**
   * @since 4.4
   */
  public static CDOBranchPoint normalizeBranchPoint(CDOBranchPoint branchPoint)
  {
    long timeStamp = branchPoint.getTimeStamp();
    if (timeStamp == CDOBranchPoint.UNSPECIFIED_DATE)
    {
      return branchPoint;
    }

    CDOBranch branch = branchPoint.getBranch();
    return doNormalizeBranchPoint(branch, timeStamp);
  }

  /**
   * @since 4.2
   */
  public static CDOBranchPoint normalizeBranchPoint(CDOBranch branch, long timeStamp)
  {
    if (timeStamp == CDOBranchPoint.UNSPECIFIED_DATE)
    {
      return branch.getHead();
    }

    return doNormalizeBranchPoint(branch, timeStamp);
  }

  private static CDOBranchPoint doNormalizeBranchPoint(CDOBranch branch, long timeStamp)
  {
    for (;;)
    {
      long baseTime = branch.getBase().getTimeStamp();
      if (timeStamp < baseTime)
      {
        branch = branch.getBase().getBranch();
        if (branch == null)
        {
          throw new IllegalArgumentException("Time " + CDOCommonUtil.formatTimeStamp(timeStamp)
              + " is before the repository creation time " + CDOCommonUtil.formatTimeStamp(baseTime));
        }
      }
      else
      {
        return branch.getPoint(timeStamp);
      }
    }
  }

  public static boolean isContainedBy(CDOBranchPoint contained, CDOBranchPoint container)
  {
    CDOBranch containerBranch = container.getBranch();
    if (containerBranch == contained.getBranch())
    {
      return CDOCommonUtil.compareTimeStamps(contained.getTimeStamp(), container.getTimeStamp()) <= 0;
    }

    if (containerBranch == null)
    {
      return false;
    }

    return isContainedBy(contained, containerBranch.getBase());
  }

  public static CDOBranchPoint getAncestor(CDOBranchPoint point1, CDOBranchPoint point2)
  {
    if (point1.getBranch() == null)
    {
      // Must be the main branch base
      return point1;
    }

    if (point2.getBranch() == null)
    {
      // Must be the main branch base
      return point2;
    }

    CDOBranchPoint[] path1 = getPath(point1);
    CDOBranchPoint[] path2 = getPath(point2);
    for (CDOBranchPoint pathPoint1 : path1)
    {
      for (CDOBranchPoint pathPoint2 : path2)
      {
        if (pathPoint1.getBranch() == pathPoint2.getBranch())
        {
          if (CDOCommonUtil.compareTimeStamps(pathPoint1.getTimeStamp(), pathPoint2.getTimeStamp()) < 0)
          {
            return pathPoint1;
          }

          return pathPoint2;
        }
      }
    }

    // Can not happen because any two branches meet on the main branch
    return null;
  }

  public static CDOBranchPoint[] getPath(CDOBranchPoint point)
  {
    List<CDOBranchPoint> result = new ArrayList<CDOBranchPoint>();
    getPath(point, result);
    return result.toArray(new CDOBranchPoint[result.size()]);
  }

  private static void getPath(CDOBranchPoint point, List<CDOBranchPoint> result)
  {
    CDOBranch branch = point.getBranch();
    if (branch != null)
    {
      result.add(point);
      getPath(branch.getBase(), result);
    }
  }

  /**
   * @author Eike Stepper
   */
  private static final class AutoBranchPoint implements CDOBranchPoint
  {
    public long getTimeStamp()
    {
      return Long.MIN_VALUE;
    }

    public CDOBranch getBranch()
    {
      return null;
    }

    @Override
    public String toString()
    {
      return "BranchPoint[AUTO]";
    }
  }
}

Back to the top