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 package androidx.constraintlayout.core.parser; 17 18 import java.util.ArrayList; 19 import java.util.Objects; 20 21 public class CLKey extends CLContainer { 22 23 private static ArrayList<String> sSections = new ArrayList<>(); 24 25 static { 26 sSections.add("ConstraintSets"); 27 sSections.add("Variables"); 28 sSections.add("Generate"); 29 sSections.add("Transitions"); 30 sSections.add("KeyFrames"); 31 sSections.add("KeyAttributes"); 32 sSections.add("KeyPositions"); 33 sSections.add("KeyCycles"); 34 } 35 CLKey(char[] content)36 public CLKey(char[] content) { 37 super(content); 38 } 39 40 // @TODO: add description allocate(char[] content)41 public static CLElement allocate(char[] content) { 42 return new CLKey(content); 43 } 44 45 // @TODO: add description allocate(String name, CLElement value)46 public static CLElement allocate(String name, CLElement value) { 47 CLKey key = new CLKey(name.toCharArray()); 48 key.setStart(0); 49 key.setEnd(name.length() - 1); 50 key.set(value); 51 return key; 52 } 53 getName()54 public String getName() { 55 return content(); 56 } 57 58 @Override toJSON()59 protected String toJSON() { 60 if (mElements.size() > 0) { 61 return getDebugName() + content() + ": " + mElements.get(0).toJSON(); 62 } 63 return getDebugName() + content() + ": <> "; 64 } 65 66 @Override toFormattedJSON(int indent, int forceIndent)67 protected String toFormattedJSON(int indent, int forceIndent) { 68 StringBuilder json = new StringBuilder(getDebugName()); 69 addIndent(json, indent); 70 String content = content(); 71 if (mElements.size() > 0) { 72 json.append(content); 73 json.append(": "); 74 if (sSections.contains(content)) { 75 forceIndent = 3; 76 } 77 if (forceIndent > 0) { 78 json.append(mElements.get(0).toFormattedJSON(indent, forceIndent - 1)); 79 } else { 80 String val = mElements.get(0).toJSON(); 81 if (val.length() + indent < sMaxLine) { 82 json.append(val); 83 } else { 84 json.append(mElements.get(0).toFormattedJSON(indent, forceIndent - 1)); 85 } 86 } 87 return json.toString(); 88 } 89 return content + ": <> "; 90 } 91 92 // @TODO: add description set(CLElement value)93 public void set(CLElement value) { 94 if (mElements.size() > 0) { 95 mElements.set(0, value); 96 } else { 97 mElements.add(value); 98 } 99 } 100 101 // @TODO: add description getValue()102 public CLElement getValue() { 103 if (mElements.size() > 0) { 104 return mElements.get(0); 105 } 106 return null; 107 } 108 109 @Override equals(Object obj)110 public boolean equals(Object obj) { 111 if (this == obj) { 112 return true; 113 } 114 if (obj instanceof CLKey) { 115 CLKey objKey = (CLKey) obj; 116 if (!Objects.equals(this.getName(), objKey.getName())) { 117 return false; 118 } 119 } 120 // Delegate the rest to parent 121 return super.equals(obj); 122 } 123 124 @Override hashCode()125 public int hashCode() { 126 return super.hashCode(); 127 } 128 } 129