/*
 * Copyright 2011-2015 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.b2international.snowowl.datastore.server.internal.branch;

import java.util.Collection;

import com.b2international.commons.collections.Collections3;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSet.Builder;

/**
 * @since 4.1
 */
public class CDOBranchImpl extends BranchImpl implements InternalCDOBasedBranch {

	private final int cdoBranchId;
	private final int segmentId;
	private final Collection<Integer> segments;
	private final Collection<Integer> parentSegments;

	protected CDOBranchImpl(String name, String parentPath, long baseTimestamp, int cdoBranchId, int segmentId, Collection<Integer> segments, Collection<Integer> parentSegments) {
		this(name, parentPath, baseTimestamp, baseTimestamp, cdoBranchId, segmentId, segments, parentSegments);
	}

	protected CDOBranchImpl(String name, String parentPath, long baseTimestamp, long headTimestamp, int cdoBranchId, int segmentId, Collection<Integer> segments, Collection<Integer> parentSegments) {
		this(name, parentPath, baseTimestamp, headTimestamp, false, cdoBranchId, segmentId, segments, parentSegments);
	}

	protected CDOBranchImpl(String name, String parentPath, long baseTimestamp, long headTimestamp, boolean deleted, int cdoBranchId, int segmentId, Collection<Integer> segments, Collection<Integer> parentSegments) {
		super(name, parentPath, baseTimestamp, headTimestamp, deleted);
		this.cdoBranchId = cdoBranchId;
		this.segmentId = segmentId;
		this.segments = Collections3.toImmutableSet(segments);
		this.parentSegments = Collections3.toImmutableSet(parentSegments);
	}
	
	@Override
	protected CDOBranchImpl doCreateBranch(String name, String parentPath, long baseTimestamp, long headTimestamp, boolean deleted) {
		return new CDOBranchImpl(name, parentPath, baseTimestamp, headTimestamp, deleted, cdoBranchId, segmentId, segments, parentSegments);
	}
	
	@Override
	public int segmentId() {
		return segmentId;
	}
	
	@Override
	public Collection<Integer> segments() {
		return segments;
	}
	
	@Override
	public Collection<Integer> parentSegments() {
		return parentSegments;
	}

	@Override
	public int cdoBranchId() {
		return cdoBranchId;
	}
	
	@Override
	public InternalCDOBasedBranch withSegmentId(int segmentId) {
		final Builder<Integer> builder = ImmutableSet.builder();
		builder.add(segmentId);
		// use previous segments here, the branch got a new segment because a new child branch got opened
		builder.addAll(segments());
		return new CDOBranchImpl(name(), parentPath(), baseTimestamp(), headTimestamp(), isDeleted(), cdoBranchId(), segmentId, builder.build(), parentSegments);
	}

}

@@ -783,16 +783,55 @@
 tions3;%0A
+import com.google.common.base.Objects;%0A
 import c
@@ -3284,12 +3284,269 @@
 nts);%0A%09%7D
+%0A%09%0A%09@Override%0A%09public String toString() %7B%0A%09%09return Objects.toStringHelper(getClass())%0A%09%09%09%09.add(%22cdoBranchId%22, cdoBranchId())%0A%09%09%09%09.add(%22segmentId%22, segmentId())%0A%09%09%09%09.add(%22segments%22, segments())%0A%09%09%09%09.add(%22parentSegments%22, parentSegments())%0A%09%09%09%09.toString();%0A%09%7D
 %0A%0A%7D%0A

add toString to CDOBranchImpl