summaryrefslogtreecommitdiffstats
path: root/packages/StatementService/src/com/android/statementservice/retriever/Statement.java
blob: f83edafe0eae3ff05e761ce9d744aa599f68c3de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.statementservice.retriever;

import android.annotation.NonNull;

/**
 * An immutable value type representing a statement, consisting of a source, target, and relation.
 * This reflects an assertion that the relation holds for the source, target pair. For example, if a
 * web site has the following in its associations.json file:
 *
 * <pre>
 * {
 * "relation": ["delegate_permission/common.handle_all_urls"],
 * "target"  : {"namespace": "android_app", "package_name": "com.example.app",
 *              "sha256_cert_fingerprints": ["00:11:22:33"] }
 * }
 * </pre>
 *
 * Then invoking {@link AbstractStatementRetriever#retrieveStatements(AbstractAsset)} will return a
 * {@link Statement} with {@link #getSource} equal to the input parameter, {@link #getRelation}
 * equal to
 *
 * <pre>Relation.create("delegate_permission", "common.get_login_creds");</pre>
 *
 * and with {@link #getTarget} equal to
 *
 * <pre>AbstractAsset.create("{\"namespace\" : \"android_app\","
 *                           + "\"package_name\": \"com.example.app\"}"
 *                           + "\"sha256_cert_fingerprints\": \"[\"00:11:22:33\"]\"}");
 * </pre>
 */
public final class Statement {

    private final AbstractAsset mTarget;
    private final Relation mRelation;
    private final AbstractAsset mSource;

    private Statement(AbstractAsset source, AbstractAsset target, Relation relation) {
        mSource = source;
        mTarget = target;
        mRelation = relation;
    }

    /**
     * Returns the source asset of the statement.
     */
    @NonNull
    public AbstractAsset getSource() {
        return mSource;
    }

    /**
     * Returns the target asset of the statement.
     */
    @NonNull
    public AbstractAsset getTarget() {
        return mTarget;
    }

    /**
     * Returns the relation of the statement.
     */
    @NonNull
    public Relation getRelation() {
        return mRelation;
    }

    /**
     * Creates a new Statement object for the specified target asset and relation. For example:
     * <pre>
     *   Asset asset = Asset.Factory.create(
     *       "{\"namespace\" : \"web\",\"site\": \"https://www.test.com\"}");
     *   Relation relation = Relation.create("delegate_permission", "common.get_login_creds");
     *   Statement statement = Statement.create(asset, relation);
     * </pre>
     */
    public static Statement create(@NonNull AbstractAsset source, @NonNull AbstractAsset target,
                                   @NonNull Relation relation) {
        return new Statement(source, target, relation);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Statement statement = (Statement) o;

        if (!mRelation.equals(statement.mRelation)) {
            return false;
        }
        if (!mTarget.equals(statement.mTarget)) {
            return false;
        }
        if (!mSource.equals(statement.mSource)) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = mTarget.hashCode();
        result = 31 * result + mRelation.hashCode();
        result = 31 * result + mSource.hashCode();
        return result;
    }

    @Override
    public String toString() {
        StringBuilder statement = new StringBuilder();
        statement.append("Statement: ");
        statement.append(mSource);
        statement.append(", ");
        statement.append(mTarget);
        statement.append(", ");
        statement.append(mRelation);
        return statement.toString();
    }
}