1 /* 2 * Copyright 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.camera.core.impl; 18 19 import android.util.ArrayMap; 20 21 import org.jspecify.annotations.NonNull; 22 23 import java.util.Map; 24 25 /** 26 * A mutable {@link TagBundle} which allows insertion/removal. 27 */ 28 public class MutableTagBundle extends TagBundle { 29 MutableTagBundle(Map<String, Object> source)30 private MutableTagBundle(Map<String, Object> source) { 31 super(source); 32 } 33 34 /** 35 * Creates an empty MutableTagBundle. 36 * 37 * @return an empty MutableTagBundle containing no tag. 38 */ create()39 public static @NonNull MutableTagBundle create() { 40 return new MutableTagBundle(new ArrayMap<>()); 41 } 42 43 /** 44 * Creates a MutableTagBundle from an existing TagBundle. 45 * 46 * @param otherTagBundle TagBundle to insert. 47 * @return a MutableTagBundle prepopulated with TagBundle. 48 */ from(@onNull TagBundle otherTagBundle)49 public static @NonNull MutableTagBundle from(@NonNull TagBundle otherTagBundle) { 50 Map<String, Object> tags = new ArrayMap<>(); 51 for (String key : otherTagBundle.listKeys()) { 52 tags.put(key, otherTagBundle.getTag(key)); 53 } 54 55 return new MutableTagBundle(tags); 56 } 57 58 /** Adds a tag with specified key. */ putTag(@onNull String key, @NonNull Object value)59 public void putTag(@NonNull String key, @NonNull Object value) { 60 // If the key exists, its value will be replaced. 61 mTagMap.put(key, value); 62 } 63 64 /** Merges the given bundle into current bundle. */ addTagBundle(@onNull TagBundle bundle)65 public void addTagBundle(@NonNull TagBundle bundle) { 66 if (mTagMap != null && bundle.mTagMap != null) { 67 mTagMap.putAll(bundle.mTagMap); 68 } 69 } 70 } 71