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

utils.SortedCollectionConverters.kt Maven / Gradle / Ivy

Go to download

Part of the OSS Review Toolkit (ORT), a suite to automate software compliance checks.

The newest version!
/*
 * Copyright (C) 2023 The ORT Project Authors (see )
 *
 * 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
 *
 *     https://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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * License-Filename: LICENSE
 */

@file:Suppress("Filename", "MatchingDeclarationName")

package org.ossreviewtoolkit.model.utils

import com.fasterxml.jackson.databind.util.StdConverter

import java.util.SortedSet

import org.ossreviewtoolkit.model.ArtifactProvenance
import org.ossreviewtoolkit.model.CopyrightFinding
import org.ossreviewtoolkit.model.DependencyGraphEdge
import org.ossreviewtoolkit.model.DependencyReference
import org.ossreviewtoolkit.model.FileList
import org.ossreviewtoolkit.model.Identifier
import org.ossreviewtoolkit.model.LicenseFinding
import org.ossreviewtoolkit.model.Package
import org.ossreviewtoolkit.model.PackageReference
import org.ossreviewtoolkit.model.Project
import org.ossreviewtoolkit.model.Provenance
import org.ossreviewtoolkit.model.ProvenanceResolutionResult
import org.ossreviewtoolkit.model.RepositoryProvenance
import org.ossreviewtoolkit.model.ScanResult
import org.ossreviewtoolkit.model.Scope
import org.ossreviewtoolkit.model.SnippetFinding

class CopyrightFindingSortedSetConverter : StdConverter, SortedSet>() {
    override fun convert(value: Set) = value.toSortedSet(CopyrightFinding.COMPARATOR)
}

class DependencyGraphEdgeSortedSetConverter : StdConverter, Set>() {
    override fun convert(value: Set) = value.toSortedSet(compareBy({ it.from }, { it.to }))
}

class DependencyReferenceSortedSetConverter : StdConverter, SortedSet>() {
    override fun convert(value: Set) =
        value.toSortedSet(compareBy({ it.pkg.toString() }, { it.fragment }))
}

/** Do not convert to SortedSet in order to not require a comparator consistent with equals */
class FileListSortedSetConverter : StdConverter, Set>() {
    override fun convert(value: Set) = value.sortedBy { it.provenance.getSortKey() }.toSet()
}

/** Do not convert to SortedSet in order to not require a comparator consistent with equals */
class FileListEntrySortedSetConverter : StdConverter, Set>() {
    override fun convert(value: Set) = value.sortedBy { it.path }.toSet()
}

class LicenseFindingSortedSetConverter : StdConverter, SortedSet>() {
    override fun convert(value: Set) = value.toSortedSet(LicenseFinding.COMPARATOR)
}

class PackageReferenceSortedSetConverter : StdConverter, SortedSet>() {
    override fun convert(value: Set) = value.toSortedSet(compareBy { it.id })
}

class PackageSortedSetConverter : StdConverter, SortedSet>() {
    override fun convert(value: Set) = value.toSortedSet(compareBy { it.id })
}

class ProjectSortedSetConverter : StdConverter, SortedSet>() {
    override fun convert(value: Set) = value.toSortedSet(compareBy { it.id })
}

class ProvenanceResolutionResultSortedSetConverter :
    StdConverter, SortedSet>() {
    override fun convert(value: Set) = value.toSortedSet(compareBy { it.id })
}

class ScannersMapConverter : StdConverter>, Map>>() {
    override fun convert(value: Map>) = value.mapValues { it.value.toSortedSet() }.toSortedMap()
}

/** Do not convert to SortedSet in order to not require a comparator consistent with equals */
class ScanResultSortedSetConverter : StdConverter, Set>() {
    override fun convert(value: Set) = value.sortedBy { it.provenance.getSortKey() }.toSet()
}

class ScopeSortedSetConverter : StdConverter, SortedSet>() {
    override fun convert(value: Set) = value.toSortedSet(compareBy { it.name })
}

class SnippetFindingSortedSetConverter : StdConverter, SortedSet>() {
    override fun convert(value: Set) = value.toSortedSet(compareBy { it.sourceLocation })
}

private fun Provenance.getSortKey(): String =
    buildList {
        this += javaClass.canonicalName

        when (this@getSortKey) {
            is RepositoryProvenance -> {
                this += vcsInfo.type.toString()
                this += vcsInfo.url
            }

            is ArtifactProvenance -> {
                this += sourceArtifact.url
            }

            else -> {
                // Do not add anything, because unknown provenance does not have any properties.
            }
        }
    }.joinToString()

// TODO: Add more converters to get rid of Comparable implementations that just serve sorted output.




© 2015 - 2024 Weber Informatics LLC | Privacy Policy