Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / components / bookmarks / common / android / java / src / org / chromium / components / bookmarks / BookmarkId.java
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.
4
5 package org.chromium.components.bookmarks;
6
7 import android.text.TextUtils;
8 import android.util.Log;
9
10 import org.chromium.base.CalledByNative;
11
12 /**
13  * Simple object representing the bookmark id.
14  */
15 public class BookmarkId {
16     public static final int INVALID_FOLDER_ID = -2;
17     public static final int ROOT_FOLDER_ID = -1;
18
19     private static final String LOG_TAG = "BookmarkId";
20     private static final char TYPE_PARTNER = 'p';
21
22     private final long mId;
23     private final int mType;
24
25     public BookmarkId(long id, int type) {
26         mId = id;
27         mType = type;
28     }
29
30     /**
31      * @param c The char representing the type.
32      * @return The Bookmark type from a char representing the type.
33      */
34     private static int getBookmarkTypeFromChar(char c) {
35         switch (c) {
36             case TYPE_PARTNER:
37                 return BookmarkType.BOOKMARK_TYPE_PARTNER;
38             default:
39                 return BookmarkType.BOOKMARK_TYPE_NORMAL;
40         }
41     }
42
43     /**
44      * @param c The char representing the bookmark type.
45      * @return Whether the char representing the bookmark type is a valid type.
46      */
47     private static boolean isValidBookmarkTypeFromChar(char c) {
48         return c == TYPE_PARTNER;
49     }
50
51     /**
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.
55      */
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);
64             s = s.substring(1);
65         }
66         try {
67             id = Long.parseLong(s);
68         } catch (NumberFormatException exception) {
69             Log.e(LOG_TAG, "Error parsing url to extract the bookmark folder id.", exception);
70         }
71         return new BookmarkId(id, type);
72     }
73
74     /**
75      * @return The id of the bookmark.
76      */
77     @CalledByNative
78     public long getId() {
79         return mId;
80     }
81
82     /**
83      * @return The bookmark type.
84      */
85     @CalledByNative
86     public int getType() {
87         return mType;
88     }
89
90     private String getBookmarkTypeString() {
91         switch (mType) {
92             case BookmarkType.BOOKMARK_TYPE_PARTNER:
93                 return String.valueOf(TYPE_PARTNER);
94             case BookmarkType.BOOKMARK_TYPE_NORMAL:
95             default:
96                 return "";
97         }
98     }
99
100     @Override
101     public String toString() {
102         return getBookmarkTypeString() + mId;
103     }
104
105     @Override
106     public boolean equals(Object o) {
107         if (!(o instanceof BookmarkId))
108             return false;
109         BookmarkId item = (BookmarkId) o;
110         return (item.mId == mId && item.mType == mType);
111     }
112
113     @Override
114     public int hashCode() {
115         return toString().hashCode();
116     }
117 }