io.knowledgelinks.cicd.semantic.versioning.formatters.Functions Maven / Gradle / Ivy
package io.knowledgelinks.cicd.semantic.versioning.formatters;
/*-
* #%L
* Semantic Versioning
* %%
* Copyright (C) 2022 - 2023 Knowledgelinks
* %%
* 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.
* #L%
*/
public final class Functions {
private Functions() {
// static only methods
}
/**
* Converts the Branch name to a clean version for inserting in the version string.
*
* @param branchName the name of the branch to clean
* @param featureBranchIdentifier the portion of the branch start to remove
* @param detailsSeparator the character used to separate items in the details section
* @param maxBranchLength the longest allowable length for a branch name
* @return a cleaned branch name
*/
public static String cleanBranchName(String branchName, String featureBranchIdentifier,
String detailsSeparator, int maxBranchLength) {
if (branchName == null || branchName.length() == 0) {
return "";
}
String cleanedBranchName = branchName.trim();
if (cleanedBranchName.startsWith(featureBranchIdentifier)) {
cleanedBranchName = cleanedBranchName.substring(featureBranchIdentifier.length());
}
cleanedBranchName = Functions.getStringValue(cleanedBranchName, maxBranchLength);
if (cleanedBranchName.endsWith(detailsSeparator)) {
return cleanedBranchName.substring(0, cleanedBranchName.length() - 1);
}
return cleanedBranchName;
}
/**
* Applies the trim length the to passed in string value. Returns the shortened string.
*
* @param value the value to trim
* @param trimLength the max length of the value
* @return the trimmed string
*/
public static String getStringValue(String value, int trimLength) {
if (value == null || value.length() == 0) {
return "";
}
String newValue = value.trim();
if (trimLength > newValue.length()) {
return newValue;
}
return newValue.substring(0, trimLength);
}
/**
* Takes integer value and pads it with the supplied number of zeros.
*
* @param value the item to pad
* @param pad the number of places to pad
* @return padded string
*/
public static String getStringValue(int value, int pad) {
if (value < 0) {
return "";
}
// cannot have a pad length less than 1
int finalPad = pad < 1 ? 1 : pad;
String padFormat = String.format("%s0%s%s", "%", String.valueOf(finalPad), "d");
return String.format(padFormat, value);
}
}