-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
weiqiangliu
committed
Dec 8, 2021
1 parent
5294feb
commit 883d13d
Showing
9 changed files
with
302 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...orsABTestSDK/src/main/java/com/sensorsdata/abtest/core/RequestExperimentTaskRecorder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Created by luweibin on 2021/11/15. | ||
* Copyright 2015-2021 Sensors Data Inc. | ||
* | ||
* 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.sensorsdata.abtest.core; | ||
|
||
import android.text.TextUtils; | ||
|
||
import com.sensorsdata.abtest.OnABTestReceivedData; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
public class RequestExperimentTaskRecorder { | ||
private final String mLoginId; | ||
private final String mAnonymousId; | ||
private final String mParamName; | ||
private final Map<String, Object> mProperties; | ||
private final int mTimeoutMillSeconds; | ||
private final Map<OnABTestReceivedData<Object>, Object> mCallbacksAndDefaultValueMap = new LinkedHashMap<>(); | ||
private boolean mIsMergedTask = false; | ||
|
||
RequestExperimentTaskRecorder(String loginId, String anonymousId, String paramName, Map<String, Object> properties, int timeoutMillSeconds) { | ||
this.mLoginId = loginId; | ||
this.mAnonymousId = anonymousId; | ||
this.mParamName = paramName; | ||
this.mProperties = properties; | ||
this.mTimeoutMillSeconds = timeoutMillSeconds; | ||
} | ||
|
||
void putCallbackAndDefaultValue(OnABTestReceivedData<Object> onABTestReceivedData, Object defaultValue) { | ||
if (!this.mCallbacksAndDefaultValueMap.containsKey(onABTestReceivedData)) { | ||
this.mCallbacksAndDefaultValueMap.put(onABTestReceivedData, defaultValue); | ||
} | ||
} | ||
|
||
Map<OnABTestReceivedData<Object>, Object> getCallbacksAndDefaultValueMap() { | ||
return mCallbacksAndDefaultValueMap; | ||
} | ||
|
||
boolean isSameExperimentTask(String loginId, String anonymousId, String paramName, Map<String, Object> properties, int timeoutMillSeconds) { | ||
return TextUtils.equals(loginId, mLoginId) | ||
&& TextUtils.equals(anonymousId, mAnonymousId) | ||
&& (properties == null || TextUtils.equals(paramName, mParamName)) | ||
&& isSameProperties(properties) | ||
&& timeoutMillSeconds == mTimeoutMillSeconds; | ||
} | ||
|
||
private boolean isSameProperties(Map<String, Object> properties) { | ||
if (properties == null && mProperties == null) { | ||
return true; | ||
} else if (properties != null && mProperties != null) { | ||
if (properties.size() == mProperties.size()) { | ||
for (Map.Entry<String, Object> entry : mProperties.entrySet()) { | ||
String key = entry.getKey(); | ||
Object value = entry.getValue(); | ||
if (!properties.containsKey(key) || !value.equals(properties.get(key))) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
void setIsMergedTask(boolean isMergedTask) { | ||
this.mIsMergedTask = isMergedTask; | ||
} | ||
|
||
boolean isMergedTask() { | ||
return mIsMergedTask; | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...stSDK/src/main/java/com/sensorsdata/abtest/core/RequestExperimentTaskRecorderManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Created by luweibin on 2021/11/15. | ||
* Copyright 2015-2021 Sensors Data Inc. | ||
* | ||
* 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.sensorsdata.abtest.core; | ||
|
||
import com.sensorsdata.abtest.OnABTestReceivedData; | ||
import com.sensorsdata.analytics.android.sdk.SALog; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class RequestExperimentTaskRecorderManager { | ||
private static final String TAG = "SAB.RequestExperimentTaskManager"; | ||
private final List<RequestExperimentTaskRecorder> mTaskList = new LinkedList<>(); | ||
|
||
private static class SingleHolder { | ||
private static final RequestExperimentTaskRecorderManager INSTANCE = new RequestExperimentTaskRecorderManager(); | ||
} | ||
|
||
private RequestExperimentTaskRecorderManager() { | ||
} | ||
|
||
static RequestExperimentTaskRecorderManager getInstance() { | ||
return SingleHolder.INSTANCE; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
synchronized <T> RequestExperimentTaskRecorder createRequest(String loginId, String anonymousId, String paramName, | ||
Map<String, Object> properties, int timeoutMillSeconds, | ||
OnABTestReceivedData<T> onABTestReceivedData, T defaultValue) { | ||
SALog.i(TAG, "create new request task"); | ||
RequestExperimentTaskRecorder currentTask = new RequestExperimentTaskRecorder(loginId, anonymousId, paramName, properties, timeoutMillSeconds); | ||
mTaskList.add(currentTask); | ||
currentTask.putCallbackAndDefaultValue((OnABTestReceivedData<Object>) onABTestReceivedData, defaultValue); | ||
currentTask.setIsMergedTask(false); | ||
return currentTask; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
synchronized <T> RequestExperimentTaskRecorder mergeRequest(String loginId, String anonymousId, String paramName, | ||
Map<String, Object> properties, int timeoutMillSeconds, | ||
OnABTestReceivedData<?> onABTestReceivedData, T defaultValue) { | ||
boolean isTaskExist = false; | ||
RequestExperimentTaskRecorder currentTask = null; | ||
for (RequestExperimentTaskRecorder task : mTaskList) { | ||
if (task.isSameExperimentTask(loginId, anonymousId, paramName, properties, timeoutMillSeconds)) { | ||
currentTask = task; | ||
isTaskExist = true; | ||
break; | ||
} | ||
} | ||
if (currentTask == null) { | ||
currentTask = new RequestExperimentTaskRecorder(loginId, anonymousId, paramName, properties, timeoutMillSeconds); | ||
mTaskList.add(currentTask); | ||
isTaskExist = false; | ||
} | ||
SALog.i(TAG, "create new request task if not exist, task is exist = " + isTaskExist); | ||
currentTask.putCallbackAndDefaultValue((OnABTestReceivedData<Object>) onABTestReceivedData, defaultValue); | ||
currentTask.setIsMergedTask(isTaskExist); | ||
return currentTask; | ||
} | ||
|
||
synchronized void removeTask(RequestExperimentTaskRecorder task) { | ||
mTaskList.remove(task); | ||
} | ||
} |
Oops, something went wrong.