1 /*
<lambda>null2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.libraries.pcc.chronicle.analysis
18 
19 import com.android.libraries.pcc.chronicle.api.DataTypeDescriptor
20 import com.android.libraries.pcc.chronicle.api.ManagementStrategy
21 import com.android.libraries.pcc.chronicle.api.policy.Policy
22 
23 /**
24  * Default implementation of [PolicySet]. All [policies] provided at construction time are fixed and
25  * immutable.
26  */
27 data class DefaultPolicySet(private val policies: Set<Policy>) : PolicySet {
28   private val managementStrategies =
29     policies
30       .flatMap { it.targets }
31       .groupBy { it.schemaName }
32       .mapValues { (_, targets) ->
33         targets.flatMap { it.retentionsAsManagementStrategies() }.toSet()
34       }
35 
36   override fun findByName(policyName: String): Policy? = policies.find { it.name == policyName }
37 
38   override fun contains(policy: Policy): Boolean = policy in policies
39 
40   override fun findManagementStrategies(
41     dataTypeDescriptor: DataTypeDescriptor
42   ): Set<ManagementStrategy> = managementStrategies[dataTypeDescriptor.name] ?: emptySet()
43 
44   override fun toSet(): Set<Policy> {
45     return policies.toSet()
46   }
47 }
48