1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.components.bookmarks;
7 import android.text.TextUtils;
8 import android.util.Log;
10 import org.chromium.base.CalledByNative;
13 * Simple object representing the bookmark id.
15 public class BookmarkId {
16 public static final int INVALID_FOLDER_ID = -2;
17 public static final int ROOT_FOLDER_ID = -1;
19 private static final String LOG_TAG = "BookmarkId";
20 private static final char TYPE_PARTNER = 'p';
22 private final long mId;
23 private final int mType;
25 public BookmarkId(long id, int type) {
31 * @param c The char representing the type.
32 * @return The Bookmark type from a char representing the type.
34 private static int getBookmarkTypeFromChar(char c) {
37 return BookmarkType.BOOKMARK_TYPE_PARTNER;
39 return BookmarkType.BOOKMARK_TYPE_NORMAL;
44 * @param c The char representing the bookmark type.
45 * @return Whether the char representing the bookmark type is a valid type.
47 private static boolean isValidBookmarkTypeFromChar(char c) {
48 return c == TYPE_PARTNER;
52 * @param s The bookmark id string (Eg: p1 for partner bookmark id 1).
53 * @return the Bookmark id from the string which is a concatenation of
54 * bookmark type and the bookmark id.
56 public static BookmarkId getBookmarkIdFromString(String s) {
57 long id = ROOT_FOLDER_ID;
58 int type = BookmarkType.BOOKMARK_TYPE_NORMAL;
59 if (TextUtils.isEmpty(s))
60 return new BookmarkId(id, type);
61 char folderTypeChar = s.charAt(0);
62 if (isValidBookmarkTypeFromChar(folderTypeChar)) {
63 type = getBookmarkTypeFromChar(folderTypeChar);
67 id = Long.parseLong(s);
68 } catch (NumberFormatException exception) {
69 Log.e(LOG_TAG, "Error parsing url to extract the bookmark folder id.", exception);
71 return new BookmarkId(id, type);
75 * @return The id of the bookmark.
83 * @return The bookmark type.
86 public int getType() {
90 private String getBookmarkTypeString() {
92 case BookmarkType.BOOKMARK_TYPE_PARTNER:
93 return String.valueOf(TYPE_PARTNER);
94 case BookmarkType.BOOKMARK_TYPE_NORMAL:
101 public String toString() {
102 return getBookmarkTypeString() + mId;
106 public boolean equals(Object o) {
107 if (!(o instanceof BookmarkId))
109 BookmarkId item = (BookmarkId) o;
110 return (item.mId == mId && item.mType == mType);
114 public int hashCode() {
115 return toString().hashCode();