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

org.conqat.engine.index.shared.PreCommitUploadData Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) CQSE GmbH
 *
 * 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.index.shared;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Function;

import org.conqat.lib.commons.collections.CollectionUtils;
import org.conqat.lib.commons.collections.UnmodifiableMap;
import org.conqat.lib.commons.collections.UnmodifiableSet;
import org.conqat.lib.commons.uniformpath.UniformPath;
import org.conqat.lib.commons.uniformpath.UniformPathCompatibilityUtil;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

/** Data class for a pre-commit upload. */
public class PreCommitUploadData implements Serializable {

	/** Default serial version UID */
	private static final long serialVersionUID = 1L;

	/**
	 * A mapping from uniform paths to the content of the respective file. This should contain all the
	 * changes that need to be analyzed.
	 */
	@JsonProperty("uniformPathToContentMap")
	private final HashMap uniformPathToContentMap = new HashMap<>();

	/** Set of uniform paths that have been deleted. */
	@JsonProperty("deletedUniformPaths")
	private final HashSet deletedUniformPaths = new HashSet<>();

	@JsonCreator
	public PreCommitUploadData() {
		// Used for JSON deserialization
	}

	/**
	 * Creates a {@link PreCommitUploadData} from the given arguments.
	 */
	public static PreCommitUploadData from(Map uniformPathToContentMap,
			Collection deletedUniformPaths) {
		return new PreCommitUploadData(
				CollectionUtils.map(uniformPathToContentMap, UniformPathCompatibilityUtil::convert,
						Function.identity()),
				CollectionUtils.map(deletedUniformPaths, UniformPathCompatibilityUtil::convert));
	}

	/**
	 * Constructor.
	 */
	public PreCommitUploadData(Map uniformPathToContentMap,
			Collection deletedUniformPaths) {
		for (Entry entry : uniformPathToContentMap.entrySet()) {
			UniformPath uniformPath = entry.getKey();
			String content = entry.getValue();
			this.uniformPathToContentMap.put(uniformPath.toString(), content);
		}
		for (UniformPath uniformPath : deletedUniformPaths) {
			this.deletedUniformPaths.add(uniformPath.toString());
		}
	}

	/** @see #uniformPathToContentMap */
	public UnmodifiableMap getUniformPathToContentMap() {
		return CollectionUtils.asUnmodifiable(uniformPathToContentMap);
	}

	/** @see #deletedUniformPaths */
	public UnmodifiableSet getDeletedUniformPaths() {
		return CollectionUtils.asUnmodifiable(deletedUniformPaths);
	}

	/** Validates if the content has been properly set. */
	public boolean isValid() {
		return deletedUniformPaths != null && uniformPathToContentMap != null;
	}

	/** Returns true if no files are deleted and no files are new or changed. */
	public boolean isEmpty() {
		return deletedUniformPaths.isEmpty() && uniformPathToContentMap.isEmpty();
	}

	/** Returns true if the upload data belongs to a pure deletion upload. */
	public boolean isDeletionOnlyUpload() {
		return !deletedUniformPaths.isEmpty() && uniformPathToContentMap.isEmpty();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy