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    private Uri mComparisonUri = null;
37
38    public FolderUri(final Uri uri) {
39        fullUri = uri;
40    }
41
42    private static Uri buildComparisonUri(final Uri fullUri) {
43        final Uri.Builder builder = new Uri.Builder();
44        builder.scheme(fullUri .getScheme());
45        builder.encodedAuthority(fullUri.getEncodedAuthority());
46        builder.encodedPath(fullUri.getEncodedPath());
47
48        return builder.build();
49    }
50
51    public Uri getComparisonUri() {
52        if (mComparisonUri == null) {
53            mComparisonUri = buildComparisonUri(fullUri);
54        }
55
56        return mComparisonUri;
57    }
58
59    @Override
60    public int hashCode() {
61        return getComparisonUri().hashCode();
62    }
63
64    @Override
65    public boolean equals(final Object o) {
66        if (o instanceof FolderUri) {
67            return getComparisonUri().equals(((FolderUri) o).getComparisonUri());
68        }
69
70        return getComparisonUri().equals(o);
71    }
72
73    @Override
74    public String toString() {
75        return fullUri.toString();
76    }
77}
78