// Copyright 2017 Istio 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 labels

// Collection is a collection of labels used for comparing labels against a
// collection of labels
type Collection []Instance

// HasSubsetOf returns true if the input labels are a super set of one labels in a
// collection or if the tag collection is empty
func (c Collection) HasSubsetOf(that Instance) bool {
	if len(c) == 0 {
		return true
	}
	// prevent panic when that is nil
	if len(that) == 0 {
		return false
	}
	for _, this := range c {
		if this.SubsetOf(that) {
			return true
		}
	}
	return false
}

// IsSupersetOf returns true if the input labels are a subset set of any set of labels in a
// collection
func (c Collection) IsSupersetOf(that Instance) bool {

	if len(c) == 0 {
		return len(that) == 0
	}

	for _, this := range c {
		if that.SubsetOf(this) {
			return true
		}
	}
	return false
}
