1 /* 2 * Copyright (C) 2025 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 com.android.internal.widget.remotecompose.core.serialize; 17 18 import android.annotation.Nullable; 19 20 import java.util.LinkedHashMap; 21 import java.util.List; 22 import java.util.Map; 23 24 /** Represents a serializer for a map */ 25 public interface MapSerializer { 26 27 /** 28 * Adds a "type" field with this value 29 * 30 * @param type The name of the type 31 */ addType(String type)32 MapSerializer addType(String type); 33 34 /** 35 * Add a float expression 36 * 37 * @param key The key 38 * @param value The float src 39 */ addFloatExpressionSrc(String key, float[] value)40 MapSerializer addFloatExpressionSrc(String key, float[] value); 41 42 /** 43 * Add an int expression 44 * 45 * @param key The key 46 * @param value The int src 47 * @param mask For determining ID from int 48 */ addIntExpressionSrc(String key, int[] value, int mask)49 MapSerializer addIntExpressionSrc(String key, int[] value, int mask); 50 51 /** 52 * Add a path 53 * 54 * @param key The key 55 * @param path The path 56 */ addPath(String key, float[] path)57 MapSerializer addPath(String key, float[] path); 58 59 /** 60 * Add metadata to this map for filtering by the data format generator. 61 * 62 * @param value A set of tags to add 63 */ addTags(SerializeTags... value)64 MapSerializer addTags(SerializeTags... value); 65 66 /** 67 * Add a list entry to this map. The List values can be any primitive, List, Map, or 68 * Serializable 69 * 70 * @param key The key 71 * @param value The list 72 */ add(String key, @Nullable List<T> value)73 <T> MapSerializer add(String key, @Nullable List<T> value); 74 75 /** 76 * Add a map entry to this map. The map values can be any primitive, List, Map, or Serializable 77 * 78 * @param key The key 79 * @param value The list 80 */ add(String key, @Nullable Map<String, T> value)81 <T> MapSerializer add(String key, @Nullable Map<String, T> value); 82 83 /** 84 * Adds any Serializable type to this map 85 * 86 * @param key The key 87 * @param value The Serializable 88 */ add(String key, @Nullable Serializable value)89 MapSerializer add(String key, @Nullable Serializable value); 90 91 /** 92 * Adds a String entry 93 * 94 * @param key The key 95 * @param value The String 96 */ add(String key, @Nullable String value)97 MapSerializer add(String key, @Nullable String value); 98 99 /** 100 * Adds a color entry 101 * 102 * @param key The key 103 * @param a Alpha value [0, 1] 104 * @param r Red value [0, 1] 105 * @param g Green value [0, 1] 106 * @param b Blue value [0, 1] 107 */ add(String key, float a, float r, float g, float b)108 MapSerializer add(String key, float a, float r, float g, float b); 109 110 /** 111 * Adds an ID and Value pair. This can be either a value or variable. 112 * 113 * @param key The key 114 * @param id Maybe float NaN ID 115 * @param value Maybe value 116 */ add(String key, float id, float value)117 MapSerializer add(String key, float id, float value); 118 119 /** 120 * Adds a Byte entry 121 * 122 * @param key The key 123 * @param value The Byte 124 */ add(String key, @Nullable Byte value)125 MapSerializer add(String key, @Nullable Byte value); 126 127 /** 128 * Adds a Short entry 129 * 130 * @param key The key 131 * @param value The Short 132 */ add(String key, @Nullable Short value)133 MapSerializer add(String key, @Nullable Short value); 134 135 /** 136 * Adds an Integer entry 137 * 138 * @param key The key 139 * @param value The Integer 140 */ add(String key, @Nullable Integer value)141 MapSerializer add(String key, @Nullable Integer value); 142 143 /** 144 * Adds a Long entry 145 * 146 * @param key The key 147 * @param value The Long 148 */ add(String key, @Nullable Long value)149 MapSerializer add(String key, @Nullable Long value); 150 151 /** 152 * Adds a Float entry 153 * 154 * @param key The key 155 * @param value The Float 156 */ add(String key, @Nullable Float value)157 MapSerializer add(String key, @Nullable Float value); 158 159 /** 160 * Adds a Double entry 161 * 162 * @param key The key 163 * @param value The Double 164 */ add(String key, @Nullable Double value)165 MapSerializer add(String key, @Nullable Double value); 166 167 /** 168 * Adds a Boolean entry 169 * 170 * @param key The key 171 * @param value The Boolean 172 */ add(String key, @Nullable Boolean value)173 MapSerializer add(String key, @Nullable Boolean value); 174 175 /** 176 * Adds a Enum entry 177 * 178 * @param key The key 179 * @param value The Enum 180 */ add(String key, @Nullable Enum<T> value)181 <T extends Enum<T>> MapSerializer add(String key, @Nullable Enum<T> value); 182 183 /** 184 * Similar to Map.of, but create a LinkedHashMap preserving insertion order for predictable 185 * serialization. 186 * 187 * @param keysAndValues a even number of items, repeating String key and Object value. 188 * @return A LinkedHashMap. 189 */ orderedOf(Object... keysAndValues)190 static LinkedHashMap<String, Object> orderedOf(Object... keysAndValues) { 191 final LinkedHashMap<String, Object> map = new LinkedHashMap<>(); 192 for (int i = 0; i < keysAndValues.length; i += 2) { 193 map.put((String) keysAndValues[i], keysAndValues[i + 1]); 194 } 195 return map; 196 } 197 } 198