1 /*
2  * Copyright (C) 2010 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.tradefed.util.flag;
18 
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21 
22 public class DeviceFeatureFlag {
23 
24     // Expected flag format (same as output by "device_config list"):
25     // namespace/name=[value]
26     // Note value is optional.
27     private static final Pattern FLAG_PATTERN =
28             Pattern.compile("^(?<namespace>[^\\s/=]+)/(?<name>[^\\s/=]+)=(?<value>.*)$");
29 
30     private final String namespace;
31     private final String flagName;
32     private final String flagValue;
33 
34     /**
35      * Constructor to create a new DeviceFeatureFlag object.
36      *
37      * @param flagString A device config flag string in the format of "namespace/flagName=flagValue"
38      * @throws IllegalArgumentException if the flagString parameter cannot be parsed
39      */
DeviceFeatureFlag(String flagString)40     public DeviceFeatureFlag(String flagString) {
41         Matcher match = FLAG_PATTERN.matcher(flagString);
42         if (!match.matches()) {
43             throw new IllegalArgumentException(
44                     String.format("Failed to parse flag data: %s", flagString));
45         }
46         namespace = match.group("namespace");
47         flagName = match.group("name");
48         flagValue = match.group("value");
49     }
50 
51     /**
52      * Get the namespace of the DeviceFeatureFlag. E.g. "namespace" in flag string
53      * "namespace/flagName=flagValue".
54      *
55      * @return namespace string
56      */
getNamespace()57     public String getNamespace() {
58         return namespace;
59     }
60 
61     /**
62      * Get the flag name of the DeviceFeatureFlag. E.g. "flagName" in flag string
63      * "namespace/flagName=flagValue".
64      *
65      * @return flag name string
66      */
getFlagName()67     public String getFlagName() {
68         return flagName;
69     }
70 
71     /**
72      * Get the flag value of the DeviceFeatureFlag. E.g. "flagValue" in flag string
73      * "namespace/flagName=flagValue".
74      *
75      * @return flag value string
76      */
getFlagValue()77     public String getFlagValue() {
78         return flagValue;
79     }
80 
81     /**
82      * Convert the DeviceFeatureFlag object to a flag string in the format of
83      * "namespace/flagName=flagValue"
84      *
85      * @return formatted flag string
86      */
87     @Override
toString()88     public String toString() {
89         return String.format("%s/%s=%s", namespace, flagName, flagValue);
90     }
91 }
92