All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.conqat.engine.service.shared.data.BranchesInfo Maven / Gradle / Ivy

There is a newer version: 2025.1.0
Show newest version
/*-------------------------------------------------------------------------+
|                                                                          |
| Copyright 2005-2011 the ConQAT Project                                   |
|                                                                          |
| 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 org.conqat.engine.service.shared.data;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.conqat.engine.index.shared.GitUtils;
import org.conqat.lib.commons.collections.CollectionUtils;
import org.conqat.lib.commons.js_export.ExportToTypeScript;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.teamscale.commons.lang.ToStringHelpers;

/**
 * Transport class for the branches of a project.
 *
 * This class is used for communication with IDE clients (via the @link
 * {@link org.conqat.engine.service.shared.client.IdeServiceClient}, special
 * care has to be taken when changing its signature!
 */
@ExportToTypeScript
public class BranchesInfo {

	/**
	 * Name of the default branch. This is called "main" branch for history reasons
	 * and we decided to keep as is, as we otherwise need migrations and new
	 * versions for the IDE plugins.
	 */
	@JsonProperty("mainBranchName")
	private final String mainBranchName;

	/**
	 * The names of all available active branches, including the default branch, but
	 * excluding deleted and anonymous branches.
	 */
	@JsonProperty("branchNames")
	private final List branchNames = new ArrayList<>();

	/** The names of deleted/inactive branches. */
	@JsonProperty("deletedBranches")
	private final List deletedBranches = new ArrayList<>();

	/** A list of anonymous branches or an empty list if non exist. */
	@JsonProperty("anonymousBranches")
	private final List anonymousBranches = new ArrayList<>();

	/**
	 * This maps aliases to branch names. The aliases are displayed in the UI,
	 * instead of the branch name.
	 */
	@JsonProperty("aliases")
	private final Map aliases = new HashMap<>();

	/** Empty constructor for serialization purposes */
	public BranchesInfo() {
		this("", Collections.emptyList(), Collections.emptyList());
	}

	public BranchesInfo(String defaultBranchName, List branchNames, List deletedBranches) {
		this.mainBranchName = defaultBranchName;

		copyNonAnonymousBranches(branchNames, this.branchNames);
		copyNonAnonymousBranches(deletedBranches, this.deletedBranches);
	}

	/** Constructor only for merging. */
	private BranchesInfo(String mainBranchName, List branchNames, List deletedBranches,
			List anonymousBranches, Map aliases) {
		this.mainBranchName = mainBranchName;
		this.branchNames.addAll(branchNames);
		this.deletedBranches.addAll(deletedBranches);
		this.anonymousBranches.addAll(anonymousBranches);
		this.aliases.putAll(aliases);
	}

	/**
	 * Copies all non-anonymous branches from source to target. Anonymous branches
	 * are stored in {@link #anonymousBranches}.
	 */
	private void copyNonAnonymousBranches(List sourceBranches, List targetBranches) {
		for (String branchName : sourceBranches) {
			if (GitUtils.isAnonymousBranchName(branchName)) {
				anonymousBranches.add(branchName);
			} else {
				targetBranches.add(branchName);
			}
		}
	}

	/** @see #aliases */
	public void addBranchAlias(String branch, String alias) {
		aliases.put(branch, alias);
	}

	/** @see #aliases */
	public Map getAliases() {
		return Collections.unmodifiableMap(aliases);
	}

	/** @see #mainBranchName */
	public String getDefaultBranchName() {
		return mainBranchName;
	}

	/** Returns the union of all branch names. */
	public List getAllBranchNames() {
		List allBranchNames = new ArrayList<>(branchNames);

		// prevent NPE for deserialized objects (https://jira.cqse.eu/browse/TS-18489)
		if (deletedBranches != null) {
			allBranchNames.addAll(deletedBranches);
		}
		if (anonymousBranches != null) {
			allBranchNames.addAll(anonymousBranches);
		}
		return allBranchNames;
	}

	/**
	 * Merge a second branches info into this one. The existing branches info is
	 * preferred in case of conflicts (e.g., clashing default branch name).
	 */
	public BranchesInfo merge(BranchesInfo other) {
		List mergedBranchNames = CollectionUtils.sort(CollectionUtils.unionSet(branchNames, other.branchNames));
		List mergedDeletedBranches = CollectionUtils
				.sort(CollectionUtils.intersectionSet(deletedBranches, other.deletedBranches));
		List mergedAnonymousBranches = CollectionUtils
				.sort(CollectionUtils.unionSet(anonymousBranches, other.anonymousBranches));
		Map mergedAliases = new HashMap<>();
		mergedAliases.putAll(other.aliases);
		mergedAliases.putAll(aliases);
		return new BranchesInfo(mainBranchName, mergedBranchNames, mergedDeletedBranches, mergedAnonymousBranches,
				mergedAliases);
	}

	@Override
	public String toString() {
		return ToStringHelpers.toReflectiveStringHelper(this).toString();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy