1 /* 2 * Copyright (C) 2020 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 17 package androidx.constraintlayout.core.motion.utils; 18 19 import java.util.Arrays; 20 import java.util.HashMap; 21 22 /** 23 * Used by KeyTimeCycles (and any future time dependent behaviour) to cache its current parameters 24 * to maintain consistency across requestLayout type rebuilds. 25 */ 26 public class KeyCache { 27 28 HashMap<Object, HashMap<String, float[]>> mMap = new HashMap<>(); 29 30 // @TODO: add description setFloatValue(Object view, String type, int element, float value)31 public void setFloatValue(Object view, String type, int element, float value) { 32 if (!mMap.containsKey(view)) { 33 HashMap<String, float[]> array = new HashMap<>(); 34 float[] vArray = new float[element + 1]; 35 vArray[element] = value; 36 array.put(type, vArray); 37 mMap.put(view, array); 38 } else { 39 HashMap<String, float[]> array = mMap.get(view); 40 if (array == null) { 41 array = new HashMap<>(); 42 } 43 44 if (!array.containsKey(type)) { 45 float[] vArray = new float[element + 1]; 46 vArray[element] = value; 47 array.put(type, vArray); 48 mMap.put(view, array); 49 } else { 50 float[] vArray = array.get(type); 51 if (vArray == null) { 52 vArray = new float[0]; 53 } 54 if (vArray.length <= element) { 55 vArray = Arrays.copyOf(vArray, element + 1); 56 } 57 vArray[element] = value; 58 array.put(type, vArray); 59 } 60 } 61 } 62 63 // @TODO: add description getFloatValue(Object view, String type, int element)64 public float getFloatValue(Object view, String type, int element) { 65 if (!mMap.containsKey(view)) { 66 return Float.NaN; 67 } else { 68 HashMap<String, float[]> array = mMap.get(view); 69 if (array == null || !array.containsKey(type)) { 70 return Float.NaN; 71 } 72 float[] vArray = array.get(type); 73 if (vArray == null) { 74 return Float.NaN; 75 } 76 if (vArray.length > element) { 77 return vArray[element]; 78 } 79 return Float.NaN; 80 } 81 } 82 } 83