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.motion.utils;
17 
18 import java.util.Arrays;
19 
20 public class TypedBundle {
21 
22     private static final int INITIAL_BOOLEAN = 4;
23     private static final int INITIAL_INT = 10;
24     private static final int INITIAL_FLOAT = 10;
25     private static final int INITIAL_STRING = 5;
26 
27     int[] mTypeInt = new int[INITIAL_INT];
28     int[] mValueInt = new int[INITIAL_INT];
29     int mCountInt = 0;
30     int[] mTypeFloat = new int[INITIAL_FLOAT];
31     float[] mValueFloat = new float[INITIAL_FLOAT];
32     int mCountFloat = 0;
33     int[] mTypeString = new int[INITIAL_STRING];
34     String[] mValueString = new String[INITIAL_STRING];
35     int mCountString = 0;
36     int[] mTypeBoolean = new int[INITIAL_BOOLEAN];
37     boolean[] mValueBoolean = new boolean[INITIAL_BOOLEAN];
38     int mCountBoolean = 0;
39 
40     // @TODO: add description
getInteger(int type)41     public int getInteger(int type) {
42         for (int i = 0; i < mCountInt; i++) {
43             if (mTypeInt[i] == type) {
44                 return mValueInt[i];
45             }
46         }
47         return -1;
48     }
49 
50     // @TODO: add description
add(int type, int value)51     public void add(int type, int value) {
52         if (mCountInt >= mTypeInt.length) {
53             mTypeInt = Arrays.copyOf(mTypeInt, mTypeInt.length * 2);
54             mValueInt = Arrays.copyOf(mValueInt, mValueInt.length * 2);
55         }
56         mTypeInt[mCountInt] = type;
57         mValueInt[mCountInt++] = value;
58     }
59 
60     // @TODO: add description
add(int type, float value)61     public void add(int type, float value) {
62         if (mCountFloat >= mTypeFloat.length) {
63             mTypeFloat = Arrays.copyOf(mTypeFloat, mTypeFloat.length * 2);
64             mValueFloat = Arrays.copyOf(mValueFloat, mValueFloat.length * 2);
65         }
66         mTypeFloat[mCountFloat] = type;
67         mValueFloat[mCountFloat++] = value;
68     }
69 
70     // @TODO: add description
addIfNotNull(int type, String value)71     public void addIfNotNull(int type, String value) {
72         if (value != null) {
73             add(type, value);
74         }
75     }
76 
77     // @TODO: add description
add(int type, String value)78     public void add(int type, String value) {
79         if (mCountString >= mTypeString.length) {
80             mTypeString = Arrays.copyOf(mTypeString, mTypeString.length * 2);
81             mValueString = Arrays.copyOf(mValueString, mValueString.length * 2);
82         }
83         mTypeString[mCountString] = type;
84         mValueString[mCountString++] = value;
85     }
86 
87     // @TODO: add description
add(int type, boolean value)88     public void add(int type, boolean value) {
89         if (mCountBoolean >= mTypeBoolean.length) {
90             mTypeBoolean = Arrays.copyOf(mTypeBoolean, mTypeBoolean.length * 2);
91             mValueBoolean = Arrays.copyOf(mValueBoolean, mValueBoolean.length * 2);
92         }
93         mTypeBoolean[mCountBoolean] = type;
94         mValueBoolean[mCountBoolean++] = value;
95     }
96 
97     // @TODO: add description
applyDelta(TypedValues values)98     public void applyDelta(TypedValues values) {
99         for (int i = 0; i < mCountInt; i++) {
100             values.setValue(mTypeInt[i], mValueInt[i]);
101         }
102         for (int i = 0; i < mCountFloat; i++) {
103             values.setValue(mTypeFloat[i], mValueFloat[i]);
104         }
105         for (int i = 0; i < mCountString; i++) {
106             values.setValue(mTypeString[i], mValueString[i]);
107         }
108         for (int i = 0; i < mCountBoolean; i++) {
109             values.setValue(mTypeBoolean[i], mValueBoolean[i]);
110         }
111     }
112 
113     // @TODO: add description
applyDelta(TypedBundle values)114     public void applyDelta(TypedBundle values) {
115         for (int i = 0; i < mCountInt; i++) {
116             values.add(mTypeInt[i], mValueInt[i]);
117         }
118         for (int i = 0; i < mCountFloat; i++) {
119             values.add(mTypeFloat[i], mValueFloat[i]);
120         }
121         for (int i = 0; i < mCountString; i++) {
122             values.add(mTypeString[i], mValueString[i]);
123         }
124         for (int i = 0; i < mCountBoolean; i++) {
125             values.add(mTypeBoolean[i], mValueBoolean[i]);
126         }
127     }
128 
129     // @TODO: add description
clear()130     public void clear() {
131         mCountBoolean = 0;
132         mCountString = 0;
133         mCountFloat = 0;
134         mCountInt = 0;
135     }
136 
137     @Override
toString()138     public String toString() {
139         return "TypedBundle{" +
140                 "mCountInt=" + mCountInt +
141                 ", mCountFloat=" + mCountFloat +
142                 ", mCountString=" + mCountString +
143                 ", mCountBoolean=" + mCountBoolean +
144                 '}';
145     }
146 }
147