batch) {
60 | getListenerByTag(batch.getTag()).onReady(causingStrategy, batch);
61 | }
62 | }
--------------------------------------------------------------------------------
/batching-core/src/main/java/com/flipkart/batching/core/Data.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.flipkart.batching.core;
26 |
27 |
28 | import androidx.annotation.Keep;
29 |
30 | import com.flipkart.batching.core.data.Tag;
31 |
32 | import java.io.Serializable;
33 |
34 | /**
35 | * This is an abstract base class for storing data which implements {@link Serializable}.
36 | *
37 | * A custom data class must extend this class and call the super in the constructor with
38 | * {@link Tag} and {@link Object} as parameters.
39 | *
40 | * @see Tag
41 | * @see Object
42 | */
43 | @Keep
44 | public abstract class Data implements Serializable {
45 | private long eventId;
46 |
47 | /**
48 | * Constructor for Data object. This constructor takes {@link Tag} and {@link Object} as
49 | * parameter and generates an eventId = (System.currentTimeMillis() + System.nanoTime())
50 | */
51 |
52 | public Data() {
53 | this.eventId = System.currentTimeMillis() + System.nanoTime();
54 | }
55 |
56 | public long getEventId() {
57 | return eventId;
58 | }
59 |
60 | public void setEventId(long eventId) {
61 | this.eventId = eventId;
62 | }
63 |
64 | @Override
65 | public boolean equals(Object o) {
66 | if (o instanceof Data) {
67 | return ((Data) o).getEventId() == getEventId();
68 | } else {
69 | return super.equals(o);
70 | }
71 | }
72 |
73 | @Override
74 | public int hashCode() {
75 | return Long.valueOf(eventId).hashCode();
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/nav_header_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
25 |
26 |
37 |
38 |
44 |
45 |
51 |
52 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/batching/src/main/java/com/flipkart/batching/persistence/PersistenceStrategy.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.flipkart.batching.persistence;
26 |
27 | import com.flipkart.batching.core.Data;
28 |
29 | import java.util.Collection;
30 |
31 | /**
32 | * Interface for PersistenceStrategy. A persistence strategy must implement this interface
33 | * and override all it's methods. Persistence strategy is responsible for persisting the.
34 | */
35 | public interface PersistenceStrategy {
36 |
37 | /**
38 | * This method tells the persistence strategy about the added {@link Collection} of {@link Data}
39 | * and persist it according to the provided implementation of persistenceStrategy.
40 | *
41 | * @param dataCollection collection of {@link Data} objects
42 | */
43 | boolean add(Collection dataCollection);
44 |
45 | /**
46 | * This method returns {@link Collection} of persisted {@link Data} objects.
47 | *
48 | * @return collection of {@link Data} objects
49 | */
50 | Collection getData();
51 |
52 | /**
53 | * This method returns {@link int} size of persisted {@link Data} objects.
54 | *
55 | * @return size of {@link int} objects
56 | */
57 | int getDataSize();
58 |
59 | /**
60 | * This method removes the provided {@link Collection} of {@link Data} objects from
61 | * the provided implementation of {@link PersistenceStrategy}.
62 | *
63 | * @param dataCollection collection of {@link Data} objects
64 | */
65 | void removeData(Collection dataCollection);
66 |
67 | void onInitialized();
68 | }
69 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/app_bar_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
25 |
26 |
33 |
34 |
38 |
39 |
45 |
46 |
47 |
48 |
49 |
50 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/batching/src/main/java/com/flipkart/batching/BatchController.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.flipkart.batching;
26 |
27 | import android.os.Handler;
28 |
29 | import com.flipkart.batching.core.Batch;
30 | import com.flipkart.batching.core.Data;
31 | import com.flipkart.batching.core.SerializationStrategy;
32 |
33 | import java.util.Collection;
34 |
35 | /**
36 | * Interface class for BatchController. An implementation of BatchController must
37 | * implement this interface and override all it's methods.
38 | */
39 |
40 | public interface BatchController> {
41 | /**
42 | * This method takes {@link Data} type {@link Collection} as parameter and notifies the provided
43 | * {@link BatchingStrategy} about the added data.
44 | *
45 | * @param dataCollection collection of {@link Data}
46 | */
47 | void addToBatch(Collection dataCollection);
48 |
49 | /**
50 | * This method takes {@link Data} type {@link Collection} and a boolean as parameter and notifies the provided
51 | * {@link BatchingStrategy} about the added data.
52 | *
53 | * @param dataCollection collection of {@link Data}
54 | * @param forced whether to forcefully trigger the event call
55 | */
56 | void addToBatch(Collection dataCollection, boolean forced);
57 |
58 | /**
59 | * This method returns the initialized {@link Handler}.
60 | *
61 | * @return handler
62 | */
63 |
64 | Handler getHandler();
65 |
66 | /**
67 | * This method returns the initialized {@link SerializationStrategy}.
68 | *
69 | * @return serializationStrategy
70 | */
71 |
72 | SerializationStrategy getSerializationStrategy();
73 |
74 | void flush(boolean forced);
75 | }
76 |
--------------------------------------------------------------------------------
/batching/src/main/java/com/flipkart/batching/tape/InMemoryObjectQueue.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2012 Square, Inc.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package com.flipkart.batching.tape;
17 |
18 | import com.flipkart.batching.toolbox.LogUtil;
19 |
20 | import java.util.ArrayList;
21 | import java.util.LinkedList;
22 | import java.util.List;
23 | import java.util.Queue;
24 |
25 | /**
26 | * A queue for objects that are not serious enough to be written to disk. Objects in this queue
27 | * are kept in memory and will not be serialized.
28 | *
29 | * @param The type of elements in the queue.
30 | */
31 | public class InMemoryObjectQueue implements ObjectQueue {
32 | private static final String TAG = "InMemoryObjectQueue";
33 | private final Queue tasks;
34 | private Listener listener;
35 |
36 | @SuppressWarnings("unchecked")
37 | public InMemoryObjectQueue() {
38 | tasks = (Queue) new LinkedList();
39 | }
40 |
41 | @Override
42 | public void add(T entry) {
43 | tasks.add(entry);
44 | if (listener != null) listener.onAdd(this, entry);
45 | }
46 |
47 | @Override
48 | public T peek() {
49 | return tasks.peek();
50 | }
51 |
52 | @Override
53 | public int size() {
54 | return tasks.size();
55 | }
56 |
57 | @Override
58 | public void remove() {
59 | removeFromQueue();
60 | if (listener != null) {
61 | listener.onRemove(this);
62 | }
63 | }
64 |
65 | @Override
66 | public void remove(int n) {
67 | for (int i = 0; i < n; i++) {
68 | remove();
69 | }
70 | }
71 |
72 | private void removeFromQueue() {
73 | if (tasks.isEmpty()) {
74 | LogUtil.log(TAG, "The queue is empty");
75 | return;
76 | }
77 |
78 | tasks.remove();
79 | }
80 |
81 | @Override
82 | public void close() {
83 | tasks.clear();
84 | }
85 |
86 | @Override
87 | public void setListener(Listener listener) {
88 | if (listener != null) {
89 | for (T task : tasks) {
90 | listener.onAdd(this, task);
91 | }
92 | }
93 | this.listener = listener;
94 | }
95 |
96 | @Override
97 | public List peek(final int max) {
98 | return new ArrayList<>(tasks);
99 | }
100 | }
--------------------------------------------------------------------------------
/batching/build.gradle:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | apply plugin: 'com.android.library'
26 | apply plugin: 'com.github.dcendents.android-maven'
27 | apply plugin: "net.ltgt.errorprone"
28 |
29 | android {
30 | compileSdkVersion 28
31 | buildToolsVersion '28.0.3'
32 |
33 | lintOptions {
34 | abortOnError false
35 | }
36 |
37 | defaultConfig {
38 | minSdkVersion 14
39 | targetSdkVersion 28
40 | versionCode 1
41 | versionName "1.3.12"
42 | }
43 |
44 | buildTypes {
45 | release {
46 | minifyEnabled false
47 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
48 | }
49 | }
50 |
51 | testOptions {
52 | unitTests.returnDefaultValues = true
53 | }
54 | }
55 |
56 | dependencies {
57 | api fileTree(include: ['*.jar'], dir: 'libs')
58 | testImplementation 'junit:junit:4.12'
59 | testImplementation 'org.mockito:mockito-core:2.19.1'
60 | testImplementation 'org.robolectric:robolectric:3.3.2'
61 | testImplementation 'com.github.flipkart-incubator.batchman:batching-gson:1.3.9'
62 | testImplementation 'com.google.code.gson:gson:2.8.5'
63 |
64 | implementation 'com.github.flipkart-incubator.batchman:batching-core:1.3.9'
65 | implementation 'androidx.annotation:annotation:1.0.2'
66 | errorprone 'com.google.errorprone:error_prone_core:2.3.3'
67 | }
68 |
69 | buildscript {
70 | repositories {
71 | maven { url 'https://repo1.maven.org/maven2' }
72 | maven { url "https://plugins.gradle.org/m2/" }
73 | google()
74 | }
75 | dependencies {
76 | classpath 'com.android.tools.build:gradle:3.4.1'
77 | classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:4.7.5'
78 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
79 | classpath "net.ltgt.gradle:gradle-errorprone-plugin:0.0.12"
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/batching-gson/src/main/java/com/flipkart/batching/gson/adapters/data/EventDataTypeAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 | package com.flipkart.batching.gson.adapters.data;
25 |
26 | import com.flipkart.batching.core.data.EventData;
27 | import com.flipkart.batching.gson.adapters.BatchingTypeAdapters;
28 | import com.google.gson.TypeAdapter;
29 | import com.google.gson.stream.JsonReader;
30 | import com.google.gson.stream.JsonWriter;
31 |
32 | import java.io.IOException;
33 |
34 | public final class EventDataTypeAdapter extends TypeAdapter {
35 |
36 | @Override
37 | public void write(JsonWriter writer, EventData object) throws IOException {
38 | writer.beginObject();
39 | if (object == null) {
40 | writer.endObject();
41 | return;
42 | }
43 |
44 | writer.name("eventId");
45 | writer.value(object.getEventId());
46 |
47 | writer.endObject();
48 | }
49 |
50 | @Override
51 | public EventData read(JsonReader reader) throws IOException {
52 | if (reader.peek() == com.google.gson.stream.JsonToken.NULL) {
53 | reader.nextNull();
54 | return null;
55 | }
56 | if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) {
57 | reader.skipValue();
58 | return null;
59 | }
60 | reader.beginObject();
61 |
62 | EventData object = new EventData();
63 | while (reader.hasNext()) {
64 | String name = reader.nextName();
65 | com.google.gson.stream.JsonToken jsonToken = reader.peek();
66 | if (jsonToken == com.google.gson.stream.JsonToken.NULL) {
67 | reader.skipValue();
68 | continue;
69 | }
70 | switch (name) {
71 | case "eventId":
72 | object.setEventId(BatchingTypeAdapters.LONG.read(reader));
73 | break;
74 | default:
75 | reader.skipValue();
76 | break;
77 | }
78 | }
79 |
80 | reader.endObject();
81 | return object;
82 | }
83 | }
--------------------------------------------------------------------------------
/batching-gson/src/main/java/com/flipkart/batching/gson/adapters/data/TagTypeAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 | package com.flipkart.batching.gson.adapters.data;
25 |
26 | import com.flipkart.batching.core.data.Tag;
27 | import com.google.gson.TypeAdapter;
28 | import com.google.gson.stream.JsonReader;
29 | import com.google.gson.stream.JsonWriter;
30 |
31 | import java.io.IOException;
32 |
33 | public final class TagTypeAdapter extends TypeAdapter {
34 | public TagTypeAdapter() {
35 | }
36 |
37 | @Override
38 | public void write(JsonWriter writer, Tag object) throws IOException {
39 | writer.beginObject();
40 | if (object == null) {
41 | writer.endObject();
42 | return;
43 | }
44 |
45 | if (object.getId() != null) {
46 | writer.name("id");
47 | com.google.gson.internal.bind.TypeAdapters.STRING.write(writer, object.getId());
48 | }
49 |
50 | writer.endObject();
51 | }
52 |
53 | @Override
54 | public Tag read(JsonReader reader) throws IOException {
55 | if (reader.peek() == com.google.gson.stream.JsonToken.NULL) {
56 | reader.nextNull();
57 | return null;
58 | }
59 | if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) {
60 | reader.skipValue();
61 | return null;
62 | }
63 | reader.beginObject();
64 |
65 | String id = null;
66 | while (reader.hasNext()) {
67 | String name = reader.nextName();
68 | com.google.gson.stream.JsonToken jsonToken = reader.peek();
69 | if (jsonToken == com.google.gson.stream.JsonToken.NULL) {
70 | reader.skipValue();
71 | continue;
72 | }
73 | switch (name) {
74 | case "id":
75 | id = com.google.gson.internal.bind.TypeAdapters.STRING.read(reader);
76 | break;
77 | default:
78 | reader.skipValue();
79 | break;
80 | }
81 | }
82 |
83 | reader.endObject();
84 | return id == null ? null : new Tag(id);
85 | }
86 | }
--------------------------------------------------------------------------------
/batching/src/main/java/com/flipkart/batching/BatchingStrategy.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.flipkart.batching;
26 |
27 | import android.content.Context;
28 | import android.os.Handler;
29 |
30 | import com.flipkart.batching.core.Batch;
31 | import com.flipkart.batching.core.Data;
32 |
33 | import java.util.Collection;
34 |
35 | /**
36 | * Interface class for BatchingStrategy. An implementation of BatchingStrategy must
37 | * implement this interface and override all it's methods.
38 | */
39 |
40 | public interface BatchingStrategy> {
41 | /**
42 | * This method tells the BatchingStrategy about added data. This method should send the
43 | * provided {@link Collection} of {@link Data} objects to the provided implementation of
44 | * {@link com.flipkart.batching.persistence.PersistenceStrategy}.
45 | *
46 | * @param dataCollection collection of {@link Data} objects
47 | */
48 |
49 | void onDataPushed(Collection dataCollection);
50 |
51 | /**
52 | * This method fires the {@link OnBatchReadyListener} when a batch is ready, depending on the
53 | * provided BatchingStrategy.
54 | *
55 | * @param forced boolean type if isForced
56 | */
57 |
58 | void flush(boolean forced);
59 |
60 | /**
61 | * This method returns false if {@link Context}, {@link BatchController}, {@link OnBatchReadyListener}
62 | * and {@link Handler} are not initialized and true if initialized. Typically, onInitialized should
63 | * be called only once and the value of isInitialized must be set to true after initializing everything.
64 | *
65 | * @return boolean, true if initialized and false if not
66 | */
67 |
68 | boolean isInitialized();
69 |
70 | /**
71 | * Instance of {@link BatchController}, {@link Context}, {@link OnBatchReadyListener},
72 | * and {@link Handler} from {@link BatchController}.
73 | *
74 | * @param context context
75 | * @param onBatchReadyListener instance of {@link OnBatchReadyListener}
76 | * @param handler instance of {@link Handler}
77 | */
78 |
79 | void onInitialized(Context context, OnBatchReadyListener onBatchReadyListener, Handler handler);
80 | }
81 |
--------------------------------------------------------------------------------
/batching/src/test/java/com/flipkart/batching/BaseTestClass.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.flipkart.batching;
26 |
27 | import org.junit.After;
28 |
29 | import java.io.File;
30 |
31 | /**
32 | * Base Test Class, contains methods used by other tests
33 | */
34 | public class BaseTestClass {
35 | private static final String TEST_FILE_SUFFIX = ".tmp";
36 | private static final String TEST_FILE_DIR = "test_files";
37 |
38 | /**
39 | * Create a random string
40 | *
41 | * @return string
42 | */
43 | public String createRandomString() {
44 | double random = Math.random();
45 | return "Random" + random + TEST_FILE_SUFFIX;
46 | }
47 |
48 | /**
49 | * Create a file with random name
50 | *
51 | * @return file
52 | */
53 | public File createRandomFile() {
54 | String randomFile = createRandomString() + TEST_FILE_SUFFIX;
55 | return createTestFile(randomFile);
56 | }
57 |
58 | public File createTestFile(String fileName) {
59 | File folder = new File(TEST_FILE_DIR);
60 | folder.mkdirs();
61 | File file = new File(TEST_FILE_DIR, fileName);
62 | if (file.isDirectory()) {
63 | file.delete();
64 | }
65 | return file;
66 | }
67 |
68 | /**
69 | * Delete test files
70 | */
71 | public void deleteRandomFiles() {
72 | File folder = new File(TEST_FILE_DIR);
73 | File fList[] = folder.listFiles();
74 | if (fList != null) {
75 | for (File testFile : fList) {
76 | if (testFile.getName().endsWith(TEST_FILE_SUFFIX)) {
77 | testFile.delete();
78 | }
79 | }
80 | }
81 | folder.delete();
82 |
83 | folder = new File("");
84 | fList = folder.listFiles();
85 | if (fList != null) {
86 | for (File testFile : fList) {
87 | if (testFile.getName().endsWith(TEST_FILE_SUFFIX)) {
88 | testFile.delete();
89 | }
90 | }
91 | }
92 | }
93 |
94 | @After
95 | public void tearDownBase() {
96 | deleteRandomFiles();
97 | }
98 |
99 | }
100 |
--------------------------------------------------------------------------------
/batching-gson/src/main/java/com/flipkart/batching/gson/adapters/BatchImplTypeAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 | package com.flipkart.batching.gson.adapters;
25 |
26 | import com.flipkart.batching.core.BatchImpl;
27 | import com.flipkart.batching.core.Data;
28 | import com.flipkart.batching.core.DataCollection;
29 | import com.google.gson.TypeAdapter;
30 | import com.google.gson.stream.JsonReader;
31 | import com.google.gson.stream.JsonWriter;
32 |
33 | import java.io.IOException;
34 |
35 | public final class BatchImplTypeAdapter extends TypeAdapter> {
36 | private final TypeAdapter> typeAdapter;
37 |
38 | public BatchImplTypeAdapter(TypeAdapter typeAdapter) {
39 | this.typeAdapter = new DataCollectionTypeAdapter<>(typeAdapter);
40 | }
41 |
42 | @Override
43 | public void write(JsonWriter writer, BatchImpl object) throws IOException {
44 | writer.beginObject();
45 | if (object == null) {
46 | writer.endObject();
47 | return;
48 | }
49 |
50 | if (object.dataCollection != null) {
51 | writer.name("dataCollection");
52 | typeAdapter.write(writer, object.dataCollection);
53 | }
54 |
55 | writer.endObject();
56 | }
57 |
58 | @Override
59 | public BatchImpl read(JsonReader reader) throws IOException {
60 | if (reader.peek() == com.google.gson.stream.JsonToken.NULL) {
61 | reader.nextNull();
62 | return null;
63 | }
64 | if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) {
65 | reader.skipValue();
66 | return null;
67 | }
68 | reader.beginObject();
69 |
70 | DataCollection dataCollection = null;
71 | while (reader.hasNext()) {
72 | String name = reader.nextName();
73 | com.google.gson.stream.JsonToken jsonToken = reader.peek();
74 | if (jsonToken == com.google.gson.stream.JsonToken.NULL) {
75 | reader.skipValue();
76 | continue;
77 | }
78 | switch (name) {
79 | case "dataCollection":
80 | dataCollection = typeAdapter.read(reader);
81 | break;
82 | default:
83 | reader.skipValue();
84 | break;
85 | }
86 | }
87 |
88 | reader.endObject();
89 | return dataCollection == null ? null : new BatchImpl(dataCollection.dataCollection);
90 | }
91 | }
--------------------------------------------------------------------------------
/batching/src/test/java/com/flipkart/batching/persistence/InMemoryPersistenceTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.flipkart.batching.persistence;
26 |
27 | import com.flipkart.Utils;
28 | import com.flipkart.batching.core.Data;
29 |
30 | import junit.framework.Assert;
31 |
32 | import org.junit.Test;
33 |
34 | import java.util.ArrayList;
35 | import java.util.Collection;
36 |
37 | /**
38 | * Test for {@link InMemoryPersistenceStrategy}
39 | */
40 | public class InMemoryPersistenceTest {
41 |
42 | /**
43 | * Test to verify that data is retained in InMemoryList after {@link PersistenceStrategy#add(Collection)} is called.
44 | */
45 | @Test
46 | public void testIfDataInMemory() {
47 | InMemoryPersistenceStrategy persistenceStrategy = new InMemoryPersistenceStrategy<>();
48 |
49 | ArrayList data = Utils.fakeCollection(5);
50 | persistenceStrategy.add(data);
51 |
52 | Assert.assertEquals(persistenceStrategy.getData(), data);
53 | }
54 |
55 | /**
56 | * Test to verify that duplicate data are not being added to the persistence layer
57 | */
58 | @Test
59 | public void testIfDuplicateEntryNotPresent() {
60 | InMemoryPersistenceStrategy persistenceStrategy = new InMemoryPersistenceStrategy<>();
61 |
62 | //creates unique data
63 | ArrayList data = Utils.fakeCollection(5);
64 | persistenceStrategy.add(data);
65 | //Persistence strategy should return only 1 Data, as all the data that were added were duplicate.
66 | Assert.assertEquals(persistenceStrategy.getData().size(), 5);
67 | }
68 |
69 | /**
70 | * Test to verify that {@link PersistenceStrategy#removeData(Collection)} is clearing the inMemoryList.
71 | */
72 | @Test
73 | public void testIfRemoveData() {
74 | InMemoryPersistenceStrategy persistenceStrategy = new InMemoryPersistenceStrategy<>();
75 | ArrayList data = Utils.fakeCollection(5);
76 | persistenceStrategy.add(data);
77 | persistenceStrategy.removeData(data);
78 | Assert.assertTrue(persistenceStrategy.getData().size() == 0);
79 | }
80 |
81 | /**
82 | * Test to verify {@link InMemoryPersistenceStrategy#isInitialized()}
83 | */
84 | @Test
85 | public void testOnInitialized() {
86 | InMemoryPersistenceStrategy persistenceStrategy = new InMemoryPersistenceStrategy<>();
87 | persistenceStrategy.onInitialized();
88 | Assert.assertTrue(persistenceStrategy.isInitialized());
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/batching/src/main/java/com/flipkart/batching/persistence/InMemoryPersistenceStrategy.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.flipkart.batching.persistence;
26 |
27 | import com.flipkart.batching.core.Data;
28 |
29 | import java.util.ArrayList;
30 | import java.util.Collection;
31 |
32 | /**
33 | * A simple implementation of {@link PersistenceStrategy}.
34 | * This strategy persist data in an InMemory using {@link ArrayList}.
35 | */
36 | public class InMemoryPersistenceStrategy implements PersistenceStrategy {
37 | protected ArrayList dataList = new ArrayList<>();
38 | private boolean initialized;
39 |
40 | /**
41 | * Returns the initialization status.
42 | *
43 | * @return true if initialized
44 | */
45 | public boolean isInitialized() {
46 | return initialized;
47 | }
48 |
49 | /**
50 | * Adds the collection of data to InMemory list.
51 | *
52 | * @param dataCollection collection of {@link Data} objects
53 | * @return true if list is edited
54 | */
55 | @Override
56 | public boolean add(Collection dataCollection) {
57 | boolean isAdded = false;
58 | for (E data : dataCollection) {
59 | if (null != data) {
60 | dataList.add(data);
61 | isAdded = true;
62 | }
63 | }
64 | return isAdded;
65 | }
66 |
67 | /**
68 | * Adds the provided data object to InMemory List
69 | *
70 | * @param data data object
71 | */
72 |
73 | public void add(E data) {
74 | dataList.add(data);
75 | }
76 |
77 | /**
78 | * This method returns data stored in InMemory data list
79 | *
80 | * @return collection of data objects present InMemory
81 | */
82 |
83 | @Override
84 | public Collection getData() {
85 | return new ArrayList<>(dataList);
86 | }
87 |
88 | @Override
89 | public int getDataSize() {
90 | return dataList.size();
91 | }
92 |
93 | /**
94 | * Removes provided collection of data from InMemory list.
95 | *
96 | * @param dataCollection collection of {@link Data} objects
97 | */
98 |
99 | @Override
100 | public void removeData(Collection dataCollection) {
101 | dataList.removeAll(dataCollection);
102 | }
103 |
104 | /**
105 | * Sets initialized to true.
106 | */
107 |
108 | @Override
109 | public void onInitialized() {
110 | initialized = true;
111 | }
112 | }
113 |
--------------------------------------------------------------------------------
/batching/src/main/java/com/flipkart/batching/persistence/TagBasedPersistenceStrategy.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.flipkart.batching.persistence;
26 |
27 | import com.flipkart.batching.core.Data;
28 | import com.flipkart.batching.core.data.Tag;
29 | import com.flipkart.batching.core.data.TagData;
30 |
31 | import java.util.Collection;
32 | import java.util.Iterator;
33 |
34 | /**
35 | * Tag Based Persistence Strategy is an implementation of {@link PersistenceStrategy}.
36 | * This strategy links the provide {@link Tag} with provided {@link PersistenceStrategy} and
37 | * persist {@link Data} objects depending on there {@link Tag}.
38 | */
39 | public class TagBasedPersistenceStrategy implements PersistenceStrategy {
40 | private final PersistenceStrategy persistenceStrategy;
41 | private final Tag tag;
42 |
43 | public TagBasedPersistenceStrategy(Tag tag, PersistenceStrategy persistenceStrategy) {
44 | if (tag == null) {
45 | throw new IllegalArgumentException("Tag cannot be null");
46 | }
47 | if (persistenceStrategy == null) {
48 | throw new IllegalArgumentException("PersistenceStrategy cannot be null");
49 | }
50 | this.tag = tag;
51 | this.persistenceStrategy = persistenceStrategy;
52 | }
53 |
54 | @Override
55 | public boolean add(Collection dataCollection) {
56 | filterByTag(dataCollection);
57 | return persistenceStrategy.add(dataCollection);
58 | }
59 |
60 | @Override
61 | public Collection getData() {
62 | Collection allData = persistenceStrategy.getData();
63 | filterByTag(allData);
64 | return allData;
65 | }
66 |
67 | @Override
68 | public int getDataSize() {
69 | return persistenceStrategy.getDataSize();
70 | }
71 |
72 | @Override
73 | public void removeData(Collection dataCollection) {
74 | filterByTag(dataCollection);
75 | persistenceStrategy.removeData(dataCollection);
76 | }
77 |
78 | @Override
79 | public void onInitialized() {
80 | persistenceStrategy.onInitialized();
81 | }
82 |
83 | /**
84 | * This method filters the provided collection of {@link Data} objects by {@link Tag}.
85 | *
86 | * @param allData collection of {@link Data} objects.
87 | */
88 | private void filterByTag(Collection allData) {
89 | Iterator iterator = allData.iterator();
90 | while (iterator.hasNext()) {
91 | TagData data = (TagData) iterator.next();
92 | if (null == data || !tag.equals(data.getTag())) {
93 | iterator.remove();
94 | }
95 | }
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/batching/src/main/java/com/flipkart/batching/strategy/SizeBatchingStrategy.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.flipkart.batching.strategy;
26 |
27 | import android.content.Context;
28 | import android.os.Handler;
29 |
30 | import com.flipkart.batching.BatchingStrategy;
31 | import com.flipkart.batching.OnBatchReadyListener;
32 | import com.flipkart.batching.core.Data;
33 | import com.flipkart.batching.core.batch.SizeBatch;
34 | import com.flipkart.batching.persistence.PersistenceStrategy;
35 |
36 | import java.util.Collection;
37 |
38 | /**
39 | * SizeBatchingStrategy extends abstract class {@link BaseBatchingStrategy} which is an
40 | * implementation of {@link BatchingStrategy}. This class takes maxBatchSize and persistenceStrategy
41 | * as parameters in constructor. This strategy persist data according to the provided
42 | * {@link PersistenceStrategy} and calls {@link #onReadyListener} when the batch reaches the
43 | * maxBatchSize limit.
44 | */
45 | public class SizeBatchingStrategy extends BaseBatchingStrategy> {
46 | private int currentBatchSize;
47 | private int maxBatchSize;
48 |
49 | public SizeBatchingStrategy(int maxBatchSize, PersistenceStrategy persistenceStrategy) {
50 | super(persistenceStrategy);
51 | currentBatchSize = 0;
52 | if (maxBatchSize <= 0) {
53 | throw new IllegalArgumentException("Max. Batch Size should be greater than 0");
54 | } else {
55 | this.maxBatchSize = maxBatchSize;
56 | }
57 | }
58 |
59 |
60 | @Override
61 | public void onDataPushed(Collection dataCollection) {
62 | super.onDataPushed(dataCollection);
63 | currentBatchSize = getPersistenceStrategy().getDataSize();
64 | }
65 |
66 | @Override
67 | public void flush(boolean forced) {
68 | currentBatchSize = getPersistenceStrategy().getDataSize();
69 | if ((forced || isBatchReady()) && currentBatchSize > 0) {
70 | Collection data = getPersistenceStrategy().getData();
71 | getPersistenceStrategy().removeData(data);
72 | getOnReadyListener().onReady(this, new SizeBatch(data, maxBatchSize));
73 | }
74 | }
75 |
76 | @Override
77 | public void onInitialized(Context context, OnBatchReadyListener> onBatchReadyListener, Handler handler) {
78 | super.onInitialized(context, onBatchReadyListener, handler);
79 | }
80 |
81 | /**
82 | * Returns true if currentBatch reaches the defined maxBatchSize.
83 | *
84 | * @return boolean type batch ready state
85 | */
86 | protected boolean isBatchReady() {
87 | return currentBatchSize >= maxBatchSize;
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/batching/src/main/java/com/flipkart/batching/strategy/BaseBatchingStrategy.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.flipkart.batching.strategy;
26 |
27 | import android.content.Context;
28 | import android.os.Handler;
29 |
30 | import com.flipkart.batching.BatchController;
31 | import com.flipkart.batching.BatchingStrategy;
32 | import com.flipkart.batching.OnBatchReadyListener;
33 | import com.flipkart.batching.core.Batch;
34 | import com.flipkart.batching.core.Data;
35 | import com.flipkart.batching.persistence.PersistenceStrategy;
36 |
37 | import java.util.Collection;
38 |
39 | /**
40 | * This abstract class implements {@link BatchingStrategy} interface. BaseBatchingStrategy
41 | * hold an instance of provided {@link PersistenceStrategy}, {@link OnBatchReadyListener},
42 | * {@link BatchController} and {@link Context}.
43 | *
44 | * A class extending BaseBatchingStrategy must call super from it's constructor,
45 | * {@link #onDataPushed(Collection)} and {@link BatchingStrategy#onInitialized(Context, OnBatchReadyListener, Handler)} methods.
46 | */
47 | public abstract class BaseBatchingStrategy> implements BatchingStrategy {
48 | private Context context;
49 | private OnBatchReadyListener onReadyListener;
50 | private PersistenceStrategy persistenceStrategy;
51 | private boolean initialized = false;
52 |
53 | public BaseBatchingStrategy(PersistenceStrategy persistenceStrategy) {
54 | if (persistenceStrategy != null) {
55 | this.persistenceStrategy = persistenceStrategy;
56 | } else {
57 | throw new IllegalArgumentException("Persistence Strategy cannot be null.");
58 | }
59 | }
60 |
61 | @Override
62 | public boolean isInitialized() {
63 | return initialized;
64 | }
65 |
66 | @Override
67 | public void onDataPushed(Collection dataCollection) {
68 | persistenceStrategy.add(dataCollection);
69 | }
70 |
71 | @Override
72 | public abstract void flush(boolean forced);
73 |
74 | @Override
75 | public void onInitialized(Context context,
76 | OnBatchReadyListener onBatchReadyListener, Handler handler) {
77 | this.initialized = true;
78 | this.onReadyListener = onBatchReadyListener;
79 | this.context = context;
80 | this.persistenceStrategy.onInitialized();
81 | }
82 |
83 | public Context getContext() {
84 | return context;
85 | }
86 |
87 | public OnBatchReadyListener getOnReadyListener() {
88 | return onReadyListener;
89 | }
90 |
91 | public PersistenceStrategy getPersistenceStrategy() {
92 | return persistenceStrategy;
93 | }
94 | }
--------------------------------------------------------------------------------
/batching/src/main/java/com/flipkart/batching/listener/TrimPersistedBatchReadyListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.flipkart.batching.listener;
26 |
27 | import android.os.Handler;
28 |
29 | import com.flipkart.batching.BatchingStrategy;
30 | import com.flipkart.batching.core.Batch;
31 | import com.flipkart.batching.core.Data;
32 | import com.flipkart.batching.core.SerializationStrategy;
33 |
34 |
35 | /**
36 | * TrimPersistedBatchReadyListener that extends {@link PersistedBatchReadyListener}
37 | */
38 | public class TrimPersistedBatchReadyListener> extends PersistedBatchReadyListener {
39 | public final static int MODE_TRIM_NONE = 0;
40 | public final static int MODE_TRIM_AT_START = 1;
41 | public final static int MODE_TRIM_ON_READY = 1 << 1;
42 | protected final Handler handler;
43 | private final TrimmedBatchCallback trimListener;
44 | int mode;
45 | private int trimSize, maxQueueSize;
46 |
47 | public TrimPersistedBatchReadyListener(String filePath, SerializationStrategy serializationStrategy, Handler handler, int maxQueueSize, int trimSize, int mode, PersistedBatchCallback persistedBatchCallback, TrimmedBatchCallback trimmedBatchCallback) {
48 | super(filePath, serializationStrategy, handler, persistedBatchCallback);
49 | if (trimSize > maxQueueSize) {
50 | throw new IllegalArgumentException("trimSize must be smaller than maxQueueSize");
51 | }
52 | this.trimSize = trimSize;
53 | this.maxQueueSize = maxQueueSize;
54 | this.handler = handler;
55 | this.mode = mode;
56 | this.trimListener = trimmedBatchCallback;
57 | }
58 |
59 | @Override
60 | protected void onInitialized() {
61 | super.onInitialized();
62 | if ((mode & MODE_TRIM_AT_START) == MODE_TRIM_AT_START) {
63 | trimQueue();
64 | }
65 | }
66 |
67 | @Override
68 | public void onReady(BatchingStrategy causingStrategy, T batch) {
69 | super.onReady(causingStrategy, batch);
70 | handler.post(new Runnable() {
71 | @Override
72 | public void run() {
73 | if ((mode & MODE_TRIM_ON_READY) == MODE_TRIM_ON_READY) {
74 | trimQueue();
75 | }
76 | }
77 | });
78 | }
79 |
80 | void trimQueue() {
81 | int oldSize = getSize();
82 | if (oldSize >= maxQueueSize && remove(trimSize)) {
83 | callTrimListener(oldSize, getSize());
84 | }
85 | }
86 |
87 | private void callTrimListener(int oldSize, int newSize) {
88 | if (trimListener != null) {
89 | trimListener.onTrimmed(oldSize, newSize);
90 | }
91 | }
92 | }
93 |
--------------------------------------------------------------------------------
/batching-gson/src/main/java/com/flipkart/batching/gson/adapters/data/TagDataTypeAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 | package com.flipkart.batching.gson.adapters.data;
25 |
26 | import com.flipkart.batching.core.data.Tag;
27 | import com.flipkart.batching.core.data.TagData;
28 | import com.flipkart.batching.gson.adapters.BatchingTypeAdapters;
29 | import com.google.gson.TypeAdapter;
30 | import com.google.gson.stream.JsonReader;
31 | import com.google.gson.stream.JsonWriter;
32 |
33 | import java.io.IOException;
34 |
35 | public final class TagDataTypeAdapter extends TypeAdapter {
36 |
37 | private TypeAdapter tagTypeAdapter;
38 |
39 | public TagDataTypeAdapter() {
40 | this.tagTypeAdapter = new TagTypeAdapter();
41 | }
42 |
43 | @Override
44 | public void write(JsonWriter writer, TagData object) throws IOException {
45 | writer.beginObject();
46 | if (object == null) {
47 | writer.endObject();
48 | return;
49 | }
50 |
51 | if (object.getTag() != null) {
52 | writer.name("tag");
53 | tagTypeAdapter.write(writer, object.getTag());
54 | }
55 |
56 | writer.name("eventId");
57 | writer.value(object.getEventId());
58 |
59 | writer.endObject();
60 | }
61 |
62 | @Override
63 | public TagData read(JsonReader reader) throws IOException {
64 | if (reader.peek() == com.google.gson.stream.JsonToken.NULL) {
65 | reader.nextNull();
66 | return null;
67 | }
68 | if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) {
69 | reader.skipValue();
70 | return null;
71 | }
72 | reader.beginObject();
73 |
74 | Tag tag = null;
75 | long eventId = 0L;
76 | while (reader.hasNext()) {
77 | String name = reader.nextName();
78 | com.google.gson.stream.JsonToken jsonToken = reader.peek();
79 | if (jsonToken == com.google.gson.stream.JsonToken.NULL) {
80 | reader.skipValue();
81 | continue;
82 | }
83 | switch (name) {
84 | case "tag":
85 | tag = tagTypeAdapter.read(reader);
86 | break;
87 | case "eventId":
88 | eventId = BatchingTypeAdapters.LONG.read(reader);
89 | break;
90 | default:
91 | reader.skipValue();
92 | break;
93 | }
94 | }
95 |
96 | reader.endObject();
97 | TagData tagData = new TagData(tag);
98 | tagData.setEventId(eventId);
99 | return tagData;
100 | }
101 | }
--------------------------------------------------------------------------------
/batching/src/main/java/com/flipkart/batching/persistence/SQLPersistenceStrategy.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.flipkart.batching.persistence;
26 |
27 | import android.content.Context;
28 |
29 | import com.flipkart.batching.core.Batch;
30 | import com.flipkart.batching.core.Data;
31 | import com.flipkart.batching.core.SerializationStrategy;
32 | import com.flipkart.batching.toolbox.LogUtil;
33 |
34 | import java.io.IOException;
35 | import java.util.Collection;
36 |
37 | /**
38 | * SQLPersistenceStrategy extends {@link InMemoryPersistenceStrategy} which is an implementation
39 | * of {@link PersistenceStrategy}. This persistence strategy persists the data in SQL Database and
40 | * sync the data on initialization of this strategy using the {@link #syncData()} method. Constructor
41 | * and all the overridden methods must call super method, to initialize and perform operations on
42 | * InMemory data list.
43 | */
44 | public class SQLPersistenceStrategy extends InMemoryPersistenceStrategy {
45 | private static final String TAG = "SQLPersistenceStrategy";
46 | private DatabaseHelper databaseHelper;
47 | private SerializationStrategy serializationStrategy;
48 | private String databaseName;
49 | private Context context;
50 |
51 | public SQLPersistenceStrategy(SerializationStrategy serializationStrategy, String databaseName, Context context) {
52 | super();
53 | this.serializationStrategy = serializationStrategy;
54 | this.databaseName = databaseName;
55 | this.context = context;
56 | }
57 |
58 | @Override
59 | public boolean add(final Collection dataCollection) {
60 | super.add(dataCollection);
61 | try {
62 | databaseHelper.addData(dataCollection);
63 | } catch (IOException e) {
64 | LogUtil.log(TAG, e.getLocalizedMessage());
65 | }
66 | return true;
67 | }
68 |
69 | /**
70 | * This method is called from constructor, when instance of {@link SQLPersistenceStrategy} is
71 | * initialized. The InMemory data list is updated with the persisted {@link Data} objects
72 | * which were not batched before.
73 | */
74 | private void syncData() {
75 | try {
76 | super.add(databaseHelper.getAllData());
77 | } catch (IOException e) {
78 | LogUtil.log(TAG, e.getLocalizedMessage());
79 | }
80 | }
81 |
82 | @Override
83 | public void removeData(final Collection dataCollection) {
84 | super.removeData(dataCollection);
85 | databaseHelper.deleteAll();
86 | }
87 |
88 | @Override
89 | public void onInitialized() {
90 | if (!isInitialized()) {
91 | this.databaseHelper = new DatabaseHelper<>(serializationStrategy, databaseName, context);
92 | syncData();
93 | }
94 | super.onInitialized();
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/batching-gson/src/main/java/com/flipkart/batching/gson/adapters/DataCollectionTypeAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 | package com.flipkart.batching.gson.adapters;
25 |
26 | import androidx.annotation.NonNull;
27 |
28 | import com.flipkart.batching.core.Data;
29 | import com.flipkart.batching.core.DataCollection;
30 | import com.google.gson.TypeAdapter;
31 | import com.google.gson.internal.ObjectConstructor;
32 | import com.google.gson.stream.JsonReader;
33 | import com.google.gson.stream.JsonWriter;
34 |
35 | import java.io.IOException;
36 | import java.util.ArrayList;
37 | import java.util.Collection;
38 |
39 | public class DataCollectionTypeAdapter extends TypeAdapter> {
40 |
41 | @NonNull
42 | private TypeAdapter> collectionTypeAdapter;
43 |
44 | public DataCollectionTypeAdapter(@NonNull TypeAdapter typeAdapter) {
45 | collectionTypeAdapter = new BatchingTypeAdapters.ListTypeAdapter<>(typeAdapter, new ObjectConstructor>() {
46 | @Override
47 | public Collection construct() {
48 | return new ArrayList<>();
49 | }
50 | });
51 | }
52 |
53 | @Override
54 | public void write(JsonWriter out, DataCollection value) throws IOException {
55 | out.beginObject();
56 | if (value == null) {
57 | out.endObject();
58 | return;
59 | }
60 |
61 | if (value.dataCollection != null) {
62 | out.name("dataCollection");
63 | collectionTypeAdapter.write(out, value.dataCollection);
64 | }
65 |
66 | out.endObject();
67 | }
68 |
69 | @Override
70 | public DataCollection read(JsonReader reader) throws IOException {
71 | if (reader.peek() == com.google.gson.stream.JsonToken.NULL) {
72 | reader.nextNull();
73 | return null;
74 | }
75 | if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) {
76 | reader.skipValue();
77 | return null;
78 | }
79 | reader.beginObject();
80 |
81 | Collection collection = null;
82 | while (reader.hasNext()) {
83 | String name = reader.nextName();
84 | com.google.gson.stream.JsonToken jsonToken = reader.peek();
85 | if (jsonToken == com.google.gson.stream.JsonToken.NULL) {
86 | reader.skipValue();
87 | continue;
88 | }
89 | switch (name) {
90 | case "dataCollection":
91 | collection = collectionTypeAdapter.read(reader);
92 | break;
93 | default:
94 | reader.skipValue();
95 | break;
96 | }
97 | }
98 |
99 | reader.endObject();
100 | return null != collection ? new DataCollection<>(collection) : null;
101 | }
102 | }
--------------------------------------------------------------------------------
/batching-gson/src/main/java/com/flipkart/batching/gson/adapters/batch/TimeBatchTypeAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 | package com.flipkart.batching.gson.adapters.batch;
25 |
26 | import com.flipkart.batching.core.Data;
27 | import com.flipkart.batching.core.DataCollection;
28 | import com.flipkart.batching.core.batch.TimeBatch;
29 | import com.flipkart.batching.gson.adapters.BatchingTypeAdapters;
30 | import com.flipkart.batching.gson.adapters.DataCollectionTypeAdapter;
31 | import com.google.gson.TypeAdapter;
32 | import com.google.gson.stream.JsonReader;
33 | import com.google.gson.stream.JsonWriter;
34 |
35 | import java.io.IOException;
36 |
37 | public final class TimeBatchTypeAdapter extends TypeAdapter> {
38 | private final TypeAdapter> typeAdapter;
39 |
40 | public TimeBatchTypeAdapter(TypeAdapter typeAdapter) {
41 | this.typeAdapter = new DataCollectionTypeAdapter(typeAdapter);
42 | }
43 |
44 | @Override
45 | public void write(JsonWriter writer, TimeBatch object) throws IOException {
46 | writer.beginObject();
47 | if (object == null) {
48 | writer.endObject();
49 | return;
50 | }
51 |
52 | writer.name("timeOut");
53 | writer.value(object.getTimeOut());
54 |
55 | if (object.dataCollection != null) {
56 | writer.name("dataCollection");
57 | typeAdapter.write(writer, object.dataCollection);
58 | }
59 |
60 | writer.endObject();
61 | }
62 |
63 | @Override
64 | public TimeBatch read(JsonReader reader) throws IOException {
65 | if (reader.peek() == com.google.gson.stream.JsonToken.NULL) {
66 | reader.nextNull();
67 | return null;
68 | }
69 | if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) {
70 | reader.skipValue();
71 | return null;
72 | }
73 | reader.beginObject();
74 |
75 | long timeOut = 0L;
76 | DataCollection dataCollection = null;
77 |
78 | while (reader.hasNext()) {
79 | String name = reader.nextName();
80 | com.google.gson.stream.JsonToken jsonToken = reader.peek();
81 | if (jsonToken == com.google.gson.stream.JsonToken.NULL) {
82 | reader.skipValue();
83 | continue;
84 | }
85 | switch (name) {
86 | case "timeOut":
87 | timeOut = BatchingTypeAdapters.LONG.read(reader);
88 | break;
89 | case "dataCollection":
90 | dataCollection = typeAdapter.read(reader);
91 | break;
92 | default:
93 | reader.skipValue();
94 | break;
95 | }
96 | }
97 |
98 | reader.endObject();
99 | return dataCollection == null ? null : new TimeBatch<>(dataCollection.dataCollection, timeOut);
100 | }
101 | }
--------------------------------------------------------------------------------
/batching-gson/src/main/java/com/flipkart/batching/gson/adapters/batch/SizeBatchTypeAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 | package com.flipkart.batching.gson.adapters.batch;
25 |
26 | import com.flipkart.batching.core.Data;
27 | import com.flipkart.batching.core.DataCollection;
28 | import com.flipkart.batching.core.batch.SizeBatch;
29 | import com.flipkart.batching.gson.adapters.BatchingTypeAdapters;
30 | import com.flipkart.batching.gson.adapters.DataCollectionTypeAdapter;
31 | import com.google.gson.TypeAdapter;
32 | import com.google.gson.stream.JsonReader;
33 | import com.google.gson.stream.JsonWriter;
34 |
35 | import java.io.IOException;
36 |
37 | public final class SizeBatchTypeAdapter extends TypeAdapter> {
38 | private final TypeAdapter> typeAdapter;
39 |
40 | public SizeBatchTypeAdapter(TypeAdapter typeAdapter) {
41 | this.typeAdapter = new DataCollectionTypeAdapter(typeAdapter);
42 | }
43 |
44 | @Override
45 | public void write(JsonWriter writer, SizeBatch object) throws IOException {
46 | writer.beginObject();
47 | if (object == null) {
48 | writer.endObject();
49 | return;
50 | }
51 |
52 | writer.name("maxBatchSize");
53 | writer.value(object.getMaxBatchSize());
54 |
55 | if (object.dataCollection != null) {
56 | writer.name("dataCollection");
57 | typeAdapter.write(writer, object.dataCollection);
58 | }
59 |
60 | writer.endObject();
61 | }
62 |
63 | @Override
64 | public SizeBatch read(JsonReader reader) throws IOException {
65 | if (reader.peek() == com.google.gson.stream.JsonToken.NULL) {
66 | reader.nextNull();
67 | return null;
68 | }
69 | if (reader.peek() != com.google.gson.stream.JsonToken.BEGIN_OBJECT) {
70 | reader.skipValue();
71 | return null;
72 | }
73 | reader.beginObject();
74 |
75 | int maxBatchSize = 0;
76 | DataCollection dataCollection = null;
77 |
78 | while (reader.hasNext()) {
79 | String name = reader.nextName();
80 | com.google.gson.stream.JsonToken jsonToken = reader.peek();
81 | if (jsonToken == com.google.gson.stream.JsonToken.NULL) {
82 | reader.skipValue();
83 | continue;
84 | }
85 |
86 | switch (name) {
87 | case "maxBatchSize":
88 | maxBatchSize = BatchingTypeAdapters.INTEGER.read(reader);
89 | break;
90 | case "dataCollection":
91 | dataCollection = typeAdapter.read(reader);
92 | break;
93 | default:
94 | reader.skipValue();
95 | break;
96 | }
97 | }
98 |
99 | reader.endObject();
100 | return dataCollection == null ? null : new SizeBatch<>(dataCollection.dataCollection, maxBatchSize);
101 | }
102 | }
--------------------------------------------------------------------------------
/batching-core/src/test/java/com/flipkart/batching_core/Utils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.flipkart.batching.core;
26 |
27 | import com.flipkart.batching.core.data.EventData;
28 | import com.flipkart.batching.core.data.Tag;
29 | import com.flipkart.batching.core.data.TagData;
30 |
31 | import java.util.ArrayList;
32 |
33 | /**
34 | * Utils class for Test
35 | */
36 | public class Utils {
37 |
38 | /**
39 | * Method to create fake array list of Data.
40 | *
41 | * @param size
42 | * @return dataList
43 | */
44 | static Data eventData;
45 |
46 | public static ArrayList fakeCollection(int size) {
47 | ArrayList dataList = new ArrayList<>();
48 | for (int i = 0; i < size; i++) {
49 | eventData = new EventData();
50 | eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i);
51 | dataList.add(eventData);
52 | }
53 | return dataList;
54 | }
55 |
56 | public static ArrayList fakeAdsCollection(int size) {
57 | ArrayList dataList = new ArrayList<>();
58 | for (int i = 0; i < size; i++) {
59 | eventData = new EventData();
60 | eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i);
61 | dataList.add(eventData);
62 | }
63 | return dataList;
64 | }
65 |
66 | public static ArrayList fakeDebugCollection(int size) {
67 | ArrayList dataList = new ArrayList<>();
68 | for (int i = 0; i < size; i++) {
69 | eventData = new EventData();
70 | eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i);
71 | dataList.add(eventData);
72 | }
73 | return dataList;
74 | }
75 |
76 | public static ArrayList fakeBuisnessCollection(int size) {
77 | ArrayList dataList = new ArrayList<>();
78 | for (int i = 0; i < size; i++) {
79 | eventData = new EventData();
80 | eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i);
81 | dataList.add(eventData);
82 | }
83 | return dataList;
84 | }
85 |
86 | public static ArrayList fakeTagAdsCollection(int size) {
87 | ArrayList dataList = new ArrayList<>();
88 | for (int i = 0; i < size; i++) {
89 | TagData eventData = new TagData(new Tag("ADS"));
90 | eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i);
91 | dataList.add(eventData);
92 | }
93 | return dataList;
94 | }
95 |
96 | public static ArrayList fakeTagDebugCollection(int size) {
97 | ArrayList dataList = new ArrayList<>();
98 | for (int i = 0; i < size; i++) {
99 | TagData eventData = new TagData(new Tag("DEBUG"));
100 | eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i);
101 | dataList.add(eventData);
102 | }
103 | return dataList;
104 | }
105 |
106 | }
107 |
--------------------------------------------------------------------------------
/batching/src/test/java/com/flipkart/Utils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.flipkart;
26 |
27 | import com.flipkart.batching.core.Data;
28 | import com.flipkart.batching.core.data.EventData;
29 | import com.flipkart.batching.core.data.Tag;
30 | import com.flipkart.batching.core.data.TagData;
31 |
32 | import java.util.ArrayList;
33 |
34 | /**
35 | * Utils class for Test
36 | */
37 | public class Utils {
38 |
39 | /**
40 | * Method to create fake array list of Data.
41 | *
42 | * @param size
43 | * @return dataList
44 | */
45 | static Data eventData;
46 |
47 | public static ArrayList fakeCollection(int size) {
48 | ArrayList dataList = new ArrayList<>();
49 | for (int i = 0; i < size; i++) {
50 | eventData = new EventData();
51 | eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i);
52 | dataList.add(eventData);
53 | }
54 | return dataList;
55 | }
56 |
57 | public static ArrayList fakeAdsCollection(int size) {
58 | ArrayList dataList = new ArrayList<>();
59 | for (int i = 0; i < size; i++) {
60 | eventData = new EventData();
61 | eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i);
62 | dataList.add(eventData);
63 | }
64 | return dataList;
65 | }
66 |
67 | public static ArrayList fakeDebugCollection(int size) {
68 | ArrayList dataList = new ArrayList<>();
69 | for (int i = 0; i < size; i++) {
70 | eventData = new EventData();
71 | eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i);
72 | dataList.add(eventData);
73 | }
74 | return dataList;
75 | }
76 |
77 | public static ArrayList fakeBuisnessCollection(int size) {
78 | ArrayList dataList = new ArrayList<>();
79 | for (int i = 0; i < size; i++) {
80 | eventData = new EventData();
81 | eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i);
82 | dataList.add(eventData);
83 | }
84 | return dataList;
85 | }
86 |
87 | public static ArrayList fakeTagAdsCollection(int size) {
88 | ArrayList dataList = new ArrayList<>();
89 | for (int i = 0; i < size; i++) {
90 | TagData eventData = new TagData(new Tag("ADS"));
91 | eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i);
92 | dataList.add(eventData);
93 | }
94 | return dataList;
95 | }
96 |
97 | public static ArrayList fakeTagDebugCollection(int size) {
98 | ArrayList dataList = new ArrayList<>();
99 | for (int i = 0; i < size; i++) {
100 | TagData eventData = new TagData(new Tag("DEBUG"));
101 | eventData.setEventId(System.currentTimeMillis() + System.nanoTime() + i);
102 | dataList.add(eventData);
103 | }
104 | return dataList;
105 | }
106 |
107 | }
108 |
--------------------------------------------------------------------------------
/batching/src/test/java/com/flipkart/batching/listener/TagBatchReadyTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * The MIT License (MIT)
3 | *
4 | * Copyright (c) 2017 Flipkart Internet Pvt. Ltd.
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"), to deal
8 | * in the Software without restriction, including without limitation the rights
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 | * copies of the Software, and to permit persons to whom the Software is
11 | * furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included in
14 | * all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 | * THE SOFTWARE.
23 | */
24 |
25 | package com.flipkart.batching.listener;
26 |
27 | import com.flipkart.Utils;
28 | import com.flipkart.batching.BatchingStrategy;
29 | import com.flipkart.batching.BuildConfig;
30 | import com.flipkart.batching.OnBatchReadyListener;
31 | import com.flipkart.batching.core.batch.SizeBatch;
32 | import com.flipkart.batching.core.batch.TagBatch;
33 | import com.flipkart.batching.core.data.Tag;
34 | import com.flipkart.batching.core.data.TagData;
35 | import com.flipkart.batching.strategy.TagBatchingStrategy;
36 |
37 | import junit.framework.Assert;
38 |
39 | import org.junit.Test;
40 | import org.junit.runner.RunWith;
41 | import org.robolectric.RobolectricTestRunner;
42 | import org.robolectric.annotation.Config;
43 |
44 | import static org.mockito.Matchers.any;
45 | import static org.mockito.Mockito.mock;
46 | import static org.mockito.Mockito.times;
47 | import static org.mockito.Mockito.verify;
48 |
49 | /**
50 | * Test for {@link TagBatchReadyListener}
51 | */
52 | @RunWith(RobolectricTestRunner.class)
53 | @Config(constants = BuildConfig.class, sdk = 21)
54 | public class TagBatchReadyTest {
55 |
56 |
57 | /**
58 | * Test to add tag listeners.
59 | */
60 | @Test
61 | public void testAddTagListener() {
62 | TagBatchReadyListener tagBatchReadyListener;
63 | Tag AD = new Tag("ADS");
64 | Tag BUSINESS = new Tag("BUSINESS");
65 | Tag DEBUG = new Tag("DEBUG");
66 |
67 | OnBatchReadyListener> onBatchReadyListener = mock(OnBatchReadyListener.class);
68 | tagBatchReadyListener = new TagBatchReadyListener<>();
69 | tagBatchReadyListener.addListenerForTag(AD, onBatchReadyListener);
70 | tagBatchReadyListener.addListenerForTag(DEBUG, onBatchReadyListener);
71 | tagBatchReadyListener.addListenerForTag(BUSINESS, onBatchReadyListener);
72 |
73 | //assert that the tagMaps are not null
74 | Assert.assertTrue(!tagBatchReadyListener.getTagOnBatchReadyListenerMap().isEmpty());
75 | }
76 |
77 |
78 | /**
79 | * Test to verify {@link TagBatchReadyListener#onReady(BatchingStrategy, TagBatch)}
80 | */
81 | @Test
82 | public void testOnReady() {
83 | TagBatchReadyListener tagBatchReadyListener;
84 | Tag AD = new Tag("ADS");
85 | Tag BUSINESS = new Tag("BUSINESS");
86 | Tag DEBUG = new Tag("DEBUG");
87 |
88 | OnBatchReadyListener> onBatchReadyListener = mock(OnBatchReadyListener.class);
89 | tagBatchReadyListener = new TagBatchReadyListener<>();
90 | tagBatchReadyListener.addListenerForTag(AD, onBatchReadyListener);
91 | tagBatchReadyListener.addListenerForTag(DEBUG, onBatchReadyListener);
92 | tagBatchReadyListener.addListenerForTag(BUSINESS, onBatchReadyListener);
93 |
94 | tagBatchReadyListener.onReady(new TagBatchingStrategy<>(), new TagBatch<>(new Tag("ADS"), new SizeBatch(Utils.fakeCollection(2), 4)));
95 | //verify that it gets called once , when tagBatchReadyListener's onReady gets called
96 | verify(onBatchReadyListener, times(1)).onReady(any(BatchingStrategy.class), any(TagBatch.class));
97 | }
98 | }
--------------------------------------------------------------------------------