Skip to content

Commit

Permalink
⚡ 调整 map 声明时的初始容量
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyd-c committed Mar 29, 2021
1 parent 6a479d9 commit 13514d6
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthAmazonRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public String authorize(String state) {
*/
@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
Map<String, String> form = new HashMap<>(8);
Map<String, String> form = new HashMap<>(9);
form.put("grant_type", "authorization_code");
form.put("code", authCallback.getCode());
form.put("redirect_uri", config.getRedirectUri());
Expand All @@ -95,7 +95,7 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {

@Override
public AuthResponse refresh(AuthToken authToken) {
Map<String, String> form = new HashMap<>(6);
Map<String, String> form = new HashMap<>(7);
form.put("grant_type", "refresh_token");
form.put("refresh_token", authToken.getRefreshToken());
form.put("client_id", config.getClientId());
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/me/zhyd/oauth/request/AuthElemeRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public AuthElemeRequest(AuthConfig config, AuthStateCache authStateCache) {

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
Map<String, String> form = new HashMap<>(4);
Map<String, String> form = new HashMap<>(7);
form.put("client_id", config.getClientId());
form.put("redirect_uri", config.getRedirectUri());
form.put("code", authCallback.getCode());
Expand All @@ -67,13 +67,13 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
Map<String, Object> parameters = new HashMap<>();
Map<String, Object> parameters = new HashMap<>(4);
// 获取商户账号信息的API接口名称
String action = "eleme.user.getUser";
// 时间戳,单位秒。API服务端允许客户端请求最大时间误差为正负5分钟。
final long timestamp = System.currentTimeMillis();
// 公共参数
Map<String, Object> metasHashMap = new HashMap<>();
Map<String, Object> metasHashMap = new HashMap<>(4);
metasHashMap.put("app_key", config.getClientId());
metasHashMap.put("timestamp", timestamp);
String signature = GlobalAuthUtils.generateElemeSignature(config.getClientId(), config.getClientSecret(), timestamp, action, authToken
Expand Down Expand Up @@ -118,7 +118,7 @@ protected AuthUser getUserInfo(AuthToken authToken) {

@Override
public AuthResponse refresh(AuthToken oldToken) {
Map<String, String> form = new HashMap<>(2);
Map<String, String> form = new HashMap<>(4);
form.put("refresh_token", oldToken.getRefreshToken());
form.put("grant_type", "refresh_token");

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/me/zhyd/oauth/request/AuthHuaweiRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public AuthHuaweiRequest(AuthConfig config, AuthStateCache authStateCache) {
*/
@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
Map<String, String> form = new HashMap<>(5);
Map<String, String> form = new HashMap<>(8);
form.put("grant_type", "authorization_code");
form.put("code", authCallback.getAuthorization_code());
form.put("client_id", config.getClientId());
Expand All @@ -67,7 +67,7 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {
*/
@Override
protected AuthUser getUserInfo(AuthToken authToken) {
Map<String, String> form = new HashMap<>(4);
Map<String, String> form = new HashMap<>(7);
form.put("nsp_ts", System.currentTimeMillis() + "");
form.put("access_token", authToken.getAccessToken());
form.put("nsp_fmt", "JS");
Expand Down Expand Up @@ -100,7 +100,7 @@ protected AuthUser getUserInfo(AuthToken authToken) {
*/
@Override
public AuthResponse refresh(AuthToken authToken) {
Map<String, String> form = new HashMap<>(4);
Map<String, String> form = new HashMap<>(7);
form.put("client_id", config.getClientId());
form.put("client_secret", config.getClientSecret());
form.put("refresh_token", authToken.getRefreshToken());
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/zhyd/oauth/request/AuthJdRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public AuthJdRequest(AuthConfig config, AuthStateCache authStateCache) {
@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {

Map<String, String> params = new HashMap<>(5);
Map<String, String> params = new HashMap<>(7);
params.put("app_key", config.getClientId());
params.put("app_secret", config.getClientSecret());
params.put("grant_type", "authorization_code");
Expand Down Expand Up @@ -104,7 +104,7 @@ private JSONObject getUserDataJsonObject(JSONObject object) {

@Override
public AuthResponse refresh(AuthToken oldToken) {
Map<String, String> params = new HashMap<>(5);
Map<String, String> params = new HashMap<>(7);
params.put("app_key", config.getClientId());
params.put("app_secret", config.getClientSecret());
params.put("grant_type", "refresh_token");
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/me/zhyd/oauth/request/AuthMeituanRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public AuthMeituanRequest(AuthConfig config, AuthStateCache authStateCache) {

@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
Map<String, String> form = new HashMap<>(4);
Map<String, String> form = new HashMap<>(7);
form.put("app_id", config.getClientId());
form.put("secret", config.getClientSecret());
form.put("code", authCallback.getCode());
Expand All @@ -55,7 +55,7 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
Map<String, String> form = new HashMap<>(3);
Map<String, String> form = new HashMap<>(5);
form.put("app_id", config.getClientId());
form.put("secret", config.getClientSecret());
form.put("access_token", authToken.getAccessToken());
Expand All @@ -79,7 +79,7 @@ protected AuthUser getUserInfo(AuthToken authToken) {

@Override
public AuthResponse refresh(AuthToken oldToken) {
Map<String, String> form = new HashMap<>(4);
Map<String, String> form = new HashMap<>(7);
form.put("app_id", config.getClientId());
form.put("secret", config.getClientSecret());
form.put("refresh_token", oldToken.getRefreshToken());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public AuthTeambitionRequest(AuthConfig config, AuthStateCache authStateCache) {
*/
@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
Map<String, String> form = new HashMap<>(4);
Map<String, String> form = new HashMap<>(7);
form.put("client_id", config.getClientId());
form.put("client_secret", config.getClientSecret());
form.put("code", authCallback.getCode());
Expand Down Expand Up @@ -90,7 +90,7 @@ public AuthResponse refresh(AuthToken oldToken) {
String uid = oldToken.getUid();
String refreshToken = oldToken.getRefreshToken();

Map<String, String> form = new HashMap<>(2);
Map<String, String> form = new HashMap<>(4);
form.put("_userId", uid);
form.put("refresh_token", refreshToken);
String response = new HttpUtils(config.getHttpConfig()).post(source.refresh(), form, false);
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/me/zhyd/oauth/request/AuthTwitterRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {
httpHeader.add("Authorization", header);
httpHeader.add(Constants.CONTENT_TYPE, "application/x-www-form-urlencoded");

Map<String, String> form = new HashMap<>(1);
Map<String, String> form = new HashMap<>(3);
form.put("oauth_verifier", authCallback.getOauth_verifier());
String response = new HttpUtils(config.getHttpConfig()).post(source.accessToken(), form, httpHeader, false);

Expand All @@ -118,7 +118,7 @@ protected AuthToken getAccessToken(AuthCallback authCallback) {

@Override
protected AuthUser getUserInfo(AuthToken authToken) {
Map<String, String> queryParams = new HashMap<>();
Map<String, String> queryParams = new HashMap<>(5);
queryParams.put("user_id", authToken.getUserId());
queryParams.put("screen_name", authToken.getScreenName());
queryParams.put("include_entities", Boolean.toString(true));
Expand Down Expand Up @@ -162,7 +162,7 @@ protected String userInfoUrl(AuthToken authToken) {
}

private Map<String, String> buildOauthParams() {
Map<String, String> params = new HashMap<>(5);
Map<String, String> params = new HashMap<>(12);
params.put("oauth_consumer_key", config.getClientId());
params.put("oauth_nonce", GlobalAuthUtils.generateNonce(32));
params.put("oauth_signature_method", "HMAC-SHA1");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/zhyd/oauth/request/AuthXmlyRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public AuthXmlyRequest(AuthConfig config, AuthStateCache authStateCache) {
*/
@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
Map<String, String> map = new HashMap<>(6);
Map<String, String> map = new HashMap<>(9);
map.put("code", authCallback.getCode());
map.put("client_id", config.getClientId());
map.put("client_secret", config.getClientSecret());
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/me/zhyd/oauth/utils/GlobalAuthUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,18 @@ public static String urlDecode(String value) {
* @return map
*/
public static Map<String, String> parseStringToMap(String accessTokenStr) {
Map<String, String> res = new HashMap<>(6);
Map<String, String> res = null;
if (accessTokenStr.contains("&")) {
String[] fields = accessTokenStr.split("&");
res = new HashMap<>((int) (fields.length / 0.75 + 1));
for (String field : fields) {
if (field.contains("=")) {
String[] keyValue = field.split("=");
res.put(GlobalAuthUtils.urlDecode(keyValue[0]), keyValue.length == 2 ? GlobalAuthUtils.urlDecode(keyValue[1]) : null);
}
}
} else {
res = new HashMap<>(0);
}
return res;
}
Expand Down

0 comments on commit 13514d6

Please sign in to comment.