1
1
package nettee .board .driven .rdb .entity .type ;
2
2
3
3
import nettee .board .domain .type .BoardStatus ;
4
- import nettee .common .marker .TypeSafeMarker ;
5
- import nettee .common .marker .TypeSafeMarker .Missing ;
6
4
import nettee .common .marker .TypeSafeMarker .Present ;
5
+ import nettee .common .status .StatusCodeUtil ;
6
+ import nettee .common .status .StatusParameters ;
7
+ import nettee .common .status .StatusParameters .GeneralPurposeFeatures ;
8
+ import nettee .common .util .EnumUtil ;
7
9
8
10
import java .util .Arrays ;
9
11
import java .util .Set ;
10
12
import java .util .stream .Collectors ;
11
13
12
- import static nettee .board .exception .BoardErrorCode .DEFAULT ;
14
+ import static nettee .board .exception .BoardErrorCode .UNMAPPED_BOARD_STATUS ;
13
15
14
16
public enum BoardEntityStatus {
15
17
REMOVED (
16
- SemanticCodeParameters .builder ()
17
- .canRead (false )
18
- .classifyingBits (0b0000_0000_0000_0000)
18
+ StatusParameters .generate ()
19
+ .generalPurposeFeatures (
20
+ GeneralPurposeFeatures .READ ,
21
+ GeneralPurposeFeatures .SUBITEM_READ
22
+ )
23
+ .categoryBits (0b0000_0000_0000_0000)
24
+ .instanceBits (0 )
19
25
),
20
26
PENDING (
21
- SemanticCodeParameters . builder ()
22
- .canRead ( false )
23
- .classifyingBits (0b0000_0000_0000_0001 )
27
+ StatusParameters . generate ()
28
+ .categoryBits (0b0000_0000_0000_0001 )
29
+ .instanceBits ( 0 )
24
30
),
25
31
ACTIVE (
26
- SemanticCodeParameters .builder ()
27
- .canRead (true )
28
- .classifyingBits (0b0000_0000_0000_0010)
32
+ StatusParameters .generate ()
33
+ .generalPurposeFeatures (GeneralPurposeFeatures .ALL )
34
+ .categoryBits (0b0000_0000_0000_0010)
35
+ .instanceBits (0 )
29
36
),
30
37
SUSPENDED (
31
- SemanticCodeParameters .builder ()
32
- .canRead (true )
33
- .classifyingBits (0b0000_0000_0000_0100)
38
+ StatusParameters .generate ()
39
+ .generalPurposeFeatures (
40
+ GeneralPurposeFeatures .READ ,
41
+ GeneralPurposeFeatures .SUBITEM_READ
42
+ )
43
+ .categoryBits (0b0000_0000_0000_01000)
44
+ .instanceBits (0 )
34
45
);
35
46
36
- /*
37
- R000 0000 0000 0000 0PPP PPPP PPPP PPPP
38
- R: generally readable status (1: readable, 0: unreadable)
39
- 0: classifying bits (16 bits)
40
- P: detailed or padded bits (15 bits)
41
- */
42
- private static final int TLB_PADDING_SIZE = 31 ;
43
- private static final int CLASSIFYING_PADDING_SIZE = 15 ;
44
-
45
47
private final int code ;
46
48
47
49
static {
48
- // NOTE util 함수가 추가되면 리팩토링
49
- assert Arrays .stream (values ())
50
- .map (BoardEntityStatus ::getCode )
51
- .collect (Collectors .toSet ())
52
- .size ()
53
- == values ().length
50
+ assert EnumUtil .isUniqueAllOf (BoardEntityStatus .class , BoardEntityStatus ::getCode )
54
51
: "BoardEntityStatus의 모든 code 필드가 고유해야 합니다." ;
55
52
}
56
53
57
- BoardEntityStatus (SemanticCodeParameters <Present , Present > semanticCodeParameters ) {
58
- this (
59
- semanticCodeParameters .canRead ,
60
- semanticCodeParameters .classifyingBits ,
61
- semanticCodeParameters .detailBits
62
- );
54
+ BoardEntityStatus (StatusParameters <Present , Present > semanticCodeParameters ) {
55
+ this (StatusCodeUtil .getAsInt (semanticCodeParameters ));
63
56
}
64
57
65
- BoardEntityStatus (boolean canRead , int classifyingBits , int detailBits ) {
66
- this .code = (canRead ? 1 << TLB_PADDING_SIZE : 0 )
67
- | (classifyingBits << CLASSIFYING_PADDING_SIZE )
68
- | detailBits ;
58
+ BoardEntityStatus (int code ) {
59
+ this .code = code ;
69
60
}
70
61
71
62
public int getCode () {
@@ -75,54 +66,32 @@ public int getCode() {
75
66
public static BoardEntityStatus valueOf (BoardStatus boardStatus ) {
76
67
assert Set .of (BoardStatus .REMOVED , BoardStatus .PENDING , BoardStatus .ACTIVE , BoardStatus .SUSPENDED )
77
68
.containsAll (Arrays .stream (BoardStatus .values ()).collect (Collectors .toSet ()))
78
- : "BoardStatus 중 일부가 BoardEntityStatus::valueOf 함수에서 매핑되지 않습니다." ;
69
+ : "BoardStatus 중 일부가 BoardEntityStatus 인스턴스에 매핑되지 않습니다." ;
79
70
80
71
return switch (boardStatus ){
81
72
case REMOVED -> REMOVED ;
82
73
case PENDING -> PENDING ;
83
74
case ACTIVE -> ACTIVE ;
84
75
case SUSPENDED -> SUSPENDED ;
85
- default -> throw new Error ("BoardStatus 중 일부가 BoardEntityStatus::valueOf 함수에서 매핑되지 않습니다." );
76
+ default -> throw new Error ("BoardStatus 중 일부가 BoardEntityStatus 인스턴스에 매핑되지 않습니다." );
86
77
};
87
78
}
88
79
89
80
public static BoardEntityStatus valueOf (int value ) {
90
81
return switch (value ) {
91
- case 0b0__0000_0000_0000_0000__000_0000_0000_0000 -> REMOVED ;
92
- case 0b0__0000_0000_0000_0001__000_0000_0000_0000 -> PENDING ;
93
- case 0b1__0000_0000_0000_0010__000_0000_0000_0000 -> ACTIVE ;
94
- case 0b1__0000_0000_0000_0100__000_0000_0000_0000 -> SUSPENDED ;
95
- default -> throw DEFAULT .exception ();
82
+ /*
83
+ * ø: 부호 비트 (사용 안 함)
84
+ * X: 기능 비트 (일반 목적 비트)
85
+ * S: System info bits
86
+ * C: Category bits
87
+ * I: Instance detail bits
88
+
89
+ ø_XXX_XXXX__SSSS_SSSS__CCCC_CCCC__IIII_IIII */
90
+ case 0b0_100_1000__0000_0000__0000_0000__0000_0000 -> REMOVED ;
91
+ case 0b0_000_0000__0000_0000__0000_0001__0000_0000 -> PENDING ;
92
+ case 0b0_110_1100__0000_0000__0000_0010__0000_0000 -> ACTIVE ;
93
+ case 0b0_100_1000__0000_0000__0000_0100__0000_0000 -> SUSPENDED ;
94
+ default -> throw UNMAPPED_BOARD_STATUS .exception ();
96
95
};
97
96
}
98
-
99
- static class SemanticCodeParameters <
100
- HAS_CAN_READ extends TypeSafeMarker ,
101
- HAS_CLASSIFYING_BITS extends TypeSafeMarker > {
102
-
103
- boolean canRead ;
104
- Integer classifyingBits ;
105
- int detailBits ;
106
-
107
- private SemanticCodeParameters () {}
108
-
109
- public static SemanticCodeParameters <Missing , Missing > builder () {
110
- return new SemanticCodeParameters <>();
111
- }
112
-
113
- SemanticCodeParameters <HAS_CAN_READ , Present > classifyingBits (Integer classifyingBits ) {
114
- this .classifyingBits = classifyingBits ;
115
- return (SemanticCodeParameters <HAS_CAN_READ , Present >) this ;
116
- }
117
-
118
- SemanticCodeParameters <Present , HAS_CLASSIFYING_BITS > canRead (boolean canRead ) {
119
- this .canRead = canRead ;
120
- return (SemanticCodeParameters <Present , HAS_CLASSIFYING_BITS >) this ;
121
- }
122
-
123
- SemanticCodeParameters <HAS_CAN_READ , HAS_CLASSIFYING_BITS > detailBits (int detailBits ) {
124
- this .detailBits = detailBits ;
125
- return this ;
126
- }
127
- }
128
97
}
0 commit comments