PageRenderTime 330ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/core/java/android/content/ContentValues.java

https://gitlab.com/drgroovestarr/frameworks_base
Java | 533 lines | 264 code | 46 blank | 223 comment | 30 complexity | d1857c05fb05191e3bb7729aa2bfc33a MD5 | raw file
  1. /*
  2. * Copyright (C) 2007 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 android.content;
  17. import android.os.Parcel;
  18. import android.os.Parcelable;
  19. import android.util.Log;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.Map;
  23. import java.util.Set;
  24. /**
  25. * This class is used to store a set of values that the {@link ContentResolver}
  26. * can process.
  27. */
  28. public final class ContentValues implements Parcelable {
  29. public static final String TAG = "ContentValues";
  30. /** Holds the actual values */
  31. private HashMap<String, Object> mValues;
  32. /**
  33. * Creates an empty set of values using the default initial size
  34. */
  35. public ContentValues() {
  36. // Choosing a default size of 8 based on analysis of typical
  37. // consumption by applications.
  38. mValues = new HashMap<String, Object>(8);
  39. }
  40. /**
  41. * Creates an empty set of values using the given initial size
  42. *
  43. * @param size the initial size of the set of values
  44. */
  45. public ContentValues(int size) {
  46. mValues = new HashMap<String, Object>(size, 1.0f);
  47. }
  48. /**
  49. * Creates a set of values copied from the given set
  50. *
  51. * @param from the values to copy
  52. */
  53. public ContentValues(ContentValues from) {
  54. mValues = new HashMap<String, Object>(from.mValues);
  55. }
  56. /**
  57. * Creates a set of values copied from the given HashMap. This is used
  58. * by the Parcel unmarshalling code.
  59. *
  60. * @param values the values to start with
  61. * {@hide}
  62. */
  63. private ContentValues(HashMap<String, Object> values) {
  64. mValues = values;
  65. }
  66. @Override
  67. public boolean equals(Object object) {
  68. if (!(object instanceof ContentValues)) {
  69. return false;
  70. }
  71. return mValues.equals(((ContentValues) object).mValues);
  72. }
  73. @Override
  74. public int hashCode() {
  75. return mValues.hashCode();
  76. }
  77. /**
  78. * Adds a value to the set.
  79. *
  80. * @param key the name of the value to put
  81. * @param value the data for the value to put
  82. */
  83. public void put(String key, String value) {
  84. mValues.put(key, value);
  85. }
  86. /**
  87. * Adds all values from the passed in ContentValues.
  88. *
  89. * @param other the ContentValues from which to copy
  90. */
  91. public void putAll(ContentValues other) {
  92. mValues.putAll(other.mValues);
  93. }
  94. /**
  95. * Adds a value to the set.
  96. *
  97. * @param key the name of the value to put
  98. * @param value the data for the value to put
  99. */
  100. public void put(String key, Byte value) {
  101. mValues.put(key, value);
  102. }
  103. /**
  104. * Adds a value to the set.
  105. *
  106. * @param key the name of the value to put
  107. * @param value the data for the value to put
  108. */
  109. public void put(String key, Short value) {
  110. mValues.put(key, value);
  111. }
  112. /**
  113. * Adds a value to the set.
  114. *
  115. * @param key the name of the value to put
  116. * @param value the data for the value to put
  117. */
  118. public void put(String key, Integer value) {
  119. mValues.put(key, value);
  120. }
  121. /**
  122. * Adds a value to the set.
  123. *
  124. * @param key the name of the value to put
  125. * @param value the data for the value to put
  126. */
  127. public void put(String key, Long value) {
  128. mValues.put(key, value);
  129. }
  130. /**
  131. * Adds a value to the set.
  132. *
  133. * @param key the name of the value to put
  134. * @param value the data for the value to put
  135. */
  136. public void put(String key, Float value) {
  137. mValues.put(key, value);
  138. }
  139. /**
  140. * Adds a value to the set.
  141. *
  142. * @param key the name of the value to put
  143. * @param value the data for the value to put
  144. */
  145. public void put(String key, Double value) {
  146. mValues.put(key, value);
  147. }
  148. /**
  149. * Adds a value to the set.
  150. *
  151. * @param key the name of the value to put
  152. * @param value the data for the value to put
  153. */
  154. public void put(String key, Boolean value) {
  155. mValues.put(key, value);
  156. }
  157. /**
  158. * Adds a value to the set.
  159. *
  160. * @param key the name of the value to put
  161. * @param value the data for the value to put
  162. */
  163. public void put(String key, byte[] value) {
  164. mValues.put(key, value);
  165. }
  166. /**
  167. * Adds a null value to the set.
  168. *
  169. * @param key the name of the value to make null
  170. */
  171. public void putNull(String key) {
  172. mValues.put(key, null);
  173. }
  174. /**
  175. * Returns the number of values.
  176. *
  177. * @return the number of values
  178. */
  179. public int size() {
  180. return mValues.size();
  181. }
  182. /**
  183. * Indicates whether this collection is empty.
  184. *
  185. * @return true iff size == 0
  186. * {@hide}
  187. * TODO: consider exposing this new method publicly
  188. */
  189. public boolean isEmpty() {
  190. return mValues.isEmpty();
  191. }
  192. /**
  193. * Remove a single value.
  194. *
  195. * @param key the name of the value to remove
  196. */
  197. public void remove(String key) {
  198. mValues.remove(key);
  199. }
  200. /**
  201. * Removes all values.
  202. */
  203. public void clear() {
  204. mValues.clear();
  205. }
  206. /**
  207. * Returns true if this object has the named value.
  208. *
  209. * @param key the value to check for
  210. * @return {@code true} if the value is present, {@code false} otherwise
  211. */
  212. public boolean containsKey(String key) {
  213. return mValues.containsKey(key);
  214. }
  215. /**
  216. * Gets a value. Valid value types are {@link String}, {@link Boolean},
  217. * {@link Number}, and {@code byte[]} implementations.
  218. *
  219. * @param key the value to get
  220. * @return the data for the value, or {@code null} if the value is missing or if {@code null}
  221. * was previously added with the given {@code key}
  222. */
  223. public Object get(String key) {
  224. return mValues.get(key);
  225. }
  226. /**
  227. * Gets a value and converts it to a String.
  228. *
  229. * @param key the value to get
  230. * @return the String for the value
  231. */
  232. public String getAsString(String key) {
  233. Object value = mValues.get(key);
  234. return value != null ? value.toString() : null;
  235. }
  236. /**
  237. * Gets a value and converts it to a Long.
  238. *
  239. * @param key the value to get
  240. * @return the Long value, or {@code null} if the value is missing or cannot be converted
  241. */
  242. public Long getAsLong(String key) {
  243. Object value = mValues.get(key);
  244. try {
  245. return value != null ? ((Number) value).longValue() : null;
  246. } catch (ClassCastException e) {
  247. if (value instanceof CharSequence) {
  248. try {
  249. return Long.valueOf(value.toString());
  250. } catch (NumberFormatException e2) {
  251. Log.e(TAG, "Cannot parse Long value for " + value + " at key " + key);
  252. return null;
  253. }
  254. } else {
  255. Log.e(TAG, "Cannot cast value for " + key + " to a Long: " + value, e);
  256. return null;
  257. }
  258. }
  259. }
  260. /**
  261. * Gets a value and converts it to an Integer.
  262. *
  263. * @param key the value to get
  264. * @return the Integer value, or {@code null} if the value is missing or cannot be converted
  265. */
  266. public Integer getAsInteger(String key) {
  267. Object value = mValues.get(key);
  268. try {
  269. return value != null ? ((Number) value).intValue() : null;
  270. } catch (ClassCastException e) {
  271. if (value instanceof CharSequence) {
  272. try {
  273. return Integer.valueOf(value.toString());
  274. } catch (NumberFormatException e2) {
  275. Log.e(TAG, "Cannot parse Integer value for " + value + " at key " + key);
  276. return null;
  277. }
  278. } else {
  279. Log.e(TAG, "Cannot cast value for " + key + " to a Integer: " + value, e);
  280. return null;
  281. }
  282. }
  283. }
  284. /**
  285. * Gets a value and converts it to a Short.
  286. *
  287. * @param key the value to get
  288. * @return the Short value, or {@code null} if the value is missing or cannot be converted
  289. */
  290. public Short getAsShort(String key) {
  291. Object value = mValues.get(key);
  292. try {
  293. return value != null ? ((Number) value).shortValue() : null;
  294. } catch (ClassCastException e) {
  295. if (value instanceof CharSequence) {
  296. try {
  297. return Short.valueOf(value.toString());
  298. } catch (NumberFormatException e2) {
  299. Log.e(TAG, "Cannot parse Short value for " + value + " at key " + key);
  300. return null;
  301. }
  302. } else {
  303. Log.e(TAG, "Cannot cast value for " + key + " to a Short: " + value, e);
  304. return null;
  305. }
  306. }
  307. }
  308. /**
  309. * Gets a value and converts it to a Byte.
  310. *
  311. * @param key the value to get
  312. * @return the Byte value, or {@code null} if the value is missing or cannot be converted
  313. */
  314. public Byte getAsByte(String key) {
  315. Object value = mValues.get(key);
  316. try {
  317. return value != null ? ((Number) value).byteValue() : null;
  318. } catch (ClassCastException e) {
  319. if (value instanceof CharSequence) {
  320. try {
  321. return Byte.valueOf(value.toString());
  322. } catch (NumberFormatException e2) {
  323. Log.e(TAG, "Cannot parse Byte value for " + value + " at key " + key);
  324. return null;
  325. }
  326. } else {
  327. Log.e(TAG, "Cannot cast value for " + key + " to a Byte: " + value, e);
  328. return null;
  329. }
  330. }
  331. }
  332. /**
  333. * Gets a value and converts it to a Double.
  334. *
  335. * @param key the value to get
  336. * @return the Double value, or {@code null} if the value is missing or cannot be converted
  337. */
  338. public Double getAsDouble(String key) {
  339. Object value = mValues.get(key);
  340. try {
  341. return value != null ? ((Number) value).doubleValue() : null;
  342. } catch (ClassCastException e) {
  343. if (value instanceof CharSequence) {
  344. try {
  345. return Double.valueOf(value.toString());
  346. } catch (NumberFormatException e2) {
  347. Log.e(TAG, "Cannot parse Double value for " + value + " at key " + key);
  348. return null;
  349. }
  350. } else {
  351. Log.e(TAG, "Cannot cast value for " + key + " to a Double: " + value, e);
  352. return null;
  353. }
  354. }
  355. }
  356. /**
  357. * Gets a value and converts it to a Float.
  358. *
  359. * @param key the value to get
  360. * @return the Float value, or {@code null} if the value is missing or cannot be converted
  361. */
  362. public Float getAsFloat(String key) {
  363. Object value = mValues.get(key);
  364. try {
  365. return value != null ? ((Number) value).floatValue() : null;
  366. } catch (ClassCastException e) {
  367. if (value instanceof CharSequence) {
  368. try {
  369. return Float.valueOf(value.toString());
  370. } catch (NumberFormatException e2) {
  371. Log.e(TAG, "Cannot parse Float value for " + value + " at key " + key);
  372. return null;
  373. }
  374. } else {
  375. Log.e(TAG, "Cannot cast value for " + key + " to a Float: " + value, e);
  376. return null;
  377. }
  378. }
  379. }
  380. /**
  381. * Gets a value and converts it to a Boolean.
  382. *
  383. * @param key the value to get
  384. * @return the Boolean value, or {@code null} if the value is missing or cannot be converted
  385. */
  386. public Boolean getAsBoolean(String key) {
  387. Object value = mValues.get(key);
  388. try {
  389. return (Boolean) value;
  390. } catch (ClassCastException e) {
  391. if (value instanceof CharSequence) {
  392. // Note that we also check against 1 here because SQLite's internal representation
  393. // for booleans is an integer with a value of 0 or 1. Without this check, boolean
  394. // values obtained via DatabaseUtils#cursorRowToContentValues will always return
  395. // false.
  396. return Boolean.valueOf(value.toString()) || "1".equals(value);
  397. } else if (value instanceof Number) {
  398. return ((Number) value).intValue() != 0;
  399. } else {
  400. Log.e(TAG, "Cannot cast value for " + key + " to a Boolean: " + value, e);
  401. return null;
  402. }
  403. }
  404. }
  405. /**
  406. * Gets a value that is a byte array. Note that this method will not convert
  407. * any other types to byte arrays.
  408. *
  409. * @param key the value to get
  410. * @return the {@code byte[]} value, or {@code null} is the value is missing or not a
  411. * {@code byte[]}
  412. */
  413. public byte[] getAsByteArray(String key) {
  414. Object value = mValues.get(key);
  415. if (value instanceof byte[]) {
  416. return (byte[]) value;
  417. } else {
  418. return null;
  419. }
  420. }
  421. /**
  422. * Returns a set of all of the keys and values
  423. *
  424. * @return a set of all of the keys and values
  425. */
  426. public Set<Map.Entry<String, Object>> valueSet() {
  427. return mValues.entrySet();
  428. }
  429. /**
  430. * Returns a set of all of the keys
  431. *
  432. * @return a set of all of the keys
  433. */
  434. public Set<String> keySet() {
  435. return mValues.keySet();
  436. }
  437. public static final Parcelable.Creator<ContentValues> CREATOR =
  438. new Parcelable.Creator<ContentValues>() {
  439. @SuppressWarnings({"deprecation", "unchecked"})
  440. public ContentValues createFromParcel(Parcel in) {
  441. // TODO - what ClassLoader should be passed to readHashMap?
  442. HashMap<String, Object> values = in.readHashMap(null);
  443. return new ContentValues(values);
  444. }
  445. public ContentValues[] newArray(int size) {
  446. return new ContentValues[size];
  447. }
  448. };
  449. public int describeContents() {
  450. return 0;
  451. }
  452. @SuppressWarnings("deprecation")
  453. public void writeToParcel(Parcel parcel, int flags) {
  454. parcel.writeMap(mValues);
  455. }
  456. /**
  457. * Unsupported, here until we get proper bulk insert APIs.
  458. * {@hide}
  459. */
  460. @Deprecated
  461. public void putStringArrayList(String key, ArrayList<String> value) {
  462. mValues.put(key, value);
  463. }
  464. /**
  465. * Unsupported, here until we get proper bulk insert APIs.
  466. * {@hide}
  467. */
  468. @SuppressWarnings("unchecked")
  469. @Deprecated
  470. public ArrayList<String> getStringArrayList(String key) {
  471. return (ArrayList<String>) mValues.get(key);
  472. }
  473. /**
  474. * Returns a string containing a concise, human-readable description of this object.
  475. * @return a printable representation of this object.
  476. */
  477. @Override
  478. public String toString() {
  479. StringBuilder sb = new StringBuilder();
  480. for (String name : mValues.keySet()) {
  481. String value = getAsString(name);
  482. if (sb.length() > 0) sb.append(" ");
  483. sb.append(name + "=" + value);
  484. }
  485. return sb.toString();
  486. }
  487. }