package com.b2international.snowowl.datastore.cdo;

import static com.google.common.base.Preconditions.checkState;

import java.util.Arrays;

import org.eclipse.emf.cdo.common.branch.CDOBranch;
import org.eclipse.emf.cdo.common.branch.CDOBranchPoint;

import com.b2international.snowowl.core.api.BranchPath;
import com.b2international.snowowl.datastore.branch.Branch;
import com.google.common.primitives.Ints;

public class CDOBranchPath implements BranchPath {

	private static int[] getBranchIds(final CDOBranch branch) {
		final CDOBranchPoint[] basePath = branch.getBasePath();
		final int[] branchIds = new int[basePath.length];
        branchIds[basePath.length - 1] = branch.getID();
        
        for (int i = 1; i < basePath.length; i++) {
        	branchIds[i - 1] = basePath[i].getBranch().getID();
        }
        
		return branchIds;
	}

	private final int[] branchIds;

	public CDOBranchPath() {
		this(new int[] { CDOBranch.MAIN_BRANCH_ID });
	}

	public CDOBranchPath(final CDOBranch branch) {
        this(getBranchIds(branch));
	}

	public CDOBranchPath(final int[] branchIds) {
        checkState(branchIds[0] == CDOBranch.MAIN_BRANCH_ID, "First branch ID segment does not match the MAIN branch identifier.");
		this.branchIds = branchIds;
	}

	@Override
	public BranchPath parent() {
		checkState(!isMain(), "Can't return parent for MAIN.");
		return new CDOBranchPath(Arrays.copyOf(branchIds, branchIds.length - 1));
	}
	
	@Override
	public boolean isMain() {
		return branchIds.length == 1;
	}
	
	@Override
	public String path() {
		return Ints.join(Branch.SEPARATOR, branchIds);
	}
}

@@ -1,8 +1,636 @@
+/*%0A * Copyright 2011-2015 B2i Healthcare Pte Ltd, http://b2i.sg%0A * %0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *      http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 
@@ -1032,16 +1032,126 @@
 .Ints;%0A%0A
+/**%0A * Wraps a sequence of CDO branch identifiers, forming a path to a physical branch in the repository.%0A */%0A
 public c
@@ -1365,24 +1365,18 @@
 ength%5D;%0A
-        
+%09%09
 branchId
@@ -1420,25 +1420,11 @@
 ();%0A
-        %0A        
+%0A%09%09
 for 
@@ -1463,24 +1463,18 @@
  i++) %7B%0A
-        
+%09%09
 %09branchI
@@ -1522,26 +1522,12 @@
 ();%0A
-        %7D%0A        
+%09%09%7D%0A
 %0A%09%09r
@@ -1706,24 +1706,18 @@
 anch) %7B%0A
-        
+%09%09
 this(get
@@ -1791,16 +1791,10 @@
 ) %7B%0A
-        
+%09%09
 chec
@@ -2121,26 +2121,25 @@
 h - 1));%0A%09%7D%0A
-%09
 %0A
+
 %09@Override%0A%09
@@ -2203,10 +2203,9 @@
 %0A%09%7D%0A
-%09
 %0A
+
 %09@Ov
@@ -2231,24 +2231,24 @@
 ng path() %7B%0A
-
 %09%09return Int
@@ -2286,11 +2286,71 @@
 Ids);%0A%09%7D
+%0A%0A%09@Override%0A%09public String toString() %7B%0A%09%09return path();%0A%09%7D
 %0A%7D%0A

Add javadoc to CDOBranchPath