blob: ec404d3993371fc506f506f8fc88a0446cf7c927 [file] [log] [blame]
// Copyright (C) 2015 The Android Open Source Project
//
// 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 build
import "time"
// FileSet is a list of Files with no duplicates.
type FileSet []File
// Files returns a FileSet formed from the list of files.
func Files(files ...File) FileSet {
return FileSet(files)
}
// Append returns a new FileSet with files appended to fs. Duplicates will not
// be included in the returned FileSet.
func (fs FileSet) Append(files ...File) FileSet {
m := fs.toMap()
out := append(FileSet{}, fs...)
for _, file := range files {
if _, found := m[file]; !found {
out = append(out, file)
}
}
return out
}
// Remove returns a new FileSet with files removed from this FileSet.
func (fs FileSet) Remove(files ...File) FileSet {
m := FileSet(files).toMap()
out := FileSet{}
for _, file := range fs {
if _, found := m[file]; !found {
out = append(out, file)
}
}
return out
}
// Filter returns the list of files in this FileSet that matches any pattern in
// patterns.
func (fs FileSet) Filter(patterns ...string) FileSet {
out := make(FileSet, 0, len(fs))
for _, file := range fs {
if file.Matches(patterns...) {
out = append(out, file)
}
}
return out
}
// Exclude returns the list of files in this FileSet that does not match any
// pattern in patterns.
func (fs FileSet) Exclude(patterns ...string) FileSet {
out := make(FileSet, 0, len(fs))
nextfile:
for _, file := range fs {
if file.Matches(patterns...) {
continue nextfile
}
out = append(out, file)
}
return out
}
// LastModified returns the most recent time any of the files were modified.
func (fs FileSet) LastModified() time.Time {
t := time.Time{}
for _, f := range fs {
m := f.LastModified()
if t.Before(m) {
t = m
}
}
return t
}
func (fs FileSet) toMap() map[File]struct{} {
m := make(map[File]struct{}, len(fs))
for _, file := range fs {
m[file] = struct{}{}
}
return m
}