PageRenderTime 39ms CodeModel.GetById 8ms RepoModel.GetById 0ms app.codeStats 0ms

/src/a_vcard/android/content/ContentValues.java

https://code.google.com/p/android-vcard/
Java | 501 lines | 239 code | 43 blank | 219 comment | 26 complexity | 8a75023bf789105c02d48bda7b9414bb MD5 | raw file
Possible License(s): Apache-2.0
  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 a_vcard.android.content;
  17. //import android.os.Parcel;
  18. //import android.os.Parcelable;
  19. import a_vcard.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 from 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. * Remove a single value.
  184. *
  185. * @param key the name of the value to remove
  186. */
  187. public void remove(String key) {
  188. mValues.remove(key);
  189. }
  190. /**
  191. * Removes all values.
  192. */
  193. public void clear() {
  194. mValues.clear();
  195. }
  196. /**
  197. * Returns true if this object has the named value.
  198. *
  199. * @param key the value to check for
  200. * @return {@code true} if the value is present, {@code false} otherwise
  201. */
  202. public boolean containsKey(String key) {
  203. return mValues.containsKey(key);
  204. }
  205. /**
  206. * Gets a value. Valid value types are {@link String}, {@link Boolean}, and
  207. * {@link Number} implementations.
  208. *
  209. * @param key the value to get
  210. * @return the data for the value
  211. */
  212. public Object get(String key) {
  213. return mValues.get(key);
  214. }
  215. /**
  216. * Gets a value and converts it to a String.
  217. *
  218. * @param key the value to get
  219. * @return the String for the value
  220. */
  221. public String getAsString(String key) {
  222. Object value = mValues.get(key);
  223. return value != null ? mValues.get(key).toString() : null;
  224. }
  225. /**
  226. * Gets a value and converts it to a Long.
  227. *
  228. * @param key the value to get
  229. * @return the Long value, or null if the value is missing or cannot be converted
  230. */
  231. public Long getAsLong(String key) {
  232. Object value = mValues.get(key);
  233. try {
  234. return value != null ? ((Number) value).longValue() : null;
  235. } catch (ClassCastException e) {
  236. if (value instanceof CharSequence) {
  237. try {
  238. return Long.valueOf(value.toString());
  239. } catch (NumberFormatException e2) {
  240. Log.e(TAG, "Cannot parse Long value for " + value + " at key " + key);
  241. return null;
  242. }
  243. } else {
  244. Log.e(TAG, "Cannot cast value for " + key + " to a Long");
  245. return null;
  246. }
  247. }
  248. }
  249. /**
  250. * Gets a value and converts it to an Integer.
  251. *
  252. * @param key the value to get
  253. * @return the Integer value, or null if the value is missing or cannot be converted
  254. */
  255. public Integer getAsInteger(String key) {
  256. Object value = mValues.get(key);
  257. try {
  258. return value != null ? ((Number) value).intValue() : null;
  259. } catch (ClassCastException e) {
  260. if (value instanceof CharSequence) {
  261. try {
  262. return Integer.valueOf(value.toString());
  263. } catch (NumberFormatException e2) {
  264. Log.e(TAG, "Cannot parse Integer value for " + value + " at key " + key);
  265. return null;
  266. }
  267. } else {
  268. Log.e(TAG, "Cannot cast value for " + key + " to a Integer");
  269. return null;
  270. }
  271. }
  272. }
  273. /**
  274. * Gets a value and converts it to a Short.
  275. *
  276. * @param key the value to get
  277. * @return the Short value, or null if the value is missing or cannot be converted
  278. */
  279. public Short getAsShort(String key) {
  280. Object value = mValues.get(key);
  281. try {
  282. return value != null ? ((Number) value).shortValue() : null;
  283. } catch (ClassCastException e) {
  284. if (value instanceof CharSequence) {
  285. try {
  286. return Short.valueOf(value.toString());
  287. } catch (NumberFormatException e2) {
  288. Log.e(TAG, "Cannot parse Short value for " + value + " at key " + key);
  289. return null;
  290. }
  291. } else {
  292. Log.e(TAG, "Cannot cast value for " + key + " to a Short");
  293. return null;
  294. }
  295. }
  296. }
  297. /**
  298. * Gets a value and converts it to a Byte.
  299. *
  300. * @param key the value to get
  301. * @return the Byte value, or null if the value is missing or cannot be converted
  302. */
  303. public Byte getAsByte(String key) {
  304. Object value = mValues.get(key);
  305. try {
  306. return value != null ? ((Number) value).byteValue() : null;
  307. } catch (ClassCastException e) {
  308. if (value instanceof CharSequence) {
  309. try {
  310. return Byte.valueOf(value.toString());
  311. } catch (NumberFormatException e2) {
  312. Log.e(TAG, "Cannot parse Byte value for " + value + " at key " + key);
  313. return null;
  314. }
  315. } else {
  316. Log.e(TAG, "Cannot cast value for " + key + " to a Byte");
  317. return null;
  318. }
  319. }
  320. }
  321. /**
  322. * Gets a value and converts it to a Double.
  323. *
  324. * @param key the value to get
  325. * @return the Double value, or null if the value is missing or cannot be converted
  326. */
  327. public Double getAsDouble(String key) {
  328. Object value = mValues.get(key);
  329. try {
  330. return value != null ? ((Number) value).doubleValue() : null;
  331. } catch (ClassCastException e) {
  332. if (value instanceof CharSequence) {
  333. try {
  334. return Double.valueOf(value.toString());
  335. } catch (NumberFormatException e2) {
  336. Log.e(TAG, "Cannot parse Double value for " + value + " at key " + key);
  337. return null;
  338. }
  339. } else {
  340. Log.e(TAG, "Cannot cast value for " + key + " to a Double");
  341. return null;
  342. }
  343. }
  344. }
  345. /**
  346. * Gets a value and converts it to a Float.
  347. *
  348. * @param key the value to get
  349. * @return the Float value, or null if the value is missing or cannot be converted
  350. */
  351. public Float getAsFloat(String key) {
  352. Object value = mValues.get(key);
  353. try {
  354. return value != null ? ((Number) value).floatValue() : null;
  355. } catch (ClassCastException e) {
  356. if (value instanceof CharSequence) {
  357. try {
  358. return Float.valueOf(value.toString());
  359. } catch (NumberFormatException e2) {
  360. Log.e(TAG, "Cannot parse Float value for " + value + " at key " + key);
  361. return null;
  362. }
  363. } else {
  364. Log.e(TAG, "Cannot cast value for " + key + " to a Float");
  365. return null;
  366. }
  367. }
  368. }
  369. /**
  370. * Gets a value and converts it to a Boolean.
  371. *
  372. * @param key the value to get
  373. * @return the Boolean value, or null if the value is missing or cannot be converted
  374. */
  375. public Boolean getAsBoolean(String key) {
  376. Object value = mValues.get(key);
  377. try {
  378. return (Boolean) value;
  379. } catch (ClassCastException e) {
  380. if (value instanceof CharSequence) {
  381. return Boolean.valueOf(value.toString());
  382. } else {
  383. Log.e(TAG, "Cannot cast value for " + key + " to a Boolean");
  384. return null;
  385. }
  386. }
  387. }
  388. /**
  389. * Gets a value that is a byte array. Note that this method will not convert
  390. * any other types to byte arrays.
  391. *
  392. * @param key the value to get
  393. * @return the byte[] value, or null is the value is missing or not a byte[]
  394. */
  395. public byte[] getAsByteArray(String key) {
  396. Object value = mValues.get(key);
  397. if (value instanceof byte[]) {
  398. return (byte[]) value;
  399. } else {
  400. return null;
  401. }
  402. }
  403. /**
  404. * Returns a set of all of the keys and values
  405. *
  406. * @return a set of all of the keys and values
  407. */
  408. public Set<Map.Entry<String, Object>> valueSet() {
  409. return mValues.entrySet();
  410. }
  411. // public static final Parcelable.Creator<ContentValues> CREATOR =
  412. // new Parcelable.Creator<ContentValues>() {
  413. // @SuppressWarnings({"deprecation", "unchecked"})
  414. // public ContentValues createFromParcel(Parcel in) {
  415. // // TODO - what ClassLoader should be passed to readHashMap?
  416. // HashMap<String, Object> values = in.readHashMap(null);
  417. // return new ContentValues(values);
  418. // }
  419. //
  420. // public ContentValues[] newArray(int size) {
  421. // return new ContentValues[size];
  422. // }
  423. // };
  424. public int describeContents() {
  425. return 0;
  426. }
  427. // @SuppressWarnings("deprecation")
  428. // public void writeToParcel(Parcel parcel, int flags) {
  429. // parcel.writeMap(mValues);
  430. // }
  431. /**
  432. * Unsupported, here until we get proper bulk insert APIs.
  433. * {@hide}
  434. */
  435. @Deprecated
  436. public void putStringArrayList(String key, ArrayList<String> value) {
  437. mValues.put(key, value);
  438. }
  439. /**
  440. * Unsupported, here until we get proper bulk insert APIs.
  441. * {@hide}
  442. */
  443. @SuppressWarnings("unchecked")
  444. @Deprecated
  445. public ArrayList<String> getStringArrayList(String key) {
  446. return (ArrayList<String>) mValues.get(key);
  447. }
  448. @Override
  449. public String toString() {
  450. StringBuilder sb = new StringBuilder();
  451. for (String name : mValues.keySet()) {
  452. String value = getAsString(name);
  453. if (sb.length() > 0) sb.append(" ");
  454. sb.append(name + "=" + value);
  455. }
  456. return sb.toString();
  457. }
  458. }