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 /**
21  * {@link CLElement} implementation for json Strings when used as property values or array elements.
22  */
23 public class CLString extends CLElement {
24 
CLString(char[] content)25     public CLString(char[] content) {
26         super(content);
27     }
28 
29     // @TODO: add description
allocate(char[] content)30     public static CLElement allocate(char[] content) {
31         return new CLString(content);
32     }
33 
34     /**
35      * Creates a {@link CLString} element from a String object.
36      */
from(@onNull String content)37     public static @NonNull CLString from(@NonNull String content) {
38         CLString stringElement = new CLString(content.toCharArray());
39         stringElement.setStart(0L);
40         stringElement.setEnd(content.length() - 1);
41         return stringElement;
42     }
43 
44     @Override
toJSON()45     protected String toJSON() {
46         return "'" + content() + "'";
47     }
48 
49     @Override
toFormattedJSON(int indent, int forceIndent)50     protected String toFormattedJSON(int indent, int forceIndent) {
51         StringBuilder json = new StringBuilder();
52         addIndent(json, indent);
53         json.append("'");
54         json.append(content());
55         json.append("'");
56         return json.toString();
57     }
58 
59     @Override
equals(Object obj)60     public boolean equals(Object obj) {
61         if (this == obj) {
62             return true;
63         }
64         if (obj instanceof CLString && this.content().equals(((CLString) obj).content())) {
65             return true;
66         }
67         return super.equals(obj);
68     }
69 
70     @Override
hashCode()71     public int hashCode() {
72         return super.hashCode();
73     }
74 }
75 
76 
77