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 package com.android.monkeyrunner.recorder.actions; 17 18 /** 19 * Utility class to create Python Dictionary Strings. 20 * 21 * {'key': 'value'} 22 */ 23 public class PyDictUtilBuilder { 24 private StringBuilder sb = new StringBuilder(); 25 PyDictUtilBuilder()26 public PyDictUtilBuilder() { 27 sb.append("{"); 28 } 29 newBuilder()30 public static PyDictUtilBuilder newBuilder() { 31 return new PyDictUtilBuilder(); 32 } 33 addHelper(String key, String value)34 private void addHelper(String key, String value) { 35 sb.append("'").append(key).append("'"); 36 sb.append(":").append(value).append(","); 37 } 38 add(String key, int value)39 public PyDictUtilBuilder add(String key, int value) { 40 addHelper(key, Integer.toString(value)); 41 return this; 42 } 43 add(String key, float value)44 public PyDictUtilBuilder add(String key, float value) { 45 addHelper(key, Float.toString(value)); 46 return this; 47 } 48 add(String key, String value)49 public PyDictUtilBuilder add(String key, String value) { 50 addHelper(key, "'" + value + "'"); 51 return this; 52 } 53 build()54 public String build() { 55 sb.append("}"); 56 return sb.toString(); 57 } 58 addTuple(String key, int x, int y)59 public PyDictUtilBuilder addTuple(String key, int x, int y) { 60 String valuestr = new StringBuilder().append("(").append(x).append(",").append(y).append(")").toString(); 61 addHelper(key, valuestr); 62 return this; 63 } 64 } 65