131c05d13612205f6a48460d23135695b267e506cAdam Lesinski/* 231c05d13612205f6a48460d23135695b267e506cAdam Lesinski * Copyright (C) 2015 The Android Open Source Project 331c05d13612205f6a48460d23135695b267e506cAdam Lesinski * 431c05d13612205f6a48460d23135695b267e506cAdam Lesinski * Licensed under the Apache License, Version 2.0 (the "License"); 531c05d13612205f6a48460d23135695b267e506cAdam Lesinski * you may not use this file except in compliance with the License. 631c05d13612205f6a48460d23135695b267e506cAdam Lesinski * You may obtain a copy of the License at 731c05d13612205f6a48460d23135695b267e506cAdam Lesinski * 831c05d13612205f6a48460d23135695b267e506cAdam Lesinski * http://www.apache.org/licenses/LICENSE-2.0 931c05d13612205f6a48460d23135695b267e506cAdam Lesinski * 1031c05d13612205f6a48460d23135695b267e506cAdam Lesinski * Unless required by applicable law or agreed to in writing, software 1131c05d13612205f6a48460d23135695b267e506cAdam Lesinski * distributed under the License is distributed on an "AS IS" BASIS, 1231c05d13612205f6a48460d23135695b267e506cAdam Lesinski * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1331c05d13612205f6a48460d23135695b267e506cAdam Lesinski * See the License for the specific language governing permissions and 1431c05d13612205f6a48460d23135695b267e506cAdam Lesinski * limitations under the License. 1531c05d13612205f6a48460d23135695b267e506cAdam Lesinski */ 1631c05d13612205f6a48460d23135695b267e506cAdam Lesinskipackage android.util; 1731c05d13612205f6a48460d23135695b267e506cAdam Lesinski 1831c05d13612205f6a48460d23135695b267e506cAdam Lesinskiimport android.text.TextUtils; 1931c05d13612205f6a48460d23135695b267e506cAdam Lesinski 2031c05d13612205f6a48460d23135695b267e506cAdam Lesinski/** 2131c05d13612205f6a48460d23135695b267e506cAdam Lesinski * Parses a list of key=value pairs, separated by some delimiter, and puts the results in 2231c05d13612205f6a48460d23135695b267e506cAdam Lesinski * an internal Map. Values can be then queried by key, or if not found, a default value 2331c05d13612205f6a48460d23135695b267e506cAdam Lesinski * can be used. 2431c05d13612205f6a48460d23135695b267e506cAdam Lesinski * @hide 2531c05d13612205f6a48460d23135695b267e506cAdam Lesinski */ 2631c05d13612205f6a48460d23135695b267e506cAdam Lesinskipublic class KeyValueListParser { 2731c05d13612205f6a48460d23135695b267e506cAdam Lesinski private final ArrayMap<String, String> mValues = new ArrayMap<>(); 2831c05d13612205f6a48460d23135695b267e506cAdam Lesinski private final TextUtils.StringSplitter mSplitter; 2931c05d13612205f6a48460d23135695b267e506cAdam Lesinski 3031c05d13612205f6a48460d23135695b267e506cAdam Lesinski /** 3131c05d13612205f6a48460d23135695b267e506cAdam Lesinski * Constructs a new KeyValueListParser. This can be reused for different strings 3231c05d13612205f6a48460d23135695b267e506cAdam Lesinski * by calling {@link #setString(String)}. 3331c05d13612205f6a48460d23135695b267e506cAdam Lesinski * @param delim The delimiter that separates key=value pairs. 3431c05d13612205f6a48460d23135695b267e506cAdam Lesinski */ 3531c05d13612205f6a48460d23135695b267e506cAdam Lesinski public KeyValueListParser(char delim) { 3631c05d13612205f6a48460d23135695b267e506cAdam Lesinski mSplitter = new TextUtils.SimpleStringSplitter(delim); 3731c05d13612205f6a48460d23135695b267e506cAdam Lesinski } 3831c05d13612205f6a48460d23135695b267e506cAdam Lesinski 3931c05d13612205f6a48460d23135695b267e506cAdam Lesinski /** 4031c05d13612205f6a48460d23135695b267e506cAdam Lesinski * Resets the parser with a new string to parse. The string is expected to be in the following 4131c05d13612205f6a48460d23135695b267e506cAdam Lesinski * format: 4231c05d13612205f6a48460d23135695b267e506cAdam Lesinski * <pre>key1=value,key2=value,key3=value</pre> 4331c05d13612205f6a48460d23135695b267e506cAdam Lesinski * 4431c05d13612205f6a48460d23135695b267e506cAdam Lesinski * where the delimiter is a comma. 4531c05d13612205f6a48460d23135695b267e506cAdam Lesinski * 4631c05d13612205f6a48460d23135695b267e506cAdam Lesinski * @param str the string to parse. 4731c05d13612205f6a48460d23135695b267e506cAdam Lesinski * @throws IllegalArgumentException if the string is malformed. 4831c05d13612205f6a48460d23135695b267e506cAdam Lesinski */ 4931c05d13612205f6a48460d23135695b267e506cAdam Lesinski public void setString(String str) throws IllegalArgumentException { 5031c05d13612205f6a48460d23135695b267e506cAdam Lesinski mValues.clear(); 5131c05d13612205f6a48460d23135695b267e506cAdam Lesinski if (str != null) { 5231c05d13612205f6a48460d23135695b267e506cAdam Lesinski mSplitter.setString(str); 5331c05d13612205f6a48460d23135695b267e506cAdam Lesinski for (String pair : mSplitter) { 5431c05d13612205f6a48460d23135695b267e506cAdam Lesinski int sep = pair.indexOf('='); 5531c05d13612205f6a48460d23135695b267e506cAdam Lesinski if (sep < 0) { 5631c05d13612205f6a48460d23135695b267e506cAdam Lesinski mValues.clear(); 5731c05d13612205f6a48460d23135695b267e506cAdam Lesinski throw new IllegalArgumentException( 5831c05d13612205f6a48460d23135695b267e506cAdam Lesinski "'" + pair + "' in '" + str + "' is not a valid key-value pair"); 5931c05d13612205f6a48460d23135695b267e506cAdam Lesinski } 6031c05d13612205f6a48460d23135695b267e506cAdam Lesinski mValues.put(pair.substring(0, sep).trim(), pair.substring(sep + 1).trim()); 6131c05d13612205f6a48460d23135695b267e506cAdam Lesinski } 6231c05d13612205f6a48460d23135695b267e506cAdam Lesinski } 6331c05d13612205f6a48460d23135695b267e506cAdam Lesinski } 6431c05d13612205f6a48460d23135695b267e506cAdam Lesinski 6531c05d13612205f6a48460d23135695b267e506cAdam Lesinski /** 66e9a988caca733d2f292991a52a0047685a69812fDianne Hackborn * Get the value for key as an int. 67e9a988caca733d2f292991a52a0047685a69812fDianne Hackborn * @param key The key to lookup. 68e9a988caca733d2f292991a52a0047685a69812fDianne Hackborn * @param def The value to return if the key was not found, or the value was not a long. 69e9a988caca733d2f292991a52a0047685a69812fDianne Hackborn * @return the int value associated with the key. 70e9a988caca733d2f292991a52a0047685a69812fDianne Hackborn */ 71e9a988caca733d2f292991a52a0047685a69812fDianne Hackborn public int getInt(String key, int def) { 72e9a988caca733d2f292991a52a0047685a69812fDianne Hackborn String value = mValues.get(key); 73e9a988caca733d2f292991a52a0047685a69812fDianne Hackborn if (value != null) { 74e9a988caca733d2f292991a52a0047685a69812fDianne Hackborn try { 75e9a988caca733d2f292991a52a0047685a69812fDianne Hackborn return Integer.parseInt(value); 76e9a988caca733d2f292991a52a0047685a69812fDianne Hackborn } catch (NumberFormatException e) { 77e9a988caca733d2f292991a52a0047685a69812fDianne Hackborn // fallthrough 78e9a988caca733d2f292991a52a0047685a69812fDianne Hackborn } 79e9a988caca733d2f292991a52a0047685a69812fDianne Hackborn } 80e9a988caca733d2f292991a52a0047685a69812fDianne Hackborn return def; 81e9a988caca733d2f292991a52a0047685a69812fDianne Hackborn } 82e9a988caca733d2f292991a52a0047685a69812fDianne Hackborn 83e9a988caca733d2f292991a52a0047685a69812fDianne Hackborn /** 8431c05d13612205f6a48460d23135695b267e506cAdam Lesinski * Get the value for key as a long. 8531c05d13612205f6a48460d23135695b267e506cAdam Lesinski * @param key The key to lookup. 8631c05d13612205f6a48460d23135695b267e506cAdam Lesinski * @param def The value to return if the key was not found, or the value was not a long. 8731c05d13612205f6a48460d23135695b267e506cAdam Lesinski * @return the long value associated with the key. 8831c05d13612205f6a48460d23135695b267e506cAdam Lesinski */ 8931c05d13612205f6a48460d23135695b267e506cAdam Lesinski public long getLong(String key, long def) { 9031c05d13612205f6a48460d23135695b267e506cAdam Lesinski String value = mValues.get(key); 9131c05d13612205f6a48460d23135695b267e506cAdam Lesinski if (value != null) { 9231c05d13612205f6a48460d23135695b267e506cAdam Lesinski try { 9331c05d13612205f6a48460d23135695b267e506cAdam Lesinski return Long.parseLong(value); 9431c05d13612205f6a48460d23135695b267e506cAdam Lesinski } catch (NumberFormatException e) { 9531c05d13612205f6a48460d23135695b267e506cAdam Lesinski // fallthrough 9631c05d13612205f6a48460d23135695b267e506cAdam Lesinski } 9731c05d13612205f6a48460d23135695b267e506cAdam Lesinski } 9831c05d13612205f6a48460d23135695b267e506cAdam Lesinski return def; 9931c05d13612205f6a48460d23135695b267e506cAdam Lesinski } 10031c05d13612205f6a48460d23135695b267e506cAdam Lesinski 10131c05d13612205f6a48460d23135695b267e506cAdam Lesinski /** 10231c05d13612205f6a48460d23135695b267e506cAdam Lesinski * Get the value for key as a float. 10331c05d13612205f6a48460d23135695b267e506cAdam Lesinski * @param key The key to lookup. 10431c05d13612205f6a48460d23135695b267e506cAdam Lesinski * @param def The value to return if the key was not found, or the value was not a float. 10531c05d13612205f6a48460d23135695b267e506cAdam Lesinski * @return the float value associated with the key. 10631c05d13612205f6a48460d23135695b267e506cAdam Lesinski */ 10731c05d13612205f6a48460d23135695b267e506cAdam Lesinski public float getFloat(String key, float def) { 10831c05d13612205f6a48460d23135695b267e506cAdam Lesinski String value = mValues.get(key); 10931c05d13612205f6a48460d23135695b267e506cAdam Lesinski if (value != null) { 11031c05d13612205f6a48460d23135695b267e506cAdam Lesinski try { 11131c05d13612205f6a48460d23135695b267e506cAdam Lesinski return Float.parseFloat(value); 11231c05d13612205f6a48460d23135695b267e506cAdam Lesinski } catch (NumberFormatException e) { 11331c05d13612205f6a48460d23135695b267e506cAdam Lesinski // fallthrough 11431c05d13612205f6a48460d23135695b267e506cAdam Lesinski } 11531c05d13612205f6a48460d23135695b267e506cAdam Lesinski } 11631c05d13612205f6a48460d23135695b267e506cAdam Lesinski return def; 11731c05d13612205f6a48460d23135695b267e506cAdam Lesinski } 11831c05d13612205f6a48460d23135695b267e506cAdam Lesinski 11931c05d13612205f6a48460d23135695b267e506cAdam Lesinski /** 12031c05d13612205f6a48460d23135695b267e506cAdam Lesinski * Get the value for key as a string. 12131c05d13612205f6a48460d23135695b267e506cAdam Lesinski * @param key The key to lookup. 12231c05d13612205f6a48460d23135695b267e506cAdam Lesinski * @param def The value to return if the key was not found. 12331c05d13612205f6a48460d23135695b267e506cAdam Lesinski * @return the string value associated with the key. 12431c05d13612205f6a48460d23135695b267e506cAdam Lesinski */ 12531c05d13612205f6a48460d23135695b267e506cAdam Lesinski public String getString(String key, String def) { 12631c05d13612205f6a48460d23135695b267e506cAdam Lesinski String value = mValues.get(key); 12731c05d13612205f6a48460d23135695b267e506cAdam Lesinski if (value != null) { 12831c05d13612205f6a48460d23135695b267e506cAdam Lesinski return value; 12931c05d13612205f6a48460d23135695b267e506cAdam Lesinski } 13031c05d13612205f6a48460d23135695b267e506cAdam Lesinski return def; 13131c05d13612205f6a48460d23135695b267e506cAdam Lesinski } 1320ef403e53e2762d077750dd0a50b73c2125cadb0Dianne Hackborn 1330ef403e53e2762d077750dd0a50b73c2125cadb0Dianne Hackborn /** 1340ef403e53e2762d077750dd0a50b73c2125cadb0Dianne Hackborn * Get the value for key as a boolean. 1350ef403e53e2762d077750dd0a50b73c2125cadb0Dianne Hackborn * @param key The key to lookup. 1360ef403e53e2762d077750dd0a50b73c2125cadb0Dianne Hackborn * @param def The value to return if the key was not found. 1370ef403e53e2762d077750dd0a50b73c2125cadb0Dianne Hackborn * @return the string value associated with the key. 1380ef403e53e2762d077750dd0a50b73c2125cadb0Dianne Hackborn */ 1390ef403e53e2762d077750dd0a50b73c2125cadb0Dianne Hackborn public boolean getBoolean(String key, boolean def) { 1400ef403e53e2762d077750dd0a50b73c2125cadb0Dianne Hackborn String value = mValues.get(key); 1410ef403e53e2762d077750dd0a50b73c2125cadb0Dianne Hackborn if (value != null) { 1420ef403e53e2762d077750dd0a50b73c2125cadb0Dianne Hackborn try { 1430ef403e53e2762d077750dd0a50b73c2125cadb0Dianne Hackborn return Boolean.parseBoolean(value); 1440ef403e53e2762d077750dd0a50b73c2125cadb0Dianne Hackborn } catch (NumberFormatException e) { 1450ef403e53e2762d077750dd0a50b73c2125cadb0Dianne Hackborn // fallthrough 1460ef403e53e2762d077750dd0a50b73c2125cadb0Dianne Hackborn } 1470ef403e53e2762d077750dd0a50b73c2125cadb0Dianne Hackborn } 1480ef403e53e2762d077750dd0a50b73c2125cadb0Dianne Hackborn return def; 1490ef403e53e2762d077750dd0a50b73c2125cadb0Dianne Hackborn } 15031c05d13612205f6a48460d23135695b267e506cAdam Lesinski} 151