FolderUri.java revision 259df5b9e11908c8ef7c91483924891dd96b3c27
1/*
2 * Copyright (C) 2013 Google Inc.
3 * Licensed to The Android Open Source Project.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.mail.utils;
19
20import android.net.Uri;
21
22/**
23 * A holder for a Folder {@link Uri} that can be compared, ignoring any query parameters.
24 */
25public class FolderUri {
26    public static final FolderUri EMPTY = new FolderUri(Uri.EMPTY);
27
28    /**
29     * The full {@link Uri}. This should be used for any queries.
30     */
31    public final Uri fullUri;
32    /**
33     * Equivalent to {@link #fullUri}, but without any query parameters, and can safely be used in
34     * comparisons to determine if two {@link Uri}s point to the same object.
35     */
36    public final Uri comparisonUri;
37
38    public FolderUri(final Uri uri) {
39        fullUri = uri;
40        comparisonUri = buildComparisonUri(uri);
41    }
42
43    private static Uri buildComparisonUri(final Uri fullUri) {
44        final Uri.Builder builder = new Uri.Builder();
45        builder.scheme(fullUri .getScheme());
46        builder.path(fullUri.getPath());
47
48        return builder.build();
49    }
50
51    @Override
52    public int hashCode() {
53        return comparisonUri.hashCode();
54    }
55
56    @Override
57    public boolean equals(final Object o) {
58        if (o instanceof FolderUri) {
59            return comparisonUri.equals(((FolderUri) o).comparisonUri);
60        }
61
62        return comparisonUri.equals(o);
63    }
64
65    @Override
66    public String toString() {
67        return fullUri.toString();
68    }
69}
70