-
Notifications
You must be signed in to change notification settings - Fork 16
/
SignHistoryUtil.java
222 lines (172 loc) · 5.81 KB
/
SignHistoryUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package com.microblog.points.service.strategy;
import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
/**
*功能描述
* @author lgj
* @Description 签到历史工具类
* @date 6/27/19
*/
@Slf4j
public class SignHistoryUtil {
/**
*功能描述
* @author lgj
* @Description 签到
* @date 6/27/19
* @param: signHistory: 原始签到字符串
* dayOfYear: 需要签到的那一天(那年的第几天)
* @return: 最新生成的签到历史字符串
*
*/
public static String sign(String signHistory,int dayOfYear) throws Exception {
if(signHistory == null){
throw new SignException("SignHistory can not be null!");
}
checkOutofDay(dayOfYear);
byte[] decodeResult = signHistoryToByte(signHistory);
int index = dayOfYear / 8;
int offset = dayOfYear % 8;
byte data = decodeResult[index];
data = (byte)(data|(1 << (7-offset)));
decodeResult[index] = data ;
String encodeResult = new BASE64Encoder().encode(decodeResult);
return encodeResult;
}
/**
*功能描述
* @author lgj
* @Description 获取默认的签到历史字符串,也就是没有任何一天签到的签到历史
* @date 6/27/19
* @param:
* @return:
*
*/
public static String defaultsignHistory(){
byte[] encodeData = new byte[46];
return new BASE64Encoder().encode(encodeData);
}
/**
*功能描述
* @author lgj
* @Description 签到历史字符串转换成字节数组
* @date 6/27/19
* @param:
* @return:
*
*/
public static byte[] signHistoryToByte(String signHistory) throws Exception {
if(signHistory == null){
throw new SignException("SignHistory can not be null!");
}
return new BASE64Decoder().decodeBuffer(signHistory);
}
/**
*功能描述
* @author lgj
* @Description 获取某年某月的签到数据
* @date 6/27/19
* @param: List<Integer>,如果当月的第一天和第三天签到,返回[1,3]
* @return:
*
*/
public static List<Integer> getSignHistoryByMonth(String signHistory, int year, int month)throws Exception{
if(signHistory == null){
throw new SignException("SignHistory can not be null!");
}
checkOutofMonth(month);
LocalDate localDate = LocalDate.of(year,month,1);
int start = localDate.getDayOfYear();
int dayOfMonth = localDate.lengthOfMonth();
//log.info("dayOfMonth = {}",dayOfMonth);
localDate = localDate.withDayOfMonth(dayOfMonth);
int end = localDate.getDayOfYear();
//log.info("start={},end={}",start,end);
Integer result = 0;
byte[] data = signHistoryToByte(signHistory);
List<Integer> signDayOfMonths = new ArrayList<>();
int signDay = 0;
for(int i = start; i< end ; i++){
signDay++;
if(isSign(data,i)){
signDayOfMonths.add(signDay);
}
}
return signDayOfMonths;
}
/**
*功能描述
* @author lgj
* @Description 测试某年的第n天是否签到
* @date 6/27/19
* @param: true: 该天签到 false:没有签到
* @return:
*
*/
public static boolean isSign(byte[] data,int dayOfYear) throws Exception{
checkOutofDay(dayOfYear);
int index = dayOfYear / 8;
int offset = dayOfYear % 8;
//System.out.print(index+"-");
int flag = data[index] & (1 << (7-offset));
return flag == 0?false:true;
}
public static boolean isSign(String signHistory,int dayOfYear) throws Exception{
checkOutofDay(dayOfYear);
byte[] data = signHistoryToByte(signHistory);
int index = dayOfYear / 8;
int offset = dayOfYear % 8;
//System.out.print(index+"-");
int flag = data[index] & (1 << (7-offset));
return flag == 0?false:true;
}
/**
*功能描述
* @author lgj
* @Description 获取过去几天的连续签到的次数
* @date 6/27/19
* @param:
* @return: 今天 6.27 签到, 同时 6.26 ,6.25 也签到 ,6.24 未签到 ,返回 3
* 今天 6.27 未签到, 同时 6.26 ,6.25 也签到 ,6.24 未签到 ,返回 2
*
*/
public static int getMaxContinuitySignDay(String signHistory) throws Exception{
int maxContinuitySignDay = 0;
if(signHistory == null){
throw new SignException("SignHistory can not be null!");
}
LocalDate localDate =LocalDate.now();
int curDayOfYear = localDate.getDayOfYear();
byte[] data = signHistoryToByte(signHistory);
int checkDayOfYear = curDayOfYear-1;
while (checkDayOfYear > 0){
if(isSign(data,checkDayOfYear)){
checkDayOfYear--;
maxContinuitySignDay++;
}
else {
break;
}
}
if(isSign(data,curDayOfYear)){
maxContinuitySignDay +=1;
}
return maxContinuitySignDay;
}
private static void checkOutofDay(int dayOfYear) throws Exception{
LocalDate localDate =LocalDate.now();
int maxDay = localDate.isLeapYear()?366:365;
if((dayOfYear <= 0)&&( dayOfYear > maxDay)){
throw new SignException("The param dayOfYear["+dayOfYear+"] is out of [0-"+ maxDay+"]");
}
}
private static void checkOutofMonth(int month) throws Exception{
if((month <= 0)&&( month > 12)){
throw new SignException("The param month["+month+"] is out of [0-"+ 12+"]");
}
}
}