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

com.metaeffekt.artifact.analysis.version.curation.functions.CuratedVersionFunctionSegmentsCount Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2021-2024 the original author or authors.
 *
 * 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.metaeffekt.artifact.analysis.version.curation.functions;

import com.metaeffekt.artifact.analysis.version.token.VersionToken;
import com.metaeffekt.artifact.analysis.version.token.VersionTokenType;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class CuratedVersionFunctionSegmentsCount extends CuratedVersionFunction {

    private final int segments;
    private final String delimiter;
    private final String expandValue;
    private final boolean reduce;
    private final boolean expand;

    protected CuratedVersionFunctionSegmentsCount(LinkedHashMap yaml) {
        super(yaml);

        this.segments = getIntPropertyOrThrow(yaml, "segments");
        this.delimiter = getStringProperty(yaml, "delimiter", ".");
        this.expandValue = getStringProperty(yaml, "expandValue", "0");
        this.reduce = getBooleanProperty(yaml, "reduce", false);
        this.expand = getBooleanProperty(yaml, "expand", true);
    }

    @Override
    protected VersionToken applyToPart(VersionToken token) {
        if (token == null) {
            return null;
        }

        if (token.isDateNumberOrSemver()) {
            final String initialValue = token.getValue();
            final List segments = Arrays.stream(initialValue.split(Pattern.quote(delimiter))).collect(Collectors.toList());

            if (segments.size() == this.segments) {
                return token;
            }

            if (segments.size() > this.segments) {
                if (reduce) {
                    final String reducedValue = String.join(delimiter, segments.subList(0, this.segments));
                    return new VersionToken(reducedValue, VersionTokenType.NUMBER_OR_SEMVER);
                } else {
                    return token;
                }
            } else {
                if (expand) {
                    while (segments.size() < this.segments) {
                        segments.add(expandValue);
                    }
                    return new VersionToken(String.join(delimiter, segments), VersionTokenType.NUMBER_OR_SEMVER);
                } else {
                    return token;
                }
            }
        }

        return token;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy