Skip to content

Commit 4942de2

Browse files
committed
IGNITE-26728 Add EnumMessage
1 parent 36cbb27 commit 4942de2

14 files changed

+262
-202
lines changed

modules/core/src/main/java/org/apache/ignite/internal/managers/communication/CacheWriteSynchronizationModeMessage.java

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,51 +18,37 @@
1818
package org.apache.ignite.internal.managers.communication;
1919

2020
import org.apache.ignite.cache.CacheWriteSynchronizationMode;
21-
import org.apache.ignite.internal.Order;
22-
import org.apache.ignite.plugin.extensions.communication.Message;
2321
import org.jetbrains.annotations.Nullable;
2422

2523
/** */
26-
public class CacheWriteSynchronizationModeMessage implements Message {
24+
public class CacheWriteSynchronizationModeMessage extends EnumMessage<CacheWriteSynchronizationMode> {
2725
/** Type code. */
2826
public static final short TYPE_CODE = 503;
2927

30-
/** Cache write synchronization mode value. */
31-
@Nullable private CacheWriteSynchronizationMode cacheWriteSyncMode;
32-
33-
/** Code of cache write synchronization mode. */
34-
@Order(0)
35-
private byte code = -1;
36-
3728
/** Constructor. */
3829
public CacheWriteSynchronizationModeMessage() {
3930
// No-op.
4031
}
4132

4233
/** Constructor. */
43-
public CacheWriteSynchronizationModeMessage(@Nullable CacheWriteSynchronizationMode mode) {
44-
cacheWriteSyncMode = mode;
45-
code = encode(mode);
34+
public CacheWriteSynchronizationModeMessage(@Nullable CacheWriteSynchronizationMode val) {
35+
super(val);
4636
}
4737

48-
/** @param mode Cache write synchronization mode to encode. */
49-
private static byte encode(@Nullable CacheWriteSynchronizationMode mode) {
50-
if (mode == null)
51-
return -1;
52-
53-
switch (mode) {
38+
/** {@inheritDoc} */
39+
@Override protected byte code0(CacheWriteSynchronizationMode val) {
40+
switch (val) {
5441
case FULL_SYNC: return 0;
5542
case FULL_ASYNC: return 1;
5643
case PRIMARY_SYNC: return 2;
5744
}
5845

59-
throw new IllegalArgumentException("Unknown cache write synchronization mode: " + mode);
46+
throw new IllegalArgumentException("Unknown cache write synchronization mode: " + val);
6047
}
6148

62-
/** @param code Code of cache write synchronization mode to decode. */
63-
@Nullable private static CacheWriteSynchronizationMode decode(short code) {
49+
/** {@inheritDoc} */
50+
@Override protected CacheWriteSynchronizationMode value0(byte code) {
6451
switch (code) {
65-
case -1: return null;
6652
case 0: return CacheWriteSynchronizationMode.FULL_SYNC;
6753
case 1: return CacheWriteSynchronizationMode.FULL_ASYNC;
6854
case 2: return CacheWriteSynchronizationMode.PRIMARY_SYNC;
@@ -71,22 +57,6 @@ private static byte encode(@Nullable CacheWriteSynchronizationMode mode) {
7157
throw new IllegalArgumentException("Unknown cache write synchronization mode code: " + code);
7258
}
7359

74-
/** @param code Code of cache write synchronization mode. */
75-
public void code(byte code) {
76-
this.code = code;
77-
cacheWriteSyncMode = decode(code);
78-
}
79-
80-
/** @return Code of cache write synchronization mode. */
81-
public byte code() {
82-
return code;
83-
}
84-
85-
/** @return Cache write synchronization mode value. */
86-
public CacheWriteSynchronizationMode value() {
87-
return cacheWriteSyncMode;
88-
}
89-
9060
/** {@inheritDoc} */
9161
@Override public short directType() {
9262
return TYPE_CODE;
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.ignite.internal.managers.communication;
19+
20+
import org.apache.ignite.internal.Order;
21+
import org.apache.ignite.plugin.extensions.communication.Message;
22+
import org.jetbrains.annotations.Nullable;
23+
24+
/**
25+
* Superclass message for all enum message wrappers.
26+
* Consistency between code-to-value and value-to-code conversions must be provided.
27+
* @param <T> Type of wrapped enum.
28+
*/
29+
public abstract class EnumMessage<T extends Enum<T>> implements Message {
30+
/** Enum value. */
31+
private @Nullable T val;
32+
33+
/** Code of enum value. */
34+
@Order(0)
35+
private byte code = -1;
36+
37+
/**
38+
* Default constructor.
39+
*/
40+
protected EnumMessage() {
41+
// No-op.
42+
}
43+
44+
/**
45+
* @param val Value.
46+
*/
47+
protected EnumMessage(@Nullable T val) {
48+
this.val = val;
49+
code = code(val);
50+
}
51+
52+
/** @return Code. */
53+
public byte code() {
54+
return code;
55+
}
56+
57+
/** @param code Code. */
58+
public void code(byte code) {
59+
this.code = code;
60+
val = value(code);
61+
}
62+
63+
/**
64+
* @return Enum value.
65+
*/
66+
public @Nullable T value() {
67+
return val;
68+
}
69+
70+
/**
71+
* Determines, whether wrapped enum has specified value.
72+
*
73+
* @param otherVal Other value.
74+
*/
75+
public boolean is(Enum<T> otherVal) {
76+
return val == otherVal;
77+
}
78+
79+
/**
80+
* @param val Value.
81+
*
82+
* @return Code of specified value or <code>-1</code> for <code>null</code>.
83+
* @throws IllegalArgumentException If unexpeced value was passed.
84+
*/
85+
private byte code(@Nullable T val) {
86+
if (val == null)
87+
return -1;
88+
89+
return code0(val);
90+
}
91+
92+
/**
93+
* @param code Code.
94+
*
95+
* @return Value for code or null for <code>-1</code>.
96+
* @throws IllegalArgumentException If unexpeced code was passed.
97+
*/
98+
private @Nullable T value(byte code) {
99+
if (code == -1)
100+
return null;
101+
102+
return value0(code);
103+
}
104+
105+
/**
106+
* @param val Value.
107+
*
108+
* @return Code of specified value.
109+
* @throws IllegalArgumentException If unexpeced value was passed.
110+
*/
111+
protected abstract byte code0(T val);
112+
113+
/**
114+
* @param code Code.
115+
* @return Value for code.
116+
* @throws IllegalArgumentException If unexpeced code was passed.
117+
*/
118+
protected abstract T value0(byte code);
119+
}

modules/core/src/main/java/org/apache/ignite/internal/managers/communication/GridCacheOperationMessage.java

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,40 +16,27 @@
1616
*/
1717
package org.apache.ignite.internal.managers.communication;
1818

19-
import org.apache.ignite.internal.Order;
2019
import org.apache.ignite.internal.processors.cache.GridCacheOperation;
21-
import org.apache.ignite.plugin.extensions.communication.Message;
2220
import org.jetbrains.annotations.Nullable;
2321

2422
/** */
25-
public class GridCacheOperationMessage implements Message {
23+
public class GridCacheOperationMessage extends EnumMessage<GridCacheOperation> {
2624
/** Type code. */
2725
public static final short TYPE_CODE = 504;
2826

29-
/** Cache oparation. */
30-
@Nullable private GridCacheOperation cacheOperation;
31-
32-
/** Cache oparation code. */
33-
@Order(0)
34-
private byte code = -1;
35-
3627
/** Constructor. */
3728
public GridCacheOperationMessage() {
3829
// No-op.
3930
}
4031

4132
/** Constructor. */
42-
public GridCacheOperationMessage(@Nullable GridCacheOperation cacheOperation) {
43-
this.cacheOperation = cacheOperation;
44-
code = encode(cacheOperation);
33+
public GridCacheOperationMessage(@Nullable GridCacheOperation val) {
34+
super(val);
4535
}
4636

47-
/** @param operation Cache operation to encode. */
48-
private static byte encode(@Nullable GridCacheOperation operation) {
49-
if (operation == null)
50-
return -1;
51-
52-
switch (operation) {
37+
/** {@inheritDoc} */
38+
@Override protected byte code0(GridCacheOperation val) {
39+
switch (val) {
5340
case READ: return 0;
5441
case CREATE: return 1;
5542
case UPDATE: return 2;
@@ -59,13 +46,12 @@ private static byte encode(@Nullable GridCacheOperation operation) {
5946
case NOOP: return 6;
6047
}
6148

62-
throw new IllegalArgumentException("Unknown cache operation: " + operation);
49+
throw new IllegalArgumentException("Unknown cache operation: " + val);
6350
}
6451

65-
/** @param code Cache operation code to dencode to a cache operation value. */
66-
@Nullable private static GridCacheOperation decode(byte code) {
52+
/** {@inheritDoc} */
53+
@Override protected GridCacheOperation value0(byte code) {
6754
switch (code) {
68-
case -1: return null;
6955
case 0: return GridCacheOperation.READ;
7056
case 1: return GridCacheOperation.CREATE;
7157
case 2: return GridCacheOperation.UPDATE;
@@ -78,22 +64,6 @@ private static byte encode(@Nullable GridCacheOperation operation) {
7864
throw new IllegalArgumentException("Unknown cache operation code: " + code);
7965
}
8066

81-
/** @code Cache operation code. */
82-
public void code(byte code) {
83-
this.code = code;
84-
cacheOperation = decode(code);
85-
}
86-
87-
/** @return Cache operation code. */
88-
public byte code() {
89-
return code;
90-
}
91-
92-
/** @return Cache operation value. */
93-
@Nullable public GridCacheOperation value() {
94-
return cacheOperation;
95-
}
96-
9767
/** {@inheritDoc} */
9868
@Override public short directType() {
9969
return TYPE_CODE;

0 commit comments

Comments
 (0)