1 /* 2 * Copyright (C) 2021 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.compatibility.common.util; 18 19 import java.util.Collections; 20 import java.util.HashMap; 21 import java.util.Map; 22 23 /** Business-Logic GCL-accessible utility for key-value stores. */ 24 public class BusinessLogicMapStore { 25 26 private static Map<String, Map<String, String>> maps = new HashMap<>(); 27 hasMap(String mapName)28 public boolean hasMap(String mapName) { 29 return maps.containsKey(mapName); 30 } 31 putMap(String mapName, String separator, String... keyValuePairs)32 public void putMap(String mapName, String separator, String... keyValuePairs) { 33 Map<String, String> map = maps.get(mapName); 34 if (map == null) { 35 map = new HashMap<>(); 36 maps.put(mapName, map); 37 } 38 39 for (String keyValuePair : keyValuePairs) { 40 String[] tmp = keyValuePair.split(separator, 2); 41 if (tmp.length != 2) { 42 throw new IllegalArgumentException( 43 "Can't split key-value pair for \"" + keyValuePair + "\""); 44 } 45 String key = tmp[0]; 46 String value = tmp[1]; 47 map.put(key, value); 48 } 49 } 50 getMap(String mapName)51 public static Map<String, String> getMap(String mapName) { 52 Map<String, String> map = maps.get(mapName); 53 if (map == null) { 54 return null; 55 } 56 return Collections.unmodifiableMap(map); 57 } 58 } 59