1 /* 2 * Copyright (C) 2022 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 androidx.constraintlayout.core.dsl; 18 19 import java.util.HashMap; 20 import java.util.Map; 21 22 public class Helper { 23 public enum Type { 24 VERTICAL_GUIDELINE, 25 HORIZONTAL_GUIDELINE, 26 VERTICAL_CHAIN, 27 HORIZONTAL_CHAIN, 28 BARRIER 29 } 30 31 protected final String name; 32 protected HelperType type = null; 33 protected String config; 34 protected Map<String, String> configMap = new HashMap<>(); 35 final static protected Map<Constraint.Side, String> sideMap = new HashMap<>(); 36 static { sideMap.put(Constraint.Side.LEFT, "'left'")37 sideMap.put(Constraint.Side.LEFT, "'left'"); sideMap.put(Constraint.Side.RIGHT, "'right'")38 sideMap.put(Constraint.Side.RIGHT, "'right'"); sideMap.put(Constraint.Side.TOP, "'top'")39 sideMap.put(Constraint.Side.TOP, "'top'"); sideMap.put(Constraint.Side.BOTTOM, "'bottom'")40 sideMap.put(Constraint.Side.BOTTOM, "'bottom'"); sideMap.put(Constraint.Side.START, "'start'")41 sideMap.put(Constraint.Side.START, "'start'"); sideMap.put(Constraint.Side.END, "'end'")42 sideMap.put(Constraint.Side.END, "'end'"); sideMap.put(Constraint.Side.BASELINE, "'baseline'")43 sideMap.put(Constraint.Side.BASELINE, "'baseline'"); 44 } 45 46 final static protected Map<Helper.Type, String> typeMap = new HashMap<>(); 47 static { typeMap.put(Type.VERTICAL_GUIDELINE, "vGuideline")48 typeMap.put(Type.VERTICAL_GUIDELINE, "vGuideline"); typeMap.put(Type.HORIZONTAL_GUIDELINE, "hGuideline")49 typeMap.put(Type.HORIZONTAL_GUIDELINE, "hGuideline"); typeMap.put(Type.VERTICAL_CHAIN, "vChain")50 typeMap.put(Type.VERTICAL_CHAIN, "vChain"); typeMap.put(Type.HORIZONTAL_CHAIN, "hChain")51 typeMap.put(Type.HORIZONTAL_CHAIN, "hChain"); typeMap.put(Type.BARRIER, "barrier")52 typeMap.put(Type.BARRIER, "barrier"); 53 } 54 Helper(String name, HelperType type)55 public Helper(String name, HelperType type) { 56 this.name = name; 57 this.type = type; 58 } 59 Helper(String name, HelperType type, String config)60 public Helper(String name, HelperType type, String config) { 61 this.name = name; 62 this.type = type; 63 this.config = config; 64 configMap = convertConfigToMap(); 65 } 66 getId()67 public String getId() { 68 return name; 69 } 70 getType()71 public HelperType getType() { 72 return type; 73 } 74 getConfig()75 public String getConfig() { 76 return config; 77 } 78 convertConfigToMap()79 public Map<String, String> convertConfigToMap() { 80 if (config == null || config.length() == 0) { 81 return null; 82 } 83 84 Map<String, String> map = new HashMap<>(); 85 StringBuilder builder = new StringBuilder(); 86 String key = ""; 87 String value = ""; 88 int squareBrackets = 0; 89 int curlyBrackets = 0; 90 char ch; 91 for (int i = 0; i < config.length(); i++) { 92 ch = config.charAt(i); 93 if (ch == ':') { 94 key = builder.toString(); 95 builder.setLength(0); 96 } else if (ch == ',' && squareBrackets == 0 && curlyBrackets == 0) { 97 value = builder.toString(); 98 map.put(key, value); 99 key = value = ""; 100 builder.setLength(0); 101 } else if (ch != ' ') { 102 switch (ch) { 103 case '[': 104 squareBrackets++; 105 break; 106 case '{': 107 curlyBrackets++; 108 break; 109 case ']': 110 squareBrackets--; 111 break; 112 case '}': 113 curlyBrackets--; 114 break; 115 } 116 117 builder.append(ch); 118 } 119 } 120 121 map.put(key, builder.toString()); 122 return map; 123 } 124 append(Map<String, String> map, StringBuilder ret)125 public void append(Map<String, String> map, StringBuilder ret) { 126 if (map.isEmpty()) { 127 return; 128 } 129 for (String key : map.keySet()) { 130 ret.append(key).append(":").append(map.get(key)).append(",\n"); 131 } 132 } 133 134 @Override toString()135 public String toString() { 136 StringBuilder ret = new StringBuilder(name + ":{\n"); 137 138 if (type != null) { 139 ret.append("type:'").append(type.toString()).append("',\n"); 140 } 141 142 if (configMap != null) { 143 append(configMap, ret); 144 } 145 146 ret.append("},\n"); 147 return ret.toString(); 148 } 149 main(String[] args)150 public static void main(String[] args) { 151 Barrier b = new Barrier("abc", "['a1', 'b2']"); 152 System.out.println(b.toString()); 153 } 154 155 public static final class HelperType { 156 final String mName; 157 HelperType(String str)158 public HelperType(String str) { 159 mName = str; 160 } 161 162 @Override toString()163 public String toString() { return mName; } 164 } 165 } 166