2021-10-26 01:21:00 +02:00
|
|
|
// Copyright 2021 Google LLC
|
|
|
|
//
|
|
|
|
// 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 compliance
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-01-10 22:50:57 +01:00
|
|
|
"strings"
|
2021-10-26 01:21:00 +02:00
|
|
|
)
|
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// LicenseConditionSet identifies sets of license conditions.
|
|
|
|
type LicenseConditionSet LicenseCondition
|
2021-10-26 01:21:00 +02:00
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// AllLicenseConditions is the set of all recognized license conditions.
|
|
|
|
const AllLicenseConditions = LicenseConditionSet(LicenseConditionMask)
|
2021-10-26 01:21:00 +02:00
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// NewLicenseConditionSet returns a set containing exactly the elements of
|
|
|
|
// `conditions`.
|
|
|
|
func NewLicenseConditionSet(conditions ...LicenseCondition) LicenseConditionSet {
|
|
|
|
cs := LicenseConditionSet(0x00)
|
2021-10-26 01:21:00 +02:00
|
|
|
for _, lc := range conditions {
|
2022-01-10 22:50:57 +01:00
|
|
|
cs |= LicenseConditionSet(lc)
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
2022-01-10 22:50:57 +01:00
|
|
|
return cs
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// Plus returns a new set containing all of the elements of `cs` and all of the
|
|
|
|
// `conditions`.
|
|
|
|
func (cs LicenseConditionSet) Plus(conditions ...LicenseCondition) LicenseConditionSet {
|
|
|
|
result := cs
|
|
|
|
for _, lc := range conditions {
|
|
|
|
result |= LicenseConditionSet(lc)
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
2022-01-10 22:50:57 +01:00
|
|
|
return result
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// Union returns a new set containing all of the elements of `cs` and all of the
|
|
|
|
// elements of the `other` sets.
|
|
|
|
func (cs LicenseConditionSet) Union(other ...LicenseConditionSet) LicenseConditionSet {
|
|
|
|
result := cs
|
|
|
|
for _, ls := range other {
|
|
|
|
result |= ls
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
2022-01-10 22:50:57 +01:00
|
|
|
return result
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// MatchingAny returns the subset of `cs` equal to any of the `conditions`.
|
|
|
|
func (cs LicenseConditionSet) MatchingAny(conditions ...LicenseCondition) LicenseConditionSet {
|
|
|
|
result := LicenseConditionSet(0x00)
|
|
|
|
for _, lc := range conditions {
|
|
|
|
result |= cs & LicenseConditionSet(lc)
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
2022-01-10 22:50:57 +01:00
|
|
|
return result
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// MatchingAnySet returns the subset of `cs` that are members of any of the
|
|
|
|
// `other` sets.
|
|
|
|
func (cs LicenseConditionSet) MatchingAnySet(other ...LicenseConditionSet) LicenseConditionSet {
|
|
|
|
result := LicenseConditionSet(0x00)
|
|
|
|
for _, ls := range other {
|
|
|
|
result |= cs & ls
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
2022-01-10 22:50:57 +01:00
|
|
|
return result
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// HasAny returns true when `cs` contains at least one of the `conditions`.
|
|
|
|
func (cs LicenseConditionSet) HasAny(conditions ...LicenseCondition) bool {
|
|
|
|
for _, lc := range conditions {
|
|
|
|
if 0x0000 != (cs & LicenseConditionSet(lc)) {
|
2021-10-26 01:21:00 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// MatchesAnySet returns true when `cs` has a non-empty intersection with at
|
|
|
|
// least one of the `other` condition sets.
|
|
|
|
func (cs LicenseConditionSet) MatchesAnySet(other ...LicenseConditionSet) bool {
|
|
|
|
for _, ls := range other {
|
|
|
|
if 0x0000 != (cs & ls) {
|
|
|
|
return true
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
}
|
2022-01-10 22:50:57 +01:00
|
|
|
return false
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// HasAll returns true when `cs` contains every one of the `conditions`.
|
|
|
|
func (cs LicenseConditionSet) HasAll(conditions ...LicenseCondition) bool {
|
|
|
|
for _, lc := range conditions {
|
|
|
|
if 0x0000 == (cs & LicenseConditionSet(lc)) {
|
|
|
|
return false
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
}
|
2022-01-10 22:50:57 +01:00
|
|
|
return true
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// MatchesEverySet returns true when `cs` has a non-empty intersection with
|
|
|
|
// each of the `other` condition sets.
|
|
|
|
func (cs LicenseConditionSet) MatchesEverySet(other ...LicenseConditionSet) bool {
|
|
|
|
for _, ls := range other {
|
|
|
|
if 0x0000 == (cs & ls) {
|
2021-10-26 01:21:00 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// Intersection returns the subset of `cs` that are members of every `other`
|
|
|
|
// set.
|
|
|
|
func (cs LicenseConditionSet) Intersection(other ...LicenseConditionSet) LicenseConditionSet {
|
|
|
|
result := cs
|
|
|
|
for _, ls := range other {
|
|
|
|
result &= ls
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
2022-01-10 22:50:57 +01:00
|
|
|
return result
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// Minus returns the subset of `cs` that are not equaal to any `conditions`.
|
|
|
|
func (cs LicenseConditionSet) Minus(conditions ...LicenseCondition) LicenseConditionSet {
|
|
|
|
result := cs
|
2021-10-26 01:21:00 +02:00
|
|
|
for _, lc := range conditions {
|
2022-01-10 22:50:57 +01:00
|
|
|
result &^= LicenseConditionSet(lc)
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
2022-01-10 22:50:57 +01:00
|
|
|
return result
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// Difference returns the subset of `cs` that are not members of any `other`
|
|
|
|
// set.
|
|
|
|
func (cs LicenseConditionSet) Difference(other ...LicenseConditionSet) LicenseConditionSet {
|
|
|
|
result := cs
|
|
|
|
for _, ls := range other {
|
|
|
|
result &^= ls
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
2022-01-10 22:50:57 +01:00
|
|
|
return result
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// Len returns the number of license conditions in the set.
|
|
|
|
func (cs LicenseConditionSet) Len() int {
|
|
|
|
size := 0
|
|
|
|
for lc := LicenseConditionSet(0x01); 0x00 != (AllLicenseConditions & lc); lc <<= 1 {
|
|
|
|
if 0x00 != (cs & lc) {
|
|
|
|
size++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return size
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// AsList returns an array of the license conditions in the set.
|
|
|
|
func (cs LicenseConditionSet) AsList() []LicenseCondition {
|
|
|
|
result := make([]LicenseCondition, 0, cs.Len())
|
|
|
|
for lc := LicenseConditionSet(0x01); 0x00 != (AllLicenseConditions & lc); lc <<= 1 {
|
|
|
|
if 0x00 != (cs & lc) {
|
|
|
|
result = append(result, LicenseCondition(lc))
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
}
|
2022-01-10 22:50:57 +01:00
|
|
|
return result
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// Names returns an array of the names of the license conditions in the set.
|
|
|
|
func (cs LicenseConditionSet) Names() []string {
|
|
|
|
result := make([]string, 0, cs.Len())
|
|
|
|
for lc := LicenseConditionSet(0x01); 0x00 != (AllLicenseConditions & lc); lc <<= 1 {
|
|
|
|
if 0x00 != (cs & lc) {
|
|
|
|
result = append(result, LicenseCondition(lc).Name())
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// IsEmpty returns true when the set contains no license conditions.
|
|
|
|
func (cs LicenseConditionSet) IsEmpty() bool {
|
|
|
|
return 0x00 == (cs & AllLicenseConditions)
|
|
|
|
}
|
2021-10-26 01:21:00 +02:00
|
|
|
|
2022-01-10 22:50:57 +01:00
|
|
|
// String returns a human-readable string representation of the set.
|
|
|
|
func (cs LicenseConditionSet) String() string {
|
|
|
|
return fmt.Sprintf("{%s}", strings.Join(cs.Names(), "|"))
|
2021-10-26 01:21:00 +02:00
|
|
|
}
|