1 /* 2 * Copyright (C) 2018 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.core.content 18 19 import android.content.ContentValues 20 21 /** 22 * Returns a new [ContentValues] with the given key/value pairs as elements. 23 * 24 * @throws IllegalArgumentException When a value is not a supported type of [ContentValues]. 25 */ 26 fun contentValuesOf(vararg pairs: Pair<String, Any?>) = ContentValues(pairs.size).apply { 27 for ((key, value) in pairs) { 28 when (value) { 29 null -> putNull(key) 30 is String -> put(key, value) 31 is Int -> put(key, value) 32 is Long -> put(key, value) 33 is Boolean -> put(key, value) 34 is Float -> put(key, value) 35 is Double -> put(key, value) 36 is ByteArray -> put(key, value) 37 is Byte -> put(key, value) 38 is Short -> put(key, value) 39 else -> { 40 val valueType = value.javaClass.canonicalName 41 throw IllegalArgumentException("Illegal value type $valueType for key \"$key\"") 42 } 43 } 44 } 45 } 46