Snowplow Android Tracker 3.1.2
GlobalContext.java
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015-2022 Snowplow Analytics Ltd. All rights reserved.
3 *
4 * This program is licensed to you under the Apache License Version 2.0,
5 * and you may not use this file except in compliance with the Apache License Version 2.0.
6 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
7 *
8 * Unless required by applicable law or agreed to in writing,
9 * software distributed under the Apache License Version 2.0 is distributed on an
10 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
12 */
13
14package com.snowplowanalytics.snowplow.globalcontexts;
15
16import androidx.annotation.NonNull;
17import androidx.annotation.Nullable;
18
19import com.snowplowanalytics.snowplow.tracker.InspectableEvent;
20import com.snowplowanalytics.snowplow.payload.SelfDescribingJson;
21
22import java.util.ArrayList;
23import java.util.List;
24import java.util.Objects;
25
26public class GlobalContext {
27
28 @NonNull
29 private FunctionalGenerator generator;
30 @Nullable
31 private FunctionalFilter filter;
32
33
34 public GlobalContext(@NonNull ContextGenerator contextGenerator) {
35 Objects.requireNonNull(contextGenerator);
36 this.generator = new FunctionalGenerator() {
37 @Override
38 @Nullable
39 public List<SelfDescribingJson> apply(@NonNull InspectableEvent event) {
40 return contextGenerator.generateContexts(event);
41 }
42 };
43 this.filter = new FunctionalFilter() {
44 @Override
45 public boolean apply(@NonNull InspectableEvent event) {
46 return contextGenerator.filterEvent(event);
47 }
48 };
49 }
50
51 public GlobalContext(@NonNull List<SelfDescribingJson> staticContexts) {
52 Objects.requireNonNull(staticContexts);
53 this.generator = new FunctionalGenerator() {
54 @Override
55 @Nullable
56 public List<SelfDescribingJson> apply(@NonNull InspectableEvent event) {
57 return staticContexts;
58 }
59 };
60 this.filter = null;
61 }
62
63 public GlobalContext(@NonNull FunctionalGenerator generator) {
64 this(generator, (FunctionalFilter) null);
65 }
66
67 public GlobalContext(@NonNull List<SelfDescribingJson> staticContexts, @Nullable SchemaRuleSet ruleset) {
68 Objects.requireNonNull(staticContexts);
69 this.generator = new FunctionalGenerator() {
70 @Override
71 @Nullable
72 public List<SelfDescribingJson> apply(@NonNull InspectableEvent event) {
73 return staticContexts;
74 }
75 };
76 this.filter = ruleset.getFilter();
77 }
78
79 public GlobalContext(@NonNull FunctionalGenerator generator, @Nullable SchemaRuleSet ruleset) {
80 this(generator, ruleset.getFilter());
81 }
82
83 public GlobalContext(@NonNull List<SelfDescribingJson> staticContexts, @Nullable FunctionalFilter filter) {
84 Objects.requireNonNull(staticContexts);
85 this.generator = new FunctionalGenerator() {
86 @Override
87 @Nullable
88 public List<SelfDescribingJson> apply(@NonNull InspectableEvent event) {
89 return staticContexts;
90 }
91 };
92 this.filter = filter;
93 }
94
95 public GlobalContext(@NonNull FunctionalGenerator generator, @Nullable FunctionalFilter filter) {
96 Objects.requireNonNull(generator);
97 this.generator = generator;
98 this.filter = filter;
99 }
100
101 @NonNull
102 public List<SelfDescribingJson> generateContexts(@NonNull InspectableEvent event) {
103 if (filter != null && !filter.apply(event)) {
104 return new ArrayList<>();
105 }
106 return generator.apply(event);
107 }
108}
GlobalContext(@NonNull FunctionalGenerator generator, @Nullable FunctionalFilter filter)
GlobalContext(@NonNull List< SelfDescribingJson > staticContexts, @Nullable SchemaRuleSet ruleset)
List< SelfDescribingJson > generateContexts(@NonNull InspectableEvent event)
GlobalContext(@NonNull ContextGenerator contextGenerator)
GlobalContext(@NonNull List< SelfDescribingJson > staticContexts)
GlobalContext(@NonNull List< SelfDescribingJson > staticContexts, @Nullable FunctionalFilter filter)
GlobalContext(@NonNull FunctionalGenerator generator, @Nullable SchemaRuleSet ruleset)
GlobalContext(@NonNull FunctionalGenerator generator)