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 org.jspecify.annotations.NonNull;
19 
20 import java.util.Iterator;
21 
22 public class CLObject extends CLContainer implements Iterable<CLKey> {
23 
CLObject(char[] content)24     public CLObject(char[] content) {
25         super(content);
26     }
27 
28     /**
29      * Allocate a CLObject around an array of chars
30      */
allocate(char[] content)31     public static CLObject allocate(char[] content) {
32         return new CLObject(content);
33     }
34 
35     /**
36      * Returns objet as a JSON5 String
37      */
38     @Override
toJSON()39     public String toJSON() {
40         StringBuilder json = new StringBuilder(getDebugName() + "{ ");
41         boolean first = true;
42         for (CLElement element : mElements) {
43             if (!first) {
44                 json.append(", ");
45             } else {
46                 first = false;
47             }
48             json.append(element.toJSON());
49         }
50         json.append(" }");
51         return json.toString();
52     }
53 
54     /**
55      * Returns a object as a formatted JSON5 String
56      */
toFormattedJSON()57     public String toFormattedJSON() {
58         return toFormattedJSON(0, 0);
59     }
60 
61     /**
62      * Returns as a formatted JSON5 String with an indentation
63      */
64     @Override
toFormattedJSON(int indent, int forceIndent)65     public String toFormattedJSON(int indent, int forceIndent) {
66         StringBuilder json = new StringBuilder(getDebugName());
67         json.append("{\n");
68         boolean first = true;
69         for (CLElement element : mElements) {
70             if (!first) {
71                 json.append(",\n");
72             } else {
73                 first = false;
74             }
75             json.append(element.toFormattedJSON(indent + sBaseIndent, forceIndent - 1));
76         }
77         json.append("\n");
78         addIndent(json, indent);
79         json.append("}");
80         return json.toString();
81     }
82 
83     @Override
iterator()84     public Iterator<CLKey> iterator() {
85         return new CLObjectIterator(this);
86     }
87 
88     private static class CLObjectIterator implements Iterator<CLKey> {
89         CLObject mObject;
90         int mIndex = 0;
91 
CLObjectIterator(CLObject clObject)92         CLObjectIterator(CLObject clObject) {
93             mObject = clObject;
94         }
95 
96         @Override
hasNext()97         public boolean hasNext() {
98             return mIndex < mObject.size();
99         }
100 
101         @Override
next()102         public CLKey next() {
103             CLKey key = (CLKey) mObject.mElements.get(mIndex);
104             mIndex++;
105             return key;
106         }
107     }
108 
109     @Override
clone()110     public @NonNull CLObject clone() {
111         // Overriding to get expected return type
112         return (CLObject) super.clone();
113     }
114 }
115