/** * @param binCtx Binary context. * @param ord Enum ordinal. * @param typeId Type Id. * @param clsName Class name. */ private void checkInstanceFromBytes(BinaryContext binCtx, int ord, int typeId, String clsName) throws IgniteCheckedException { BinaryEnumObjectImpl srcBinEnum =new BinaryEnumObjectImpl(binCtx, typeId, clsName, ord); Marshaller marsh = node1.configuration().getMarshaller(); byte[] bytes = marsh.marshal(srcBinEnum); BinaryEnumObjectImpl binEnum = new BinaryEnumObjectImpl(binCtx, bytes); assertEquals(clsName, binEnum.className()); assertEquals(typeId, binEnum.typeId()); assertEquals(ord, binEnum.enumOrdinal()); }
/** {@inheritDoc} */ @Override public int putValue(long addr) throws IgniteCheckedException { assert valBytes != null : "Value bytes must be initialized before object is stored"; return CacheObjectAdapter.putValue(addr, cacheObjectType(), valBytes, 0); }
/** {@inheritDoc} */ @Override public String toString() { if (!S.INCLUDE_SENSITIVE) return ord >= 0 ? "BinaryEnum" : "null"; // 1. Try deserializing the object. try { Object val = deserialize(); return new SB().a(val).toString(); } catch (Exception ignored) { // No-op. } // 2. Try getting meta. BinaryType type; try { type = rawType(); } catch (Exception ignored) { type = null; } if (type != null) return S.toString(type.typeName(), "ordinal", ord, true); else { if (typeId == GridBinaryMarshaller.UNREGISTERED_TYPE_ID) return S.toString("BinaryEnum", "clsName", clsName, true, "ordinal", ord, true); else return S.toString("BinaryEnum", "typeId", typeId, true, "ordinal", ord, true); } }
/** * @param val Value. */ void doWriteBinaryEnum(BinaryEnumObjectImpl val) { assert val != null; int typeId = val.typeId(); if (typeId != GridBinaryMarshaller.UNREGISTERED_TYPE_ID) { out.unsafeEnsure(1 + 4 + 4); out.unsafeWriteByte(GridBinaryMarshaller.BINARY_ENUM); out.unsafeWriteInt(typeId); out.unsafeWriteInt(val.enumOrdinal()); } else { out.unsafeEnsure(1 + 4); out.unsafeWriteByte(GridBinaryMarshaller.BINARY_ENUM); out.unsafeWriteInt(typeId); doWriteString(val.className()); out.writeInt(val.enumOrdinal()); } }
/** * Read binary enum. * * @param in Input stream. * @param ctx Binary context. * @param type Plain type. * @return Enum. */ private static BinaryEnumObjectImpl doReadBinaryEnum(BinaryInputStream in, BinaryContext ctx, EnumType type) { return new BinaryEnumObjectImpl(ctx, type.typeId, type.clsName, in.readInt()); }
/** * @param cls type to examine. * @return true if typeId equals for passed type and current * binary enum. */ public boolean isTypeEquals(final Class<?> cls) { return ctx.descriptorForClass(cls, false, false).typeId() == typeId(); } }
/** * Create binary type proxy. * * @param ctx Context. * @param obj Binary object. * @return Binary type proxy. */ public static BinaryType typeProxy(BinaryContext ctx, BinaryObjectEx obj) { if (ctx == null) throw new BinaryObjectException("BinaryContext is not set for the object."); String clsName = obj instanceof BinaryEnumObjectImpl ? ((BinaryEnumObjectImpl)obj).className() : null; return new BinaryTypeProxy(ctx, obj.typeId(), clsName); }
/** {@inheritDoc} */ @Override public boolean writeTo(ByteBuffer buf, MessageWriter writer) { writer.setBuffer(buf); if (!writer.isHeaderWritten()) { if (!writer.writeHeader(directType(), fieldsCount())) return false; writer.onHeaderWritten(); } switch (writer.state()) { case 0: if (!writer.writeString("clsName", clsName)) return false; writer.incrementState(); case 1: if (!writer.writeInt("ord", ord)) return false; writer.incrementState(); case 2: if (!writer.writeInt("typeId", typeId)) return false; writer.incrementState(); } return true; }
BinaryEnumObjectImpl val = (BinaryEnumObjectImpl)fieldVal; fieldVal = val.enumOrdinal();
/** {@inheritDoc} */ @Override public boolean putValue(ByteBuffer buf) throws IgniteCheckedException { assert valBytes != null : "Value bytes must be initialized before object is stored"; return putValue(buf, 0, objectPutSize(valBytes.length)); }
/** {@inheritDoc} */ @Nullable @Override public <T> T value(CacheObjectValueContext ctx, boolean cpy) { return deserialize(); }
/** * @param val Value. */ void doWriteBinaryEnum(BinaryEnumObjectImpl val) { assert val != null; int typeId = val.typeId(); if (typeId != GridBinaryMarshaller.UNREGISTERED_TYPE_ID) { out.unsafeEnsure(1 + 4 + 4); out.unsafeWriteByte(GridBinaryMarshaller.BINARY_ENUM); out.unsafeWriteInt(typeId); out.unsafeWriteInt(val.enumOrdinal()); } else { out.unsafeEnsure(1 + 4); out.unsafeWriteByte(GridBinaryMarshaller.BINARY_ENUM); out.unsafeWriteInt(typeId); doWriteString(val.className()); out.writeInt(val.enumOrdinal()); } }
/** {@inheritDoc} */ @Override public BinaryObject buildEnum(String typeName, int ord) { if (typeName == null || typeName.isEmpty()) throw new IllegalArgumentException("typeName"); BinaryContext ctx = marsh.context(); int typeId = ctx.typeId(typeName); return new BinaryEnumObjectImpl(ctx, typeId, null, ord); }
/** * @param cls type to examine. * @return true if typeId equals for passed type and current * binary enum. */ public boolean isTypeEquals(final Class<?> cls) { return ctx.descriptorForClass(cls, false, false).typeId() == typeId(); } }
/** * Create binary type proxy. * * @param ctx Context. * @param obj Binary object. * @return Binary type proxy. */ public static BinaryType typeProxy(BinaryContext ctx, BinaryObjectEx obj) { if (ctx == null) throw new BinaryObjectException("BinaryContext is not set for the object."); String clsName = obj instanceof BinaryEnumObjectImpl ? ((BinaryEnumObjectImpl)obj).className() : null; return new BinaryTypeProxy(ctx, obj.typeId(), clsName); }
/** {@inheritDoc} */ @Override public boolean writeTo(ByteBuffer buf, MessageWriter writer) { writer.setBuffer(buf); if (!writer.isHeaderWritten()) { if (!writer.writeHeader(directType(), fieldsCount())) return false; writer.onHeaderWritten(); } switch (writer.state()) { case 0: if (!writer.writeString("clsName", clsName)) return false; writer.incrementState(); case 1: if (!writer.writeInt("ord", ord)) return false; writer.incrementState(); case 2: if (!writer.writeInt("typeId", typeId)) return false; writer.incrementState(); } return true; }
BinaryEnumObjectImpl val = (BinaryEnumObjectImpl)fieldVal; fieldVal = val.enumOrdinal();
/** {@inheritDoc} */ @Override public boolean putValue(ByteBuffer buf) throws IgniteCheckedException { assert valBytes != null : "Value bytes must be initialized before object is stored"; return putValue(buf, 0, objectPutSize(valBytes.length)); }
/** {@inheritDoc} */ @Nullable @Override public <T> T value(CacheObjectValueContext ctx, boolean cpy) { return deserialize(); }
/** {@inheritDoc} */ @Override public Collection<BinaryObject> enumValues() { Collection<Integer> ordinals = meta.enumMap().values(); ArrayList<BinaryObject> enumValues = new ArrayList<>(ordinals.size()); for (Integer ord: ordinals) enumValues.add(new BinaryEnumObjectImpl(ctx, typeId(), typeName(), ord)); return enumValues; }