Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

ISPN-2028 Add Externalizer for Flag

  • Loading branch information...
commit 67c829385ff6a5bef5172afc3d411acca6e3d1d7 1 parent bb3ecf2
@galderz galderz authored
View
42 core/src/main/java/org/infinispan/context/Flag.java
@@ -22,6 +22,9 @@
*/
package org.infinispan.context;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;
@@ -31,6 +34,9 @@
import org.infinispan.AdvancedCache;
import org.infinispan.lifecycle.ComponentStatus;
import org.infinispan.loaders.CacheStore;
+import org.infinispan.marshall.AbstractExternalizer;
+import org.infinispan.marshall.Ids;
+import org.infinispan.util.Util;
/**
* Available flags, which may be set on a per-invocation basis. These are provided using the {@link AdvancedCache}
@@ -66,6 +72,11 @@
* <li> {@link #PUT_FOR_EXTERNAL_READ} - flags the invocation as a {@link Cache#putForExternalRead(Object, Object)}
* call, as opposed to a regular {@link Cache#put(Object, Object)}.</li>
* </ul>
+ *
+ * When making modifications to these enum, do not change the order of
+ * enumerations, so always append any new enumerations after the last one.
+ * Finally, enumerations should not be removed.
+ *
* @author Manik Surtani
* @author Galder Zamarreño
* @since 4.0
@@ -192,4 +203,35 @@
return flags;
}
}
+
+ public static class Externalizer extends AbstractExternalizer<Flag> {
+
+ @Override
+ public Integer getId() {
+ return Ids.FLAG;
+ }
+
+ @Override
+ public Set<Class<? extends Flag>> getTypeClasses() {
+ return Util.<Class<? extends Flag>>asSet(Flag.class);
+ }
+
+ @Override
+ public void writeObject(ObjectOutput output, Flag object) throws IOException {
+ output.writeByte(object.ordinal());
+ }
+
+ @Override
+ public Flag readObject(ObjectInput input) throws IOException, ClassNotFoundException {
+ byte flagByte = input.readByte();
+ try {
+ // values() cached by Class.getEnumConstants()
+ return Flag.values()[flagByte];
+ } catch (ArrayIndexOutOfBoundsException e) {
+ throw new IllegalStateException("Unknown flag index: " + flagByte);
+ }
+ }
+
+ }
+
}
View
2  core/src/main/java/org/infinispan/marshall/Ids.java
@@ -107,4 +107,6 @@
int VERSIONED_TRANSIENT_VALUE = 82;
int VERSIONED_TRANSIENT_MORTAL_VALUE = 83;
int LOCK_INFO = 84;
+
+ int FLAG = 85;
}
View
3  core/src/main/java/org/infinispan/marshall/jboss/ExternalizerTable.java
@@ -53,6 +53,7 @@
import org.infinispan.container.entries.versioned.VersionedTransientCacheValue;
import org.infinispan.container.entries.versioned.VersionedTransientMortalCacheEntry;
import org.infinispan.container.entries.versioned.VersionedTransientMortalCacheValue;
+import org.infinispan.context.Flag;
import org.infinispan.distribution.RemoteTransactionLogDetails;
import org.infinispan.distribution.ch.DefaultConsistentHash;
import org.infinispan.distribution.ch.TopologyAwareConsistentHash;
@@ -295,6 +296,8 @@ private void loadInternalMarshallables() {
addInternalExternalizer(new CacheView.Externalizer());
addInternalExternalizer(new LockInfo.Externalizer());
+
+ addInternalExternalizer(new Flag.Externalizer());
}
void addInternalExternalizer(AdvancedExternalizer<?> ext) {
View
4 core/src/test/java/org/infinispan/marshall/VersionAwareMarshallerTest.java
@@ -540,6 +540,10 @@ public int available() {
assertEquals("23", o);
}
+ public void testFlagMarshalling() throws Exception {
+ marshallAndAssertEquality(Arrays.asList(Flag.values()));
+ }
+
protected void marshallAndAssertEquality(Object writeObj) throws Exception {
byte[] bytes = marshaller.objectToByteBuffer(writeObj);
Object readObj = marshaller.objectFromByteBuffer(bytes);
Please sign in to comment.
Something went wrong with that request. Please try again.