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 public class CLNumber extends CLElement {
19 
20     float mValue = Float.NaN;
21 
CLNumber(char[] content)22     public CLNumber(char[] content) {
23         super(content);
24     }
25 
CLNumber(float value)26     public CLNumber(float value) {
27         super(null);
28         this.mValue = value;
29     }
30 
31     // @TODO: add description
allocate(char[] content)32     public static CLElement allocate(char[] content) {
33         return new CLNumber(content);
34     }
35 
36     @Override
toJSON()37     protected String toJSON() {
38         float value = getFloat();
39         int intValue = (int) value;
40         if ((float) intValue == value) {
41             return "" + intValue;
42         }
43         return "" + value;
44     }
45 
46     @Override
toFormattedJSON(int indent, int forceIndent)47     protected String toFormattedJSON(int indent, int forceIndent) {
48         StringBuilder json = new StringBuilder();
49         addIndent(json, indent);
50         float value = getFloat();
51         int intValue = (int) value;
52         if ((float) intValue == value) {
53             json.append(intValue);
54         } else {
55             json.append(value);
56         }
57         return json.toString();
58     }
59 
60     // @TODO: add description
isInt()61     public boolean isInt() {
62         float value = getFloat();
63         int intValue = (int) value;
64         return ((float) intValue == value);
65     }
66 
67     @Override
getInt()68     public int getInt() {
69         if (Float.isNaN(mValue) && hasContent()) {
70             // If the value is undefined, attempt to define it from the content
71             mValue = Integer.parseInt(content());
72         }
73         return (int) mValue;
74     }
75 
76     @Override
getFloat()77     public float getFloat() {
78         if (Float.isNaN(mValue) && hasContent()) {
79             // If the value is undefined, attempt to define it from the content
80             mValue = Float.parseFloat(content());
81         }
82         return mValue;
83     }
84 
85     // @TODO: add description
putValue(float value)86     public void putValue(float value) {
87         this.mValue = value;
88     }
89 
90     @Override
equals(Object obj)91     public boolean equals(Object obj) {
92         if (this == obj) {
93             return true;
94         }
95 
96         if (obj instanceof CLNumber) {
97             float thisFloat = getFloat();
98             float otherFloat = ((CLNumber) obj).getFloat();
99             if (Float.isNaN(thisFloat) && Float.isNaN(otherFloat)) {
100                 // Consider equal if both elements have a NaN value
101                 return true;
102             }
103             return thisFloat == otherFloat;
104         }
105 
106         return false;
107     }
108 
109     @Override
hashCode()110     public int hashCode() {
111         // Auto-generated with Intellij Action "equals() and hashcode()"
112         int result = super.hashCode();
113         result = 31 * result + (mValue != 0.0f ? Float.floatToIntBits(mValue) : 0);
114         return result;
115     }
116 }
117