1 /*
2  * Copyright (C) 2019 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 package com.android.car.audio;
17 
18 
19 import java.util.HashSet;
20 import java.util.Set;
21 
22 class CarAudioZonesValidator {
validate(CarAudioZone[] carAudioZones)23     static void validate(CarAudioZone[] carAudioZones) {
24         validateAtLeastOneZoneDefined(carAudioZones);
25         validateVolumeGroupsForEachZone(carAudioZones);
26         validateEachAddressAppearsAtMostOnce(carAudioZones);
27     }
28 
validateAtLeastOneZoneDefined(CarAudioZone[] carAudioZones)29     private static void validateAtLeastOneZoneDefined(CarAudioZone[] carAudioZones) {
30         if (carAudioZones.length == 0) {
31             throw new RuntimeException("At least one zone should be defined");
32         }
33     }
34 
validateVolumeGroupsForEachZone(CarAudioZone[] carAudioZones)35     private static void validateVolumeGroupsForEachZone(CarAudioZone[] carAudioZones) {
36         for (CarAudioZone zone : carAudioZones) {
37             if (!zone.validateVolumeGroups()) {
38                 throw new RuntimeException(
39                         "Invalid volume groups configuration for zone " + zone.getId());
40             }
41         }
42     }
43 
validateEachAddressAppearsAtMostOnce(CarAudioZone[] carAudioZones)44     private static void validateEachAddressAppearsAtMostOnce(CarAudioZone[] carAudioZones) {
45         Set<String> addresses = new HashSet<>();
46         for (CarAudioZone zone : carAudioZones) {
47             for (CarVolumeGroup carVolumeGroup : zone.getVolumeGroups()) {
48                 for (String address : carVolumeGroup.getAddresses()) {
49                     if (!addresses.add(address)) {
50                         throw new RuntimeException("Device with address "
51                                 + address + " appears in multiple volume groups or audio zones");
52                     }
53                 }
54             }
55         }
56     }
57 }
58