Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,51 +18,37 @@
package org.apache.ignite.internal.managers.communication;

import org.apache.ignite.cache.CacheWriteSynchronizationMode;
import org.apache.ignite.internal.Order;
import org.apache.ignite.plugin.extensions.communication.Message;
import org.jetbrains.annotations.Nullable;

/** */
public class CacheWriteSynchronizationModeMessage implements Message {
public class CacheWriteSynchronizationModeMessage extends EnumMessage<CacheWriteSynchronizationMode> {
/** Type code. */
public static final short TYPE_CODE = 503;

/** Cache write synchronization mode value. */
@Nullable private CacheWriteSynchronizationMode cacheWriteSyncMode;

/** Code of cache write synchronization mode. */
@Order(0)
private byte code = -1;

/** Constructor. */
public CacheWriteSynchronizationModeMessage() {
// No-op.
}

/** Constructor. */
public CacheWriteSynchronizationModeMessage(@Nullable CacheWriteSynchronizationMode mode) {
cacheWriteSyncMode = mode;
code = encode(mode);
public CacheWriteSynchronizationModeMessage(@Nullable CacheWriteSynchronizationMode val) {
super(val);
}

/** @param mode Cache write synchronization mode to encode. */
private static byte encode(@Nullable CacheWriteSynchronizationMode mode) {
if (mode == null)
return -1;

switch (mode) {
/** {@inheritDoc} */
@Override protected byte code0(CacheWriteSynchronizationMode val) {
switch (val) {
case FULL_SYNC: return 0;
case FULL_ASYNC: return 1;
case PRIMARY_SYNC: return 2;
}

throw new IllegalArgumentException("Unknown cache write synchronization mode: " + mode);
throw new IllegalArgumentException("Unknown cache write synchronization mode: " + val);
}

/** @param code Code of cache write synchronization mode to decode. */
@Nullable private static CacheWriteSynchronizationMode decode(short code) {
/** {@inheritDoc} */
@Override protected CacheWriteSynchronizationMode value0(byte code) {
switch (code) {
case -1: return null;
case 0: return CacheWriteSynchronizationMode.FULL_SYNC;
case 1: return CacheWriteSynchronizationMode.FULL_ASYNC;
case 2: return CacheWriteSynchronizationMode.PRIMARY_SYNC;
Expand All @@ -71,22 +57,6 @@ private static byte encode(@Nullable CacheWriteSynchronizationMode mode) {
throw new IllegalArgumentException("Unknown cache write synchronization mode code: " + code);
}

/** @param code Code of cache write synchronization mode. */
public void code(byte code) {
this.code = code;
cacheWriteSyncMode = decode(code);
}

/** @return Code of cache write synchronization mode. */
public byte code() {
return code;
}

/** @return Cache write synchronization mode value. */
public CacheWriteSynchronizationMode value() {
return cacheWriteSyncMode;
}

/** {@inheritDoc} */
@Override public short directType() {
return TYPE_CODE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.ignite.internal.managers.communication;

import org.apache.ignite.internal.Order;
import org.apache.ignite.plugin.extensions.communication.Message;
import org.jetbrains.annotations.Nullable;

/**
* Superclass message for all enum message wrappers.
* Consistency between code-to-value and value-to-code conversions must be provided.
* @param <T> Type of wrapped enum.
*/
public abstract class EnumMessage<T extends Enum<T>> implements Message {
/** Enum value. */
private @Nullable T val;

/** Code of enum value. */
@Order(0)
private byte code = -1;

/**
* Default constructor.
*/
protected EnumMessage() {
// No-op.
}

/**
* @param val Value.
*/
protected EnumMessage(@Nullable T val) {
this.val = val;
code = code(val);
}

/** @return Code. */
public byte code() {
return code;
}

/** @param code Code. */
public void code(byte code) {
this.code = code;
val = value(code);
}

/**
* @return Enum value.
*/
public @Nullable T value() {
return val;
}

/**
* Determines, whether wrapped enum has specified value.
*
* @param otherVal Other value.
*/
public boolean is(Enum<T> otherVal) {
return val == otherVal;
}

/**
* @param val Value.
*
* @return Code of specified value or <code>-1</code> for <code>null</code>.
* @throws IllegalArgumentException If unexpeced value was passed.
*/
private byte code(@Nullable T val) {
if (val == null)
return -1;

return code0(val);
}

/**
* @param code Code.
*
* @return Value for code or null for <code>-1</code>.
* @throws IllegalArgumentException If unexpeced code was passed.
*/
private @Nullable T value(byte code) {
if (code == -1)
return null;

return value0(code);
}

/**
* @param val Value.
*
* @return Code of specified value.
* @throws IllegalArgumentException If unexpeced value was passed.
*/
protected abstract byte code0(T val);

/**
* @param code Code.
* @return Value for code.
* @throws IllegalArgumentException If unexpeced code was passed.
*/
protected abstract T value0(byte code);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,40 +16,27 @@
*/
package org.apache.ignite.internal.managers.communication;

import org.apache.ignite.internal.Order;
import org.apache.ignite.internal.processors.cache.GridCacheOperation;
import org.apache.ignite.plugin.extensions.communication.Message;
import org.jetbrains.annotations.Nullable;

/** */
public class GridCacheOperationMessage implements Message {
public class GridCacheOperationMessage extends EnumMessage<GridCacheOperation> {
/** Type code. */
public static final short TYPE_CODE = 504;

/** Cache oparation. */
@Nullable private GridCacheOperation cacheOperation;

/** Cache oparation code. */
@Order(0)
private byte code = -1;

/** Constructor. */
public GridCacheOperationMessage() {
// No-op.
}

/** Constructor. */
public GridCacheOperationMessage(@Nullable GridCacheOperation cacheOperation) {
this.cacheOperation = cacheOperation;
code = encode(cacheOperation);
public GridCacheOperationMessage(@Nullable GridCacheOperation val) {
super(val);
}

/** @param operation Cache operation to encode. */
private static byte encode(@Nullable GridCacheOperation operation) {
if (operation == null)
return -1;

switch (operation) {
/** {@inheritDoc} */
@Override protected byte code0(GridCacheOperation val) {
switch (val) {
case READ: return 0;
case CREATE: return 1;
case UPDATE: return 2;
Expand All @@ -59,13 +46,12 @@ private static byte encode(@Nullable GridCacheOperation operation) {
case NOOP: return 6;
}

throw new IllegalArgumentException("Unknown cache operation: " + operation);
throw new IllegalArgumentException("Unknown cache operation: " + val);
}

/** @param code Cache operation code to dencode to a cache operation value. */
@Nullable private static GridCacheOperation decode(byte code) {
/** {@inheritDoc} */
@Override protected GridCacheOperation value0(byte code) {
switch (code) {
case -1: return null;
case 0: return GridCacheOperation.READ;
case 1: return GridCacheOperation.CREATE;
case 2: return GridCacheOperation.UPDATE;
Expand All @@ -78,22 +64,6 @@ private static byte encode(@Nullable GridCacheOperation operation) {
throw new IllegalArgumentException("Unknown cache operation code: " + code);
}

/** @code Cache operation code. */
public void code(byte code) {
this.code = code;
cacheOperation = decode(code);
}

/** @return Cache operation code. */
public byte code() {
return code;
}

/** @return Cache operation value. */
@Nullable public GridCacheOperation value() {
return cacheOperation;
}

/** {@inheritDoc} */
@Override public short directType() {
return TYPE_CODE;
Expand Down
Loading
Loading