Skip to content

Commit b1aafae

Browse files
committed
规范部分代码
1 parent d804d15 commit b1aafae

File tree

12 files changed

+69
-53
lines changed

12 files changed

+69
-53
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/session/Constants.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
* The ASF licenses this file to You under the Apache License, Version 2.0
66
* (the "License"); you may not use this file except in compliance with
77
* the License. You may obtain a copy of the License at
8-
*
8+
*
99
* http://www.apache.org/licenses/LICENSE-2.0
10-
*
10+
*
1111
* Unless required by applicable law or agreed to in writing, software
1212
* distributed under the License is distributed on an "AS IS" BASIS,
1313
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -23,9 +23,6 @@
2323
*
2424
* @author Craig R. McClanahan
2525
*/
26-
2726
public class Constants {
28-
29-
public static final String Package = "me.chanjar.weixin.common.session";
30-
27+
public static final String PACKAGE = "me.chanjar.weixin.common.session";
3128
}

weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSession.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,12 @@ public class StandardSession implements WxSession, InternalSession {
1111
/**
1212
* The string manager for this package.
1313
*/
14-
protected static final StringManager sm = StringManager.getManager(Constants.Package);
14+
protected static final StringManager SM = StringManager.getManager(Constants.PACKAGE);
1515
/**
1616
* Type array.
1717
*/
1818
private static final String[] EMPTY_ARRAY = new String[0];
1919

20-
// ------------------------------ WxSession
2120
protected Map<String, Object> attributes = new ConcurrentHashMap<>();
2221
/**
2322
* The session identifier of this Session.
@@ -73,7 +72,7 @@ public Object getAttribute(String name) {
7372

7473
if (!isValidInternal()) {
7574
throw new IllegalStateException
76-
(sm.getString("sessionImpl.getAttribute.ise"));
75+
(SM.getString("sessionImpl.getAttribute.ise"));
7776
}
7877

7978
if (name == null) {
@@ -86,7 +85,7 @@ public Object getAttribute(String name) {
8685
@Override
8786
public Enumeration<String> getAttributeNames() {
8887
if (!isValidInternal()) {
89-
throw new IllegalStateException(sm.getString("sessionImpl.getAttributeNames.ise"));
88+
throw new IllegalStateException(SM.getString("sessionImpl.getAttributeNames.ise"));
9089
}
9190

9291
Set<String> names = new HashSet<>();
@@ -98,7 +97,7 @@ public Enumeration<String> getAttributeNames() {
9897
public void setAttribute(String name, Object value) {
9998
// Name cannot be null
10099
if (name == null) {
101-
throw new IllegalArgumentException(sm.getString("sessionImpl.setAttribute.namenull"));
100+
throw new IllegalArgumentException(SM.getString("sessionImpl.setAttribute.namenull"));
102101
}
103102

104103
// Null value is the same as removeAttribute()
@@ -109,7 +108,7 @@ public void setAttribute(String name, Object value) {
109108

110109
// Validate our current state
111110
if (!isValidInternal()) {
112-
throw new IllegalStateException(sm.getString("sessionImpl.setAttribute.ise", getIdInternal()));
111+
throw new IllegalStateException(SM.getString("sessionImpl.setAttribute.ise", getIdInternal()));
113112
}
114113

115114
this.attributes.put(name, value);
@@ -123,8 +122,9 @@ public void removeAttribute(String name) {
123122

124123
@Override
125124
public void invalidate() {
126-
if (!isValidInternal())
127-
throw new IllegalStateException(sm.getString("sessionImpl.invalidate.ise"));
125+
if (!isValidInternal()) {
126+
throw new IllegalStateException(SM.getString("sessionImpl.invalidate.ise"));
127+
}
128128

129129
// Cause this session to expire
130130
expire();

weixin-java-common/src/main/java/me/chanjar/weixin/common/session/StandardSessionManager.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
*/
1414
public class StandardSessionManager implements WxSessionManager, InternalSessionManager {
1515

16-
protected static final StringManager sm =
17-
StringManager.getManager(Constants.Package);
16+
protected static final StringManager SM = StringManager.getManager(Constants.PACKAGE);
1817
/**
1918
* The descriptive name of this Manager implementation (for logging).
2019
*/
@@ -82,7 +81,7 @@ public WxSession getSession(String sessionId) {
8281
public WxSession getSession(String sessionId, boolean create) {
8382
if (sessionId == null) {
8483
throw new IllegalStateException
85-
(sm.getString("sessionManagerImpl.getSession.ise"));
84+
(SM.getString("sessionManagerImpl.getSession.ise"));
8685
}
8786

8887
InternalSession session = findSession(sessionId);
@@ -124,25 +123,24 @@ public void remove(InternalSession session, boolean update) {
124123

125124
@Override
126125
public InternalSession findSession(String id) {
127-
128-
if (id == null)
126+
if (id == null) {
129127
return (null);
128+
}
130129
return this.sessions.get(id);
131-
132130
}
133131

134132
@Override
135133
public InternalSession createSession(String sessionId) {
136134
if (sessionId == null) {
137135
throw new IllegalStateException
138-
(sm.getString("sessionManagerImpl.createSession.ise"));
136+
(SM.getString("sessionManagerImpl.createSession.ise"));
139137
}
140138

141139
if ((this.maxActiveSessions >= 0) &&
142140
(getActiveSessions() >= this.maxActiveSessions)) {
143141
this.rejectedSessions++;
144142
throw new TooManyActiveSessionsException(
145-
sm.getString("sessionManagerImpl.createSession.tmase"),
143+
SM.getString("sessionManagerImpl.createSession.tmase"),
146144
this.maxActiveSessions);
147145
}
148146

@@ -230,8 +228,9 @@ public InternalSession[] findSessions() {
230228
@Override
231229
public void backgroundProcess() {
232230
this.count = (this.count + 1) % this.processExpiresFrequency;
233-
if (this.count == 0)
231+
if (this.count == 0) {
234232
processExpires();
233+
}
235234
}
236235

237236
/**
@@ -243,16 +242,18 @@ public void processExpires() {
243242
InternalSession sessions[] = findSessions();
244243
int expireHere = 0;
245244

246-
if (this.log.isDebugEnabled())
245+
if (this.log.isDebugEnabled()) {
247246
this.log.debug("Start expire sessions {} at {} sessioncount {}", getName(), timeNow, sessions.length);
248-
for (int i = 0; i < sessions.length; i++) {
249-
if (sessions[i] != null && !sessions[i].isValid()) {
247+
}
248+
for (InternalSession session : sessions) {
249+
if (session != null && !session.isValid()) {
250250
expireHere++;
251251
}
252252
}
253253
long timeEnd = System.currentTimeMillis();
254-
if (this.log.isDebugEnabled())
254+
if (this.log.isDebugEnabled()) {
255255
this.log.debug("End expire sessions {} processingTime {} expired sessions: {}", getName(), timeEnd - timeNow, expireHere);
256+
}
256257
this.processingTime += (timeEnd - timeNow);
257258

258259
}

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/WxCryptUtil.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
*/
2929
public class WxCryptUtil {
3030

31-
private static final Base64 base64 = new Base64();
31+
private static final Base64 BASE64 = new Base64();
3232
private static final Charset CHARSET = StandardCharsets.UTF_8;
3333

34-
private static final ThreadLocal<DocumentBuilder> builderLocal = new ThreadLocal<DocumentBuilder>() {
34+
private static final ThreadLocal<DocumentBuilder> BUILDER_LOCAL = new ThreadLocal<DocumentBuilder>() {
3535
@Override
3636
protected DocumentBuilder initialValue() {
3737
try {
@@ -65,7 +65,7 @@ public WxCryptUtil(String token, String encodingAesKey,
6565

6666
static String extractEncryptPart(String xml) {
6767
try {
68-
DocumentBuilder db = builderLocal.get();
68+
DocumentBuilder db = BUILDER_LOCAL.get();
6969
Document document = db.parse(new InputSource(new StringReader(xml)));
7070

7171
Element root = document.getDocumentElement();
@@ -192,7 +192,7 @@ protected String encrypt(String randomStr, String plainText) {
192192
byte[] encrypted = cipher.doFinal(unencrypted);
193193

194194
// 使用BASE64对加密后的字符串进行编码
195-
return base64.encodeToString(encrypted);
195+
return BASE64.encodeToString(encrypted);
196196
} catch (Exception e) {
197197
throw new RuntimeException(e);
198198
}

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/http/HttpType.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,16 @@
44
* Created by ecoolper on 2017/4/28.
55
*/
66
public enum HttpType {
7-
JODD_HTTP, APACHE_HTTP, OK_HTTP;
7+
/**
8+
* jodd-http.
9+
*/
10+
JODD_HTTP,
11+
/**
12+
* apache httpclient.
13+
*/
14+
APACHE_HTTP,
15+
/**
16+
* okhttp.
17+
*/
18+
OK_HTTP
819
}

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/res/StringManager.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@
4646
*/
4747
public class StringManager {
4848

49-
private static final Map<String, Map<Locale, StringManager>> managers =
50-
new Hashtable<>();
49+
private static final Map<String, Map<Locale, StringManager>> MANAGERS = new Hashtable<>();
5150
private static int LOCALE_CACHE_SIZE = 10;
5251
/**
5352
* The ResourceBundle for this StringManager.
@@ -118,16 +117,16 @@ public static final synchronized StringManager getManager(
118117
public static final synchronized StringManager getManager(
119118
String packageName, Locale locale) {
120119

121-
Map<Locale, StringManager> map = managers.get(packageName);
120+
Map<Locale, StringManager> map = MANAGERS.get(packageName);
122121
if (map == null) {
123-
/*
124-
* Don't want the HashMap to be expanded beyond LOCALE_CACHE_SIZE.
125-
* Expansion occurs when size() exceeds capacity. Therefore keep
126-
* size at or below capacity.
127-
* removeEldestEntry() executes after insertion therefore the test
128-
* for removal needs to use one less than the maximum desired size
129-
*
130-
*/
122+
/*
123+
* Don't want the HashMap to be expanded beyond LOCALE_CACHE_SIZE.
124+
* Expansion occurs when size() exceeds capacity. Therefore keep
125+
* size at or below capacity.
126+
* removeEldestEntry() executes after insertion therefore the test
127+
* for removal needs to use one less than the maximum desired size
128+
*
129+
*/
131130
map = new LinkedHashMap<Locale, StringManager>(LOCALE_CACHE_SIZE, 1, true) {
132131
private static final long serialVersionUID = 1L;
133132

@@ -137,7 +136,7 @@ protected boolean removeEldestEntry(
137136
return size() > (LOCALE_CACHE_SIZE - 1);
138137
}
139138
};
140-
managers.put(packageName, map);
139+
MANAGERS.put(packageName, map);
141140
}
142141

143142
StringManager mgr = map.get(locale);

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpUser.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@
1616
@Data
1717
public class WxCpUser implements Serializable {
1818
public enum Gender {
19+
/**
20+
* 男
21+
*/
1922
MALE("男", "1"),
20-
FEMAIL("女", "2");
23+
/**
24+
* 女
25+
*/
26+
FEMALE("女", "2");
2127

2228
private String genderName;
2329
private String code;
@@ -40,7 +46,7 @@ public static Gender fromCode(String code) {
4046
return Gender.MALE;
4147
}
4248
if ("2".equals(code)) {
43-
return Gender.FEMAIL;
49+
return Gender.FEMALE;
4450
}
4551

4652
return null;

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpUserServiceImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public void testCreate() throws Exception {
3737
user.setName("Some Woman");
3838
user.setDepartIds(new Integer[]{2});
3939
user.setEmail("none@none.com");
40-
user.setGender(WxCpUser.Gender.FEMAIL);
40+
user.setGender(WxCpUser.Gender.FEMALE);
4141
user.setMobile("13560084979");
4242
user.setPosition("woman");
4343
user.setTelephone("3300393");

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpCardServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public void markCardCode(String code, String cardId, String openId, boolean isMa
213213
WxMpCardResult cardResult = WxMpGsonBuilder.INSTANCE.create().fromJson(tmpJsonElement,
214214
new TypeToken<WxMpCardResult>() {
215215
}.getType());
216-
if (!cardResult.getErrorCode().equals("0")) {
216+
if (!"0".equals(cardResult.getErrorCode())) {
217217
this.log.warn("朋友的券mark失败:{}", cardResult.getErrorMsg());
218218
}
219219
}

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceAbstractImpl.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ public String post(String url, String postData) throws WxErrorException {
227227
/**
228228
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求
229229
*/
230+
@Override
230231
public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
231232
int retryTimes = 0;
232233
do {

0 commit comments

Comments
 (0)