1// Copyright 2017 Istio Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package labels
16
17// Collection is a collection of labels used for comparing labels against a
18// collection of labels
19type Collection []Instance
20
21// HasSubsetOf returns true if the input labels are a super set of one labels in a
22// collection or if the tag collection is empty
23func (c Collection) HasSubsetOf(that Instance) bool {
24	if len(c) == 0 {
25		return true
26	}
27	// prevent panic when that is nil
28	if len(that) == 0 {
29		return false
30	}
31	for _, this := range c {
32		if this.SubsetOf(that) {
33			return true
34		}
35	}
36	return false
37}
38
39// IsSupersetOf returns true if the input labels are a subset set of any set of labels in a
40// collection
41func (c Collection) IsSupersetOf(that Instance) bool {
42
43	if len(c) == 0 {
44		return len(that) == 0
45	}
46
47	for _, this := range c {
48		if that.SubsetOf(this) {
49			return true
50		}
51	}
52	return false
53}
54