repo
stringlengths
1
191
file
stringlengths
23
351
code
stringlengths
0
5.32M
file_length
int64
0
5.32M
avg_line_length
float64
0
2.9k
max_line_length
int64
0
288k
extension_type
stringclasses
1 value
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/TypedObjectMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import org.infinispan.protostream.descriptors.WireType; /** * Marshaller for a typed object. * @author Paul Ferraro */ public class TypedObjectMarshaller implements FieldMarshaller<Object> { private final ScalarMarshaller<Class<?>> type; public TypedObjectMarshaller(ScalarMarshaller<Class<?>> typeValue) { this.type = typeValue; } @Override public Object readFrom(ProtoStreamReader reader) throws IOException { Class<?> targetClass = this.type.readFrom(reader); Object result = null; while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); if (index == AnyField.ANY.getIndex()) { result = reader.readObject(targetClass); } else { reader.skipField(tag); } } return result; } @Override public void writeTo(ProtoStreamWriter writer, Object value) throws IOException { this.type.writeTo(writer, value.getClass()); writer.writeObject(AnyField.ANY.getIndex(), value); } @Override public Class<? extends Object> getJavaClass() { return Object.class; } @Override public WireType getWireType() { return this.type.getWireType(); } }
2,421
32.178082
84
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/FunctionalMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import java.util.function.Function; import org.wildfly.common.function.ExceptionFunction; /** * Marshaller that uses a functional transformation of another marshaller. * @author Paul Ferraro * @param <T> the type of this marshaller * @param <V> the type of the mapped marshaller */ public class FunctionalMarshaller<T, V> implements ProtoStreamMarshaller<T> { private final Class<T> targetClass; private final Function<ProtoStreamOperation, ProtoStreamMarshaller<V>> marshallerFactory; private final ExceptionFunction<T, V, IOException> function; private final ExceptionFunction<V, T, IOException> factory; public FunctionalMarshaller(Class<T> targetClass, Class<V> sourceClass, ExceptionFunction<T, V, IOException> function, ExceptionFunction<V, T, IOException> factory) { this(targetClass, operation -> operation.findMarshaller(sourceClass), function, factory); } public FunctionalMarshaller(Class<T> targetClass, ProtoStreamMarshaller<V> marshaller, ExceptionFunction<T, V, IOException> function, ExceptionFunction<V, T, IOException> factory) { this(targetClass, operation -> marshaller, function, factory); } public FunctionalMarshaller(Class<T> targetClass, Function<ProtoStreamOperation, ProtoStreamMarshaller<V>> marshallerFactory, ExceptionFunction<T, V, IOException> function, ExceptionFunction<V, T, IOException> factory) { this.targetClass = targetClass; this.marshallerFactory = marshallerFactory; this.function = function; this.factory = factory; } @Override public T readFrom(ProtoStreamReader reader) throws IOException { ProtoStreamMarshaller<V> marshaller = this.marshallerFactory.apply(reader); V value = marshaller.readFrom(reader); return this.factory.apply(value); } @Override public void writeTo(ProtoStreamWriter writer, T object) throws IOException { V value = this.function.apply(object); ProtoStreamMarshaller<V> marshaller = this.marshallerFactory.apply(writer); marshaller.writeTo(writer, value); } @Override public Class<? extends T> getJavaClass() { return this.targetClass; } }
3,312
42.025974
224
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/Scalar.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import org.infinispan.protostream.descriptors.WireType; /** * Enumeration of common scalar marshaller implementations. * @author Paul Ferraro */ public enum Scalar implements ScalarMarshallerProvider { ANY(new ScalarMarshaller<Object>() { @Override public Object readFrom(ProtoStreamReader reader) throws IOException { return reader.readAny(); } @Override public void writeTo(ProtoStreamWriter writer, Object value) throws IOException { writer.writeAnyNoTag(value); } @Override public Class<? extends Object> getJavaClass() { return Object.class; } @Override public WireType getWireType() { return WireType.LENGTH_DELIMITED; } }), BOOLEAN(new ScalarMarshaller<Boolean>() { @Override public Boolean readFrom(ProtoStreamReader reader) throws IOException { return reader.readUInt32() != 0; } @Override public void writeTo(ProtoStreamWriter writer, Boolean value) throws IOException { writer.writeVarint32(value ? 1 : 0); } @Override public Class<? extends Boolean> getJavaClass() { return Boolean.class; } @Override public WireType getWireType() { return WireType.VARINT; } }), BYTE(new ScalarMarshaller<Byte>() { @Override public Byte readFrom(ProtoStreamReader reader) throws IOException { return (byte) reader.readSInt32(); } @Override public void writeTo(ProtoStreamWriter writer, Byte value) throws IOException { // Roll the bits in order to move the sign bit from position 31 to position 0, to reduce the wire length of negative numbers. // See https://developers.google.com/protocol-buffers/docs/encoding writer.writeVarint32((value << 1) ^ (value >> (Integer.SIZE - 1))); } @Override public Class<? extends Byte> getJavaClass() { return Byte.class; } @Override public WireType getWireType() { return WireType.VARINT; } }), SHORT(new ScalarMarshaller<Short>() { @Override public Short readFrom(ProtoStreamReader reader) throws IOException { return (short) reader.readSInt32(); } @Override public void writeTo(ProtoStreamWriter writer, Short value) throws IOException { // Roll the bits in order to move the sign bit from position 31 to position 0, to reduce the wire length of negative numbers. // See https://developers.google.com/protocol-buffers/docs/encoding writer.writeVarint32((value << 1) ^ (value >> (Integer.SIZE - 1))); } @Override public Class<? extends Short> getJavaClass() { return Short.class; } @Override public WireType getWireType() { return WireType.VARINT; } }), INTEGER(new ScalarMarshaller<Integer>() { @Override public Integer readFrom(ProtoStreamReader reader) throws IOException { return reader.readSInt32(); } @Override public void writeTo(ProtoStreamWriter writer, Integer value) throws IOException { // Roll the bits in order to move the sign bit from position 31 to position 0, to reduce the wire length of negative numbers. // See https://developers.google.com/protocol-buffers/docs/encoding writer.writeVarint32((value << 1) ^ (value >> (Integer.SIZE - 1))); } @Override public Class<? extends Integer> getJavaClass() { return Integer.class; } @Override public WireType getWireType() { return WireType.VARINT; } }), LONG(new ScalarMarshaller<Long>() { @Override public Long readFrom(ProtoStreamReader reader) throws IOException { return reader.readSInt64(); } @Override public void writeTo(ProtoStreamWriter writer, Long value) throws IOException { // Roll the bits in order to move the sign bit from position 63 to position 0, to reduce the wire length of negative numbers. // See https://developers.google.com/protocol-buffers/docs/encoding writer.writeVarint64((value << 1) ^ (value >> (Long.SIZE - 1))); } @Override public Class<? extends Long> getJavaClass() { return Long.class; } @Override public WireType getWireType() { return WireType.VARINT; } }), FLOAT(new ScalarMarshaller<Float>() { @Override public Float readFrom(ProtoStreamReader reader) throws IOException { return reader.readFloat(); } @Override public void writeTo(ProtoStreamWriter writer, Float value) throws IOException { int bits = Float.floatToRawIntBits(value); byte[] bytes = new byte[Float.BYTES]; for (int i = 0; i < bytes.length; ++i) { int index = i * Byte.SIZE; bytes[i] = (byte) ((bits >> index) & 0xFF); } writer.writeRawBytes(bytes, 0, bytes.length); } @Override public Class<? extends Float> getJavaClass() { return Float.class; } @Override public WireType getWireType() { return WireType.FIXED32; } }), DOUBLE(new ScalarMarshaller<Double>() { @Override public Double readFrom(ProtoStreamReader reader) throws IOException { return reader.readDouble(); } @Override public void writeTo(ProtoStreamWriter writer, Double value) throws IOException { long bits = Double.doubleToRawLongBits(value); byte[] bytes = new byte[Double.BYTES]; for (int i = 0; i < bytes.length; ++i) { int index = i * Byte.SIZE; if (index < Integer.SIZE) { bytes[i] = (byte) ((bits >> index) & 0xFF); } else { bytes[i] = (byte) ((int) (bits >> index) & 0xFF); } } writer.writeRawBytes(bytes, 0, bytes.length); } @Override public Class<? extends Double> getJavaClass() { return Double.class; } @Override public WireType getWireType() { return WireType.FIXED64; } }), CHARACTER(new ScalarMarshaller<Character>() { @Override public Character readFrom(ProtoStreamReader reader) throws IOException { return (char) reader.readUInt32(); } @Override public void writeTo(ProtoStreamWriter writer, Character value) throws IOException { writer.writeVarint32(value); } @Override public Class<? extends Character> getJavaClass() { return Character.class; } @Override public WireType getWireType() { return WireType.VARINT; } }), BYTE_BUFFER(new ScalarMarshaller<ByteBuffer>() { @Override public ByteBuffer readFrom(ProtoStreamReader reader) throws IOException { return reader.readByteBuffer(); } @Override public void writeTo(ProtoStreamWriter writer, ByteBuffer buffer) throws IOException { if (buffer.hasArray()) { int length = buffer.remaining(); writer.writeVarint32(length); writer.writeRawBytes(buffer.array(), buffer.arrayOffset(), length); } else { // Don't mess with existing position ByteBuffer copy = buffer.asReadOnlyBuffer(); int length = copy.remaining(); writer.writeVarint32(length); byte[] bytes = new byte[length]; copy.get(bytes, copy.position(), length); writer.writeRawBytes(bytes, 0, length); } } @Override public Class<? extends ByteBuffer> getJavaClass() { return ByteBuffer.class; } @Override public WireType getWireType() { return WireType.LENGTH_DELIMITED; } }), BYTE_ARRAY(new ScalarMarshaller<byte[]>() { @Override public byte[] readFrom(ProtoStreamReader reader) throws IOException { return reader.readByteArray(); } @Override public void writeTo(ProtoStreamWriter writer, byte[] value) throws IOException { writer.writeVarint32(value.length); writer.writeRawBytes(value, 0, value.length); } @Override public Class<? extends byte[]> getJavaClass() { return byte[].class; } @Override public WireType getWireType() { return WireType.LENGTH_DELIMITED; } }), STRING(new ScalarMarshaller<String>() { @Override public String readFrom(ProtoStreamReader reader) throws IOException { return StandardCharsets.UTF_8.decode(reader.readByteBuffer()).toString(); } @Override public void writeTo(ProtoStreamWriter writer, String value) throws IOException { BYTE_BUFFER.writeTo(writer, StandardCharsets.UTF_8.encode(value)); } @Override public Class<? extends String> getJavaClass() { return String.class; } @Override public WireType getWireType() { return WireType.LENGTH_DELIMITED; } }), REFERENCE(new ScalarMarshaller<Reference>() { @Override public Reference readFrom(ProtoStreamReader reader) throws IOException { return new Reference(reader.readUInt32()); } @Override public void writeTo(ProtoStreamWriter writer, Reference value) throws IOException { writer.writeVarint32(value.getAsInt()); } @Override public Class<? extends Reference> getJavaClass() { return Reference.class; } @Override public WireType getWireType() { return WireType.VARINT; } }), ; private final ScalarMarshaller<?> marshaller; Scalar(ScalarMarshaller<?> marshaller) { this.marshaller = marshaller; } @Override public ScalarMarshaller<?> getMarshaller() { return this.marshaller; } }
11,826
31.94429
137
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamBuilderFieldSetMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; /** * Marshaller for an object whose fields are marshalled via a {@link FieldSetMarshaller} and constructed via a {@link ProtoStreamBuilder}. * @author Paul Ferraro * @param <T> the type of this marshaller * @param <B> the builder type used by this marshaller */ public class ProtoStreamBuilderFieldSetMarshaller<T, B extends ProtoStreamBuilder<T>> extends FunctionalFieldSetMarshaller<T, B> { @SuppressWarnings("unchecked") public ProtoStreamBuilderFieldSetMarshaller(FieldSetMarshaller<T, B> marshaller) { super((Class<T>) marshaller.getBuilder().build().getClass(), marshaller, B::build); } public ProtoStreamBuilderFieldSetMarshaller(Class<? extends T> targetClass, FieldSetMarshaller<T, B> marshaller) { super(targetClass, marshaller, B::build); } }
1,879
43.761905
138
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ClassLoaderMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; /** * A marshaller for the fields of a {@link ClassLoader}. * @author Paul Ferraro */ public interface ClassLoaderMarshaller extends FieldSetMarshaller<ClassLoader, ClassLoader> { }
1,266
39.870968
93
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/StackTraceElementMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import org.infinispan.protostream.descriptors.WireType; /** * @author Paul Ferraro */ public enum StackTraceElementMarshaller implements ProtoStreamMarshaller<StackTraceElement> { INSTANCE; private static final int CLASS_NAME_INDEX = 1; private static final int METHOD_NAME_INDEX = 2; private static final int FILE_NAME_INDEX = 3; private static final int LINE_NUMBER_INDEX = 4; private static final int CLASS_LOADER_NAME_INDEX = 5; private static final int MODULE_NAME_INDEX = 6; private static final int MODULE_VERSION_INDEX = 7; @Override public StackTraceElement readFrom(ProtoStreamReader reader) throws IOException { String className = null; String methodName = null; String fileName = null; int line = -1; String classLoaderName = null; String moduleName = null; String moduleVersion = null; while (!reader.isAtEnd()) { int tag = reader.readTag(); switch (WireType.getTagFieldNumber(tag)) { case CLASS_NAME_INDEX: className = reader.readAny(String.class); break; case METHOD_NAME_INDEX: methodName = reader.readAny(String.class); break; case FILE_NAME_INDEX: fileName = reader.readAny(String.class); break; case LINE_NUMBER_INDEX: line = reader.readUInt32(); if (line == 0) { // Native method line = -2; } break; case CLASS_LOADER_NAME_INDEX: classLoaderName = reader.readAny(String.class); break; case MODULE_NAME_INDEX: moduleName = reader.readAny(String.class); break; case MODULE_VERSION_INDEX: moduleVersion = reader.readAny(String.class); break; default: reader.skipField(tag); } } return new StackTraceElement(classLoaderName, moduleName, moduleVersion, className, methodName, fileName, line); } @Override public void writeTo(ProtoStreamWriter writer, StackTraceElement element) throws IOException { writer.writeAny(CLASS_NAME_INDEX, element.getClassName()); writer.writeAny(METHOD_NAME_INDEX, element.getMethodName()); String fileName = element.getFileName(); if (fileName != null) { writer.writeAny(FILE_NAME_INDEX, fileName); } int line = element.getLineNumber(); boolean nativeMethod = element.isNativeMethod(); if (nativeMethod || line > 0) { writer.writeUInt32(LINE_NUMBER_INDEX, nativeMethod ? 0 : line); } String classLoaderName = element.getClassLoaderName(); if (classLoaderName != null) { writer.writeAny(CLASS_LOADER_NAME_INDEX, classLoaderName); } String moduleName = element.getModuleName(); if (moduleName != null) { writer.writeAny(MODULE_NAME_INDEX, moduleName); } String moduleVersion = element.getModuleVersion(); if (moduleVersion != null) { writer.writeAny(MODULE_VERSION_INDEX, moduleVersion); } } @Override public Class<? extends StackTraceElement> getJavaClass() { return StackTraceElement.class; } }
4,668
38.235294
120
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/EnumMarshallerAdapter.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; /** * Adapts a {@link org.infinispan.protostream.EnumMarshaller} to a {@link ProtoStreamMarshaller}. * @author Paul Ferraro */ public class EnumMarshallerAdapter<E extends Enum<E>> extends EnumMarshaller<E> { private final String typeName; @SuppressWarnings("unchecked") public EnumMarshallerAdapter(org.infinispan.protostream.EnumMarshaller<E> marshaller) { super((Class<E>) marshaller.getJavaClass()); this.typeName = marshaller.getTypeName(); } @Override public String getTypeName() { return this.typeName; } }
1,653
36.590909
97
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ScalarMarshallerProvider.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import java.util.OptionalInt; import org.infinispan.protostream.descriptors.WireType; /** * Provider for a {@link ScalarMarshaller}. * @author Paul Ferraro */ public interface ScalarMarshallerProvider extends ScalarMarshaller<Object> { ScalarMarshaller<?> getMarshaller(); @Override default Class<? extends Object> getJavaClass() { return this.getMarshaller().getJavaClass(); } @Override default WireType getWireType() { return this.getMarshaller().getWireType(); } @Override default Object readFrom(ProtoStreamReader reader) throws IOException { return this.getMarshaller().readFrom(reader); } @Override default void writeTo(ProtoStreamWriter writer, Object value) throws IOException { this.cast(Object.class).writeTo(writer, value); } @Override default OptionalInt size(ProtoStreamSizeOperation operation, Object value) { return this.cast(Object.class).size(operation, value); } @SuppressWarnings("unchecked") default <T> ScalarMarshaller<T> cast(Class<T> type) { if (!type.isAssignableFrom(this.getJavaClass())) { throw new IllegalArgumentException(type.getName()); } return (ScalarMarshaller<T>) this.getMarshaller(); } }
2,398
33.271429
85
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/LoadedClassField.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import java.io.InvalidClassException; import org.infinispan.protostream.descriptors.WireType; import org.wildfly.security.manager.WildFlySecurityManager; /** * A class field that marshals instances of {@link Class} using a {@link ClassLoaderMarshaller}. * @author Paul Ferraro */ public class LoadedClassField implements Field<Class<?>>, FieldMarshaller<Class<?>> { private final ClassLoaderMarshaller loaderMarshaller; private final int index; private final int loaderIndex; public LoadedClassField(ClassLoaderMarshaller loaderMarshaller, int index) { this.loaderMarshaller = loaderMarshaller; this.index = index; this.loaderIndex = index + 1; } @Override public FieldMarshaller<Class<?>> getMarshaller() { return this; } @Override public Class<?> readFrom(ProtoStreamReader reader) throws IOException { String className = Scalar.STRING.cast(String.class).readFrom(reader); ClassLoader loader = this.loaderMarshaller.getBuilder(); while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); if ((index >= this.loaderIndex) && (index < this.loaderIndex + this.loaderMarshaller.getFields())) { loader = this.loaderMarshaller.readField(reader, index - this.loaderIndex, loader); } else { reader.skipField(tag); } } try { return loader.loadClass(className); } catch (ClassNotFoundException e) { InvalidClassException exception = new InvalidClassException(e.getLocalizedMessage()); exception.initCause(e); throw exception; } } @Override public void writeTo(ProtoStreamWriter writer, Class<?> targetClass) throws IOException { Scalar.STRING.writeTo(writer, targetClass.getName()); this.loaderMarshaller.writeFields(writer, this.loaderIndex, WildFlySecurityManager.getClassLoaderPrivileged(targetClass)); } @Override public Class<? extends Class<?>> getJavaClass() { return ScalarClass.ANY.getJavaClass(); } @Override public int getIndex() { return this.index; } @Override public WireType getWireType() { return WireType.LENGTH_DELIMITED; } }
3,460
35.431579
130
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/DefaultSerializationContextInitializerProvider.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import org.infinispan.protostream.SerializationContextInitializer; import org.wildfly.clustering.marshalling.protostream.util.UtilMarshallerProvider; import org.wildfly.clustering.marshalling.protostream.util.concurrent.ConcurrentMarshallerProvider; import org.wildfly.clustering.marshalling.protostream.util.concurrent.atomic.AtomicMarshallerProvider; import org.wildfly.clustering.marshalling.protostream.math.MathMarshallerProvider; import org.wildfly.clustering.marshalling.protostream.net.NetMarshallerProvider; import org.wildfly.clustering.marshalling.protostream.sql.SQLMarshallerProvider; import org.wildfly.clustering.marshalling.protostream.time.TimeMarshallerProvider; /** * @author Paul Ferraro */ public enum DefaultSerializationContextInitializerProvider implements SerializationContextInitializerProvider { ANY(new AnySerializationContextInitializer()), MATH(new ProviderSerializationContextInitializer<>("java.math.proto", MathMarshallerProvider.class)), NET(new ProviderSerializationContextInitializer<>("java.net.proto", NetMarshallerProvider.class)), SQL(new ProviderSerializationContextInitializer<>("java.sql.proto", SQLMarshallerProvider.class)), TIME(new ProviderSerializationContextInitializer<>("java.time.proto", TimeMarshallerProvider.class)), UTIL(new ProviderSerializationContextInitializer<>("java.util.proto", UtilMarshallerProvider.class)), ATOMIC(new ProviderSerializationContextInitializer<>("java.util.concurrent.atomic.proto", AtomicMarshallerProvider.class)), CONCURRENT(new ProviderSerializationContextInitializer<>("java.util.concurrent.proto", ConcurrentMarshallerProvider.class)), MARSHALLING(new ProviderSerializationContextInitializer<>("org.wildfly.clustering.marshalling.spi.proto", MarshallingMarshallerProvider.class)), ; private final SerializationContextInitializer initializer; DefaultSerializationContextInitializerProvider(SerializationContextInitializer initializer) { this.initializer = initializer; } @Override public SerializationContextInitializer getInitializer() { return this.initializer; } }
3,220
53.59322
148
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ExceptionMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.LinkedList; import java.util.List; import org.infinispan.protostream.descriptors.WireType; /** * Generic marshaller for a Throwable. * @author Paul Ferraro * @param <E> the target type of this marshaller */ public class ExceptionMarshaller<E extends Throwable> implements ProtoStreamMarshaller<E> { private static final int CLASS_INDEX = 1; private static final int MESSAGE_INDEX = 2; private static final int CAUSE_INDEX = 3; private static final int STACK_TRACE_ELEMENT_INDEX = 4; private static final int SUPPRESSED_INDEX = 5; private final Class<E> exceptionClass; public ExceptionMarshaller(Class<E> exceptionClass) { this.exceptionClass = exceptionClass; } @Override public Class<? extends E> getJavaClass() { return this.exceptionClass; } @Override public E readFrom(ProtoStreamReader reader) throws IOException { Class<E> exceptionClass = this.exceptionClass; String message = null; Throwable cause = null; List<StackTraceElement> stackTrace = new LinkedList<>(); List<Throwable> suppressed = new LinkedList<>(); while (!reader.isAtEnd()) { int tag = reader.readTag(); switch (WireType.getTagFieldNumber(tag)) { case CLASS_INDEX: exceptionClass = reader.readObject(Class.class); break; case MESSAGE_INDEX: message = reader.readString(); break; case CAUSE_INDEX: cause = reader.readAny(Throwable.class); break; case STACK_TRACE_ELEMENT_INDEX: stackTrace.add(reader.readObject(StackTraceElement.class)); break; case SUPPRESSED_INDEX: suppressed.add(reader.readAny(Throwable.class)); break; default: reader.skipField(tag); } } E exception = this.createException(exceptionClass, message, cause); if (!stackTrace.isEmpty()) { exception.setStackTrace(stackTrace.toArray(new StackTraceElement[0])); } for (Throwable e : suppressed) { exception.addSuppressed(e); } return exception; } @Override public void writeTo(ProtoStreamWriter writer, E exception) throws IOException { if (this.exceptionClass == Throwable.class) { writer.writeObject(CLASS_INDEX, exception.getClass()); } String message = exception.getMessage(); Throwable cause = exception.getCause(); // Avoid serializing redundant message if ((message != null) && ((cause == null) || !cause.toString().equals(message))) { writer.writeString(MESSAGE_INDEX, message); } if (cause != null) { writer.writeAny(CAUSE_INDEX, cause); } for (StackTraceElement element : exception.getStackTrace()) { writer.writeObject(STACK_TRACE_ELEMENT_INDEX, element); } for (Throwable suppressed : exception.getSuppressed()) { writer.writeAny(SUPPRESSED_INDEX, suppressed); } } private E createException(Class<E> exceptionClass, String message, Throwable cause) throws IOException { Constructor<E> emptyConstructor = this.getConstructor(exceptionClass); Constructor<E> messageConstructor = this.getConstructor(exceptionClass, String.class); Constructor<E> causeConstructor = this.getConstructor(exceptionClass, Throwable.class); Constructor<E> messageCauseConstructor = this.getConstructor(exceptionClass, String.class, Throwable.class); try { if (cause != null) { if (message != null) { if (messageCauseConstructor != null) { return messageCauseConstructor.newInstance(message, cause); } } else { if (causeConstructor != null) { return causeConstructor.newInstance(cause); } } } E exception = (message != null) ? ((messageConstructor != null) ? messageConstructor.newInstance(message) : null) : ((emptyConstructor != null) ? emptyConstructor.newInstance() : null); if (exception == null) { throw new NoSuchMethodException(String.format("%s(%s)", exceptionClass.getName(), (message != null) ? String.class.getName() : "")); } if (cause != null) { exception.initCause(cause); } return exception; } catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) { throw new IOException(e); } } private Constructor<E> getConstructor(Class<E> exceptionClass, Class<?>... parameterTypes) { try { return exceptionClass.getConstructor(parameterTypes); } catch (NoSuchMethodException e) { return null; } } }
6,378
39.891026
197
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ClassField.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import java.lang.reflect.Array; import org.infinispan.protostream.descriptors.WireType; /** * Various strategies for marshalling a Class. * @author Paul Ferraro */ public enum ClassField implements Field<Class<?>> { ANY(ScalarClass.ANY), ID(ScalarClass.ID), NAME(ScalarClass.NAME), FIELD(ScalarClass.FIELD), ARRAY(new FieldMarshaller<Class<?>>() { @Override public Class<?> readFrom(ProtoStreamReader reader) throws IOException { int dimensions = reader.readUInt32(); Class<?> componentClass = Object.class; while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); if (index == ANY.getIndex()) { componentClass = ScalarClass.ANY.readFrom(reader); } else { reader.skipField(tag); } } for (int i = 0; i < dimensions; ++i) { componentClass = Array.newInstance(componentClass, 0).getClass(); } return componentClass; } @Override public void writeTo(ProtoStreamWriter writer, Class<?> targetClass) throws IOException { int dimensions = 0; Class<?> componentClass = targetClass; while (componentClass.isArray() && !componentClass.getComponentType().isPrimitive()) { componentClass = componentClass.getComponentType(); dimensions += 1; } writer.writeVarint32(dimensions); if (componentClass != Object.class) { writer.writeTag(ANY.getIndex(), ANY.getMarshaller().getWireType()); ScalarClass.ANY.writeTo(writer, componentClass); } } @Override public Class<? extends Class<?>> getJavaClass() { return ScalarClass.ANY.getJavaClass(); } @Override public WireType getWireType() { return WireType.VARINT; } }), ; private final FieldMarshaller<Class<?>> marshaller; ClassField(ScalarMarshaller<Class<?>> value) { this(new ScalarFieldMarshaller<>(value)); } ClassField(FieldMarshaller<Class<?>> marshaller) { this.marshaller = marshaller; } @Override public int getIndex() { return this.ordinal() + 1; } @Override public FieldMarshaller<Class<?>> getMarshaller() { return this.marshaller; } private static final ClassField[] FIELDS = values(); static ClassField fromIndex(int index) { return (index > 0) && (index <= FIELDS.length) ? FIELDS[index - 1] : null; } }
3,826
33.790909
98
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/AbstractSerializationContextInitializer.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import java.io.UncheckedIOException; import java.security.PrivilegedAction; import org.infinispan.protostream.FileDescriptorSource; import org.infinispan.protostream.SerializationContext; import org.infinispan.protostream.SerializationContextInitializer; import org.wildfly.security.manager.WildFlySecurityManager; /** * @author Paul Ferraro */ public abstract class AbstractSerializationContextInitializer implements SerializationContextInitializer, PrivilegedAction<FileDescriptorSource> { private final String resourceName; private final ClassLoader loader; protected AbstractSerializationContextInitializer() { this(null); } protected AbstractSerializationContextInitializer(String resourceName) { this(resourceName, null); } protected AbstractSerializationContextInitializer(String resourceName, ClassLoader loader) { this.resourceName = (resourceName == null) ? this.getClass().getPackage().getName() + ".proto" : resourceName; this.loader = (loader == null) ? WildFlySecurityManager.getClassLoaderPrivileged(this.getClass()) : loader; } @Deprecated @Override public final String getProtoFileName() { return null; } @Deprecated @Override public final String getProtoFile() { return null; } @Override public void registerSchema(SerializationContext context) { context.registerProtoFiles(WildFlySecurityManager.doUnchecked(this)); } @Override public FileDescriptorSource run() { try { return FileDescriptorSource.fromResources(this.loader, this.resourceName); } catch (IOException e) { throw new UncheckedIOException(e); } } @Override public String toString() { return this.resourceName; } }
2,930
33.081395
146
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ValueMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import java.util.function.Supplier; import org.wildfly.common.function.Functions; /** * ProtoStream marshaller for fixed values. * @author Paul Ferraro * @param <T> the type of this marshaller */ public class ValueMarshaller<T> implements ProtoStreamMarshaller<T> { private final Class<T> targetClass; private final Supplier<T> factory; public ValueMarshaller(T value) { this(Functions.constantSupplier(value)); } @SuppressWarnings("unchecked") public ValueMarshaller(Supplier<T> factory) { this.targetClass = (Class<T>) factory.get().getClass(); this.factory = factory; } @Override public T readFrom(ProtoStreamReader reader) throws IOException { while (!reader.isAtEnd()) { int tag = reader.readTag(); reader.skipField(tag); } return this.factory.get(); } @Override public void writeTo(ProtoStreamWriter writer, T value) { // Nothing to write } @Override public Class<? extends T> getJavaClass() { return this.targetClass; } }
2,202
30.927536
70
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/SimpleClassLoaderMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; /** * @author Paul Ferraro */ public class SimpleClassLoaderMarshaller implements ClassLoaderMarshaller { private final ClassLoader loader; public SimpleClassLoaderMarshaller(ClassLoader loader) { this.loader = loader; } @Override public ClassLoader getBuilder() { return this.loader; } @Override public int getFields() { return 0; } @Override public ClassLoader readField(ProtoStreamReader reader, int index, ClassLoader loader) throws IOException { return loader; } @Override public void writeFields(ProtoStreamWriter writer, int startIndex, ClassLoader value) throws IOException { } }
1,798
30.561404
110
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamReader.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import org.infinispan.protostream.TagReader; /** * A {@link TagReader} with the additional ability to read an arbitrary embedded object. * @author Paul Ferraro */ public interface ProtoStreamReader extends ProtoStreamOperation, TagReader { /** * Reads an object of an arbitrary type from this reader. * @return a supplier of the unmarshalled object * @throws IOException if the object could not be read with the associated marshaller. */ Object readAny() throws IOException; /** * Reads an object of an arbitrary type from this reader, cast to the specified type. * @param the expected type * @return a supplier of the unmarshalled object * @throws IOException if the object could not be read with the associated marshaller. */ default <T> T readAny(Class<T> targetClass) throws IOException { return targetClass.cast(this.readAny()); } /** * Reads an object of the specified type from this reader. * @param <T> the type of the associated marshaller * @param targetClass the class of the associated marshaller * @return the unmarshalled object * @throws IOException if no marshaller is associated with the specified class, or if the object could not be read with the associated marshaller. */ <T> T readObject(Class<T> targetClass) throws IOException; /** * Reads an num of the specified type from this reader. * @param <T> the type of the associated marshaller * @param targetClass the class of the associated marshaller * @return the unmarshalled enum * @throws IOException if no marshaller is associated with the specified enum class, or if the enum could not be read with the associated marshaller. */ default <E extends Enum<E>> E readEnum(Class<E> enumClass) throws IOException { EnumMarshaller<E> marshaller = (EnumMarshaller<E>) this.getSerializationContext().getMarshaller(enumClass); int code = this.readEnum(); return marshaller.decode(code); } /** * Deprecated to discourage use. * @deprecated Use {@link #readUInt32()} or {@link #readSInt32()} */ @Deprecated @Override int readInt32() throws IOException; /** * Deprecated to discourage use. * @deprecated Use {@link #readSFixed32()} instead. */ @Deprecated @Override int readFixed32() throws IOException; /** * Deprecated to discourage use. * @deprecated Use {@link #readUInt64()} or {@link #readSInt64()} */ @Deprecated @Override long readInt64() throws IOException; /** * Deprecated to discourage use. * @deprecated Use {@link #readSFixed64()} instead. */ @Deprecated @Override long readFixed64() throws IOException; }
3,906
35.858491
153
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/FieldSetMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; /** * Marshaller for a set of fields, to be shared between multiple marshallers. * @author Paul Ferraro * @param <T> the type of this marshaller * @param <B> the builder type for reading embedded fields */ public interface FieldSetMarshaller<T, B> { /** * Returns a builder for use with {@link #readField(ProtoStreamReader, int, Object)}. * May return a shared instance, if the builder type is immutable, or a new instance, if the builder is mutable. * @return a builder. */ B getBuilder(); /** * Returns the number of fields written by this marshaller. * @return a positive number */ int getFields(); /** * Reads a single field from the specified reader at the specified index. * @param reader a ProtoStream reader * @param index the field index * @param builder the builder to be populated with the read field * @return the builder containing the read field * @throws IOException if the field could not be read */ B readField(ProtoStreamReader reader, int index, B builder) throws IOException; /** * Writes the set of fields from the specified object to the specified writer beginning at the specified index. * @param writer a ProtoStream writer * @param startIndex the start index for the embedded fields * @param value the value to be written * @throws IOException if the value could not be written */ void writeFields(ProtoStreamWriter writer, int startIndex, T value) throws IOException; }
2,647
38.522388
116
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/Any.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.util.Objects; import java.util.function.Supplier; /** * A wrapper for an arbitrary object. * @author Paul Ferraro */ class Any implements Supplier<Object> { private final Object value; Any(Object value) { this.value = value; } @Override public Object get() { return this.value; } @Override public int hashCode() { return (this.value != null) ? this.value.hashCode() : 0; } @Override public boolean equals(Object object) { if (!(object instanceof Any)) return false; return Objects.equals(this.value, ((Any) object).value); } @Override public String toString() { return (this.value != null) ? this.value.toString() : null; } }
1,837
29.131148
70
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamMarshallerAdapter.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import org.infinispan.protostream.ProtobufTagMarshaller; import org.infinispan.protostream.impl.TagWriterImpl; /** * Adapts a {@link ProtobufTagMarshaller} to a {@link ProtoStreamMarshaller}. * @author Paul Ferraro */ public class ProtoStreamMarshallerAdapter<T> implements ProtoStreamMarshaller<T> { private final ProtobufTagMarshaller<T> marshaller; ProtoStreamMarshallerAdapter(ProtobufTagMarshaller<T> marshaller) { this.marshaller = marshaller; } @Override public Class<? extends T> getJavaClass() { return this.marshaller.getJavaClass(); } @Override public String getTypeName() { return this.marshaller.getTypeName(); } @Override public T readFrom(ProtoStreamReader reader) throws IOException { return this.read((ReadContext) reader); } @Override public void writeTo(ProtoStreamWriter writer, T value) throws IOException { this.write((TagWriterImpl) ((WriteContext) writer).getWriter(), value); } @Override public T read(ReadContext context) throws IOException { return this.marshaller.read(context); } @Override public void write(WriteContext context, T value) throws IOException { this.marshaller.write(context, value); } }
2,394
32.263889
82
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/FunctionalFieldSetMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import java.util.function.Function; import org.infinispan.protostream.descriptors.WireType; /** * Marshaller for an object whose fields are marshalled via a {@link FieldSetMarshaller}. * @param <T> the type of this marshaller * @param <B> the builder type for reading embedded fields */ public class FunctionalFieldSetMarshaller<T, B> implements ProtoStreamMarshaller<T> { private static final int START_INDEX = 1; private final Class<? extends T> targetClass; private final FieldSetMarshaller<T, B> marshaller; private final Function<B, T> build; public FunctionalFieldSetMarshaller(Class<? extends T> targetClass, FieldSetMarshaller<T, B> marshaller, Function<B, T> factory) { this.targetClass = targetClass; this.marshaller = marshaller; this.build = factory; } @Override public T readFrom(ProtoStreamReader reader) throws IOException { B builder = this.marshaller.getBuilder(); while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); if ((index >= START_INDEX) && (index < START_INDEX + this.marshaller.getFields())) { builder = this.marshaller.readField(reader, index - START_INDEX, builder); } else { reader.skipField(tag); } } return this.build.apply(builder); } @Override public void writeTo(ProtoStreamWriter writer, T value) throws IOException { this.marshaller.writeFields(writer, START_INDEX, value); } @Override public Class<? extends T> getJavaClass() { return this.targetClass; } }
2,786
37.178082
134
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/AbstractProtoStreamWriter.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import java.nio.ByteBuffer; import java.util.IdentityHashMap; import java.util.Map; import java.util.OptionalInt; import java.util.function.Function; import org.infinispan.protostream.ProtobufTagMarshaller.WriteContext; import org.infinispan.protostream.TagWriter; import org.infinispan.protostream.descriptors.WireType; /** * Delegates most {@link ProtoStreamWriter} operations to a {@link TagWriter}. * @author Paul Ferraro */ public abstract class AbstractProtoStreamWriter extends AbstractProtoStreamOperation implements ProtoStreamWriter, WriteContext { interface ProtoStreamWriterContext extends ProtoStreamOperation.Context { /** * Returns an existing reference to the specified object, if one exists. * @param object an object whose may already have been referenced * @return a reference for the specified object, or null, if no reference yet exists. */ Reference getReference(Object object); /** * Creates a copy of this writer context. * Used to ensure reference integrity between size and write operations. * @return a copy of this writer context */ ProtoStreamWriterContext clone(); /** * Computes the size of the specified object via the specified function, if not already known * @param object the target object * @param function a function that computes the size of the specified object * @return the computed or cached size */ OptionalInt computeSize(Object object, Function<Object, OptionalInt> function); } private final TagWriter writer; private final int depth; private final ProtoStreamWriterContext context; protected AbstractProtoStreamWriter(WriteContext context, ProtoStreamWriterContext writerContext) { super(context); this.writer = context.getWriter(); this.depth = context.depth(); this.context = writerContext; } @Override public Context getContext() { return this.context; } @Override public TagWriter getWriter() { return this.writer; } @Override public int depth() { return this.depth; } @Override public void writeAnyNoTag(Object value) throws IOException { Reference reference = this.context.getReference(value); Any any = new Any((reference != null) ? reference : value); this.writeObjectNoTag(any); if (reference == null) { this.context.record(value); } } @Override public void writeTag(int number, WireType wireType) throws IOException { this.writer.writeTag(number, wireType); } @Override public void writeVarint32(int value) throws IOException { this.writer.writeVarint32(value); } @Override public void writeVarint64(long value) throws IOException { this.writer.writeVarint64(value); } @Deprecated @Override public void writeRawByte(byte value) throws IOException { this.writer.writeRawByte(value); } @Override public void writeRawBytes(byte[] value, int offset, int length) throws IOException { this.writer.writeRawBytes(value, offset, length); } @Deprecated @Override public void writeRawBytes(ByteBuffer value) throws IOException { this.writer.writeRawBytes(value); } @Override public void writeBool(int index, boolean value) throws IOException { this.writer.writeBool(index, value); } @Override public void writeEnum(int index, int value) throws IOException { this.writer.writeEnum(index, value); } @Deprecated @Override public void writeInt32(int index, int value) throws IOException { this.writer.writeInt32(index, value); } @Deprecated @Override public void writeFixed32(int index, int value) throws IOException { this.writer.writeFixed32(index, value); } @Override public void writeUInt32(int index, int value) throws IOException { this.writer.writeUInt32(index, value); } @Override public void writeSInt32(int index, int value) throws IOException { this.writer.writeSInt32(index, value); } @Override public void writeSFixed32(int index, int value) throws IOException { this.writer.writeSFixed32(index, value); } @Deprecated @Override public void writeInt64(int index, long value) throws IOException { this.writer.writeInt64(index, value); } @Deprecated @Override public void writeFixed64(int index, long value) throws IOException { this.writer.writeFixed64(index, value); } @Override public void writeUInt64(int index, long value) throws IOException { this.writer.writeUInt64(index, value); } @Override public void writeSInt64(int index, long value) throws IOException { this.writer.writeSInt64(index, value); } @Override public void writeSFixed64(int index, long value) throws IOException { this.writer.writeSFixed64(index, value); } @Override public void writeFloat(int index, float value) throws IOException { this.writer.writeFloat(index, value); } @Override public void writeDouble(int index, double value) throws IOException { this.writer.writeDouble(index, value); } @Override public void writeBytes(int index, byte[] value) throws IOException { this.writer.writeBytes(index, value); } @Override public void writeBytes(int index, byte[] value, int offset, int length) throws IOException { this.writer.writeBytes(index, value, offset, length); } @Override public void writeBytes(int index, ByteBuffer value) throws IOException { this.writer.writeBytes(index, value); } @Override public void writeString(int index, String value) throws IOException { this.writer.writeString(index, value); } @Override public void flush() throws IOException { this.writer.flush(); } static class DefaultProtoStreamWriterContext implements ProtoStreamWriterContext, Function<Object, Reference> { private final Map<Object, Reference> references = new IdentityHashMap<>(128); private int reference = 0; // Enumerates object references private final Map<Object, OptionalInt> sizes; DefaultProtoStreamWriterContext() { this(new IdentityHashMap<>(128)); } private DefaultProtoStreamWriterContext(Map<Object, OptionalInt> sizes) { this.sizes = sizes; } @Override public void record(Object object) { if (object != null) { this.references.computeIfAbsent(object, this); } } @Override public Reference getReference(Object object) { return (object != null) ? this.references.get(object) : null; } @Override public Reference apply(Object key) { return new Reference(this.reference++); } @Override public ProtoStreamWriterContext clone() { // Share size cache DefaultProtoStreamWriterContext context = new DefaultProtoStreamWriterContext(this.sizes); // Copy references context.references.putAll(this.references); context.reference = this.reference; return context; } @Override public OptionalInt computeSize(Object object, Function<Object, OptionalInt> function) { // Don't cache size for internal wrappers, which would otherwise bloat our hashtable return (object instanceof Any) ? function.apply(object) : this.sizes.computeIfAbsent(object, function); } } }
8,982
31.082143
129
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamBuilder.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; /** * Builder used during {@link ProtoStreamMarshaller#readFrom(org.infinispan.protostream.ImmutableSerializationContext, org.infinispan.protostream.RawProtoStreamReader)}. * @author Paul Ferraro * @param <T> the target type of this builder */ public interface ProtoStreamBuilder<T> { /** * Builds an object read from a ProtoStream reader. * @return the built object */ T build(); }
1,491
39.324324
169
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ClassMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import org.infinispan.protostream.BaseMarshaller; import org.infinispan.protostream.ImmutableSerializationContext; import org.infinispan.protostream.descriptors.WireType; /** * Generic marshaller for instances of {@link Class}. * @author Paul Ferraro */ public class ClassMarshaller implements ProtoStreamMarshaller<Class<?>> { private final Field<Class<?>> field; public ClassMarshaller(ClassLoaderMarshaller marshaller) { ClassField[] fields = ClassField.values(); this.field = new LoadedClassField(marshaller, fields[fields.length - 1].getIndex() + 1); } @Override public Class<?> readFrom(ProtoStreamReader reader) throws IOException { Class<?> result = Object.class; while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); Field<Class<?>> field = index == this.field.getIndex() ? this.field : ClassField.fromIndex(index); if (field != null) { result = field.getMarshaller().readFrom(reader); } else { reader.skipField(tag); } } return result; } @Override public void writeTo(ProtoStreamWriter writer, Class<?> targetClass) throws IOException { if (targetClass != Object.class) { Field<Class<?>> field = this.getField(writer.getSerializationContext(), targetClass); writer.writeTag(field.getIndex(), field.getMarshaller().getWireType()); field.getMarshaller().writeTo(writer, targetClass); } } Field<Class<?>> getField(ImmutableSerializationContext context, Class<?> targetClass) { AnyField classField = AnyField.fromJavaType(targetClass); if (classField != null) return ClassField.FIELD; if (targetClass.isArray()) return ClassField.ARRAY; try { BaseMarshaller<?> marshaller = context.getMarshaller(targetClass); if (marshaller.getJavaClass() != targetClass) return this.field; return context.getDescriptorByName(marshaller.getTypeName()).getTypeId() != null ? ClassField.ID : ClassField.NAME; } catch (IllegalArgumentException e) { // If class does not represent a registered type, then use the loader based marshaller. return this.field; } } @Override public Class<? extends Class<?>> getJavaClass() { return this.field.getMarshaller().getJavaClass(); } }
3,604
39.965909
127
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/MarshallingMarshallerProvider.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; /** * @author Paul Ferraro */ public enum MarshallingMarshallerProvider implements ProtoStreamMarshallerProvider { BYTE_BUFFER_MARSHALLED_KEY(new ByteBufferMarshalledKeyMarshaller()), BYTE_BUFFER_MARSHALLED_VALUE(new ByteBufferMarshalledValueMarshaller()), ; private final ProtoStreamMarshaller<?> marshaller; MarshallingMarshallerProvider(ProtoStreamMarshaller<?> marshaller) { this.marshaller = marshaller; } @Override public ProtoStreamMarshaller<?> getMarshaller() { return this.marshaller; } }
1,636
37.069767
84
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ScalarFieldMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import org.infinispan.protostream.descriptors.WireType; /** * A field marshaller based on a scaler marshaller. * @author Paul Ferraro * @param <T> the type of this marshaller */ public class ScalarFieldMarshaller<T> implements FieldMarshaller<T> { private final ScalarMarshaller<T> marshaller; public ScalarFieldMarshaller(ScalarMarshaller<T> marshaller) { this.marshaller = marshaller; } @Override public T readFrom(ProtoStreamReader reader) throws IOException { T result = this.marshaller.readFrom(reader); while (!reader.isAtEnd()) { int tag = reader.readTag(); reader.skipField(tag); } return result; } @Override public void writeTo(ProtoStreamWriter writer, T value) throws IOException { this.marshaller.writeTo(writer, value); } @Override public Class<? extends T> getJavaClass() { return this.marshaller.getJavaClass(); } @Override public WireType getWireType() { return this.marshaller.getWireType(); } }
2,182
31.58209
79
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/CompositeSerializationContextInitializer.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.UncheckedIOException; import java.util.EnumSet; import java.util.List; import org.infinispan.protostream.SerializationContext; import org.infinispan.protostream.SerializationContextInitializer; /** * {@link SerializationContextInitializer} that registers a set of {@link SerializationContextInitializer} instances. * @author Paul Ferraro */ public class CompositeSerializationContextInitializer implements SerializationContextInitializer { private final Iterable<? extends SerializationContextInitializer> initializers; public CompositeSerializationContextInitializer(SerializationContextInitializer initializer1, SerializationContextInitializer initializer2) { this(List.of(initializer1, initializer2)); } public CompositeSerializationContextInitializer(SerializationContextInitializer... initializers) { this(List.of(initializers)); } public <E extends Enum<E> & SerializationContextInitializer> CompositeSerializationContextInitializer(Class<E> enumClass) { this(EnumSet.allOf(enumClass)); } public CompositeSerializationContextInitializer(Iterable<? extends SerializationContextInitializer> initializers) { this.initializers = initializers; } @Deprecated @Override public String getProtoFileName() { return null; } @Deprecated @Override public String getProtoFile() throws UncheckedIOException { return null; } @Override public void registerSchema(SerializationContext context) { for (SerializationContextInitializer initializer : this.initializers) { initializer.registerSchema(context); } } @Override public void registerMarshallers(SerializationContext context) { for (SerializationContextInitializer initializer : this.initializers) { initializer.registerMarshallers(context); } } }
2,997
35.560976
145
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamMarshallerProvider.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import java.util.OptionalInt; /** * Provides a {@link ProtoStreamMarshaller}. * @author Paul Ferraro */ public interface ProtoStreamMarshallerProvider extends ProtoStreamMarshaller<Object> { ProtoStreamMarshaller<?> getMarshaller(); @Override default Object readFrom(ProtoStreamReader reader) throws IOException { return this.getMarshaller().readFrom(reader); } @Override default void writeTo(ProtoStreamWriter writer, Object value) throws IOException { this.cast(Object.class).writeTo(writer, value); } @Override default Object read(ReadContext context) throws IOException { return this.getMarshaller().read(context); } @Override default void write(WriteContext context, Object value) throws IOException { this.cast(Object.class).write(context, value); } @Override default OptionalInt size(ProtoStreamSizeOperation operation, Object value) { return this.cast(Object.class).size(operation, value); } @Override default Class<? extends Object> getJavaClass() { return this.getMarshaller().getJavaClass(); } @SuppressWarnings("unchecked") default <T> ProtoStreamMarshaller<T> cast(Class<T> type) { if (!type.isAssignableFrom(this.getJavaClass())) { throw new IllegalArgumentException(type.getName()); } return (ProtoStreamMarshaller<T>) this.getMarshaller(); } }
2,552
33.972603
86
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/AnyMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import java.lang.reflect.Proxy; import org.infinispan.protostream.BaseMarshaller; import org.infinispan.protostream.ImmutableSerializationContext; import org.infinispan.protostream.descriptors.WireType; /** * Marshaller for an {@link Any} object. * @author Paul Ferraro */ public enum AnyMarshaller implements ProtoStreamMarshaller<Any> { INSTANCE; @Override public Class<? extends Any> getJavaClass() { return Any.class; } @Override public Any readFrom(ProtoStreamReader reader) throws IOException { Object value = null; while (!reader.isAtEnd()) { int tag = reader.readTag(); AnyField field = AnyField.fromIndex(WireType.getTagFieldNumber(tag)); if (field != null) { value = field.getMarshaller().readFrom(reader); } else { reader.skipField(tag); } } return new Any(value); } @Override public void writeTo(ProtoStreamWriter writer, Any value) throws IOException { Object object = value.get(); if (object != null) { AnyField field = getField(writer, object); writer.writeTag(field.getIndex(), field.getMarshaller().getWireType()); field.getMarshaller().writeTo(writer, object); } } private static AnyField getField(ProtoStreamWriter writer, Object value) { if (value instanceof Reference) return AnyField.REFERENCE; ImmutableSerializationContext context = writer.getSerializationContext(); Class<?> valueClass = value.getClass(); AnyField field = AnyField.fromJavaType(valueClass); if (field != null) return field; if (value instanceof Enum) { Enum<?> enumValue = (Enum<?>) value; BaseMarshaller<?> marshaller = context.getMarshaller(enumValue.getDeclaringClass()); return hasTypeId(context, marshaller) ? AnyField.IDENTIFIED_ENUM : AnyField.NAMED_ENUM; } if (valueClass.isArray()) { Class<?> componentType = valueClass.getComponentType(); AnyField componentTypeField = AnyField.fromJavaType(componentType); if (componentTypeField != null) return AnyField.FIELD_ARRAY; try { BaseMarshaller<?> marshaller = context.getMarshaller(componentType); return hasTypeId(context, marshaller) ? AnyField.IDENTIFIED_ARRAY : AnyField.NAMED_ARRAY; } catch (IllegalArgumentException e) { return AnyField.ANY_ARRAY; } } if (Proxy.isProxyClass(valueClass)) { return AnyField.PROXY; } BaseMarshaller<?> marshaller = writer.findMarshaller(valueClass); return hasTypeId(context, marshaller) ? AnyField.IDENTIFIED_OBJECT : AnyField.NAMED_OBJECT; } private static boolean hasTypeId(ImmutableSerializationContext context, BaseMarshaller<?> marshaller) { return context.getDescriptorByName(marshaller.getTypeName()).getTypeId() != null; } }
4,170
37.981308
107
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/Field.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; /** * A field of a marshaller. * @author Paul Ferraro * @param <T> the type of the associated marshaller */ public interface Field<T> { /** * Returns the index of this field. * @return the index of this field. */ int getIndex(); /** * Returns the marshaller for this field. * @return the marshaller for this field. */ FieldMarshaller<T> getMarshaller(); }
1,488
33.627907
70
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/SerializationContextBuilder.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.security.PrivilegedAction; import java.util.Collections; import java.util.EnumSet; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.ServiceLoader; import org.infinispan.protostream.ImmutableSerializationContext; import org.infinispan.protostream.SerializationContextInitializer; import org.wildfly.security.manager.WildFlySecurityManager; /** * Builds a ProtoStream {@link ImmutableSerializationContext}. * @author Paul Ferraro */ public class SerializationContextBuilder { private static final String PROTOSTREAM_BASE_PACKAGE_NAME = org.infinispan.protostream.BaseMarshaller.class.getPackage().getName(); private final DefaultSerializationContext context = new DefaultSerializationContext(); /** * Constructs a builder for a {@link org.infinispan.protostream.SerializationContext} using a default set of initializers. * @param marshaller a class loader marshaller */ public SerializationContextBuilder(ClassLoaderMarshaller marshaller) { // Load default schemas first, so they can be referenced by loader-specific schemas this.register(Collections.singleton(new LangSerializationContextInitializer(marshaller))); this.register(EnumSet.allOf(DefaultSerializationContextInitializerProvider.class)); } /** * Returns an immutable {@link org.infinispan.protostream.SerializationContext}. * @return the completed and immutable serialization context */ public ImmutableSerializationContext build() { return this.context.get(); } /** * Registers an initializer with the {@link org.infinispan.protostream.SerializationContext}. * @param initializer an initializer for the {@link org.infinispan.protostream.SerializationContext}. * @return this builder */ public SerializationContextBuilder register(SerializationContextInitializer initializer) { this.init(initializer); return this; } /** * Registers a number of initializers with the {@link org.infinispan.protostream.SerializationContext}. * @param initializers one or more initializers for the {@link org.infinispan.protostream.SerializationContext}. * @return this builder */ public SerializationContextBuilder register(Iterable<? extends SerializationContextInitializer> initializers) { for (SerializationContextInitializer initializer : initializers) { this.init(initializer); } return this; } /** * Loads {@link SerializationContextInitializer} instances from the specified {@link ClassLoader} and registers then with the {@link org.infinispan.protostream.SerializationContext}. * @param loader a class loader * @return this builder */ public SerializationContextBuilder load(ClassLoader loader) { this.tryLoad(loader); return this; } /** * Similar to {@link #load(ClassLoader)}, but throws a {@link NoSuchElementException} if no {@link SerializationContextInitializer} instances were found. * @param loader a class loader * @return this builder */ public SerializationContextBuilder require(ClassLoader loader) { if (!this.tryLoad(loader)) { throw new NoSuchElementException(); } return this; } private boolean tryLoad(ClassLoader loader) { PrivilegedAction<Boolean> action = new PrivilegedAction<>() { @Override public Boolean run() { Iterator<SerializationContextInitializer> initializers = ServiceLoader.load(SerializationContextInitializer.class, loader).iterator(); boolean init = false; while (initializers.hasNext()) { SerializationContextInitializer initializer = initializers.next(); // Do not load initializers from protostream-types if (!initializer.getClass().getName().startsWith(PROTOSTREAM_BASE_PACKAGE_NAME)) { SerializationContextBuilder.this.init(initializer); init = true; } } return init; } }; return WildFlySecurityManager.doUnchecked(action); } void init(SerializationContextInitializer initializer) { initializer.registerSchema(this.context); initializer.registerMarshallers(this.context); } }
5,536
40.94697
186
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/DefaultProtoStreamSizeOperation.java
/* * JBoss, Home of Professional Open Source. * Copyright 2023, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import java.util.OptionalInt; import org.infinispan.protostream.ImmutableSerializationContext; import org.wildfly.clustering.marshalling.protostream.AbstractProtoStreamWriter.DefaultProtoStreamWriterContext; import org.wildfly.clustering.marshalling.protostream.AbstractProtoStreamWriter.ProtoStreamWriterContext; import org.wildfly.common.function.ExceptionBiConsumer; /** * A default ProtoStream size operation. * @author Paul Ferraro */ public class DefaultProtoStreamSizeOperation extends AbstractProtoStreamOperation implements ProtoStreamSizeOperation { private final ProtoStreamWriterContext context; /** * Creates a new ProtoStream size operation using a new context. * @param context the serialization context */ public DefaultProtoStreamSizeOperation(ImmutableSerializationContext context) { this(context, new DefaultProtoStreamWriterContext()); } /** * Creates a new ProtoStream size operation using the specified context. * @param context the serialization context * @param sizeContext the context of the size operation */ public DefaultProtoStreamSizeOperation(ImmutableSerializationContext context, ProtoStreamWriterContext writerContext) { super(context); this.context = writerContext; } @Override public Context getContext() { return this.context; } @Override public <T> OptionalInt computeSize(ExceptionBiConsumer<ProtoStreamWriter, T, IOException> operation, T value) { SizeComputingProtoStreamWriter writer = new SizeComputingProtoStreamWriter(this, this.context); try { operation.accept(writer, value); return writer.get(); } catch (IOException e) { return OptionalInt.empty(); } } }
2,910
37.813333
123
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/FunctionalScalarMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import java.util.Objects; import java.util.function.BiPredicate; import java.util.function.Supplier; import org.infinispan.protostream.descriptors.WireType; import org.wildfly.common.function.ExceptionFunction; import org.wildfly.common.function.ExceptionPredicate; import org.wildfly.common.function.Functions; /** * Marshaller that reads/writes a single field by applying functions to a {@link ScalarMarshaller}. * @author Paul Ferraro * @param <T> the type of this marshaller * @param <V> the type of the mapped scalar marshaller */ public class FunctionalScalarMarshaller<T, V> implements ProtoStreamMarshaller<T> { private static final int VALUE_INDEX = 1; private final Class<? extends T> targetClass; private final Supplier<T> defaultFactory; private final ExceptionPredicate<T, IOException> skipWrite; private final ScalarMarshaller<V> marshaller; private final ExceptionFunction<T, V, IOException> function; private final ExceptionFunction<V, T, IOException> factory; /** * Constructs a new single field marshaller based on single scalar marshaller. * @param marshaller the scalar marshaller used by this marshaller * @param defaultFactory generates a default value returned by {@link #readFrom(ProtoStreamReader)} if no field was written. * @param function a function that returns a value suitable for use by the specified scalar marshaller * @param factory a function applied to the value read from the specified scalar marshaller */ @SuppressWarnings("unchecked") public FunctionalScalarMarshaller(ScalarMarshaller<V> marshaller, Supplier<T> defaultFactory, ExceptionFunction<T, V, IOException> function, ExceptionFunction<V, T, IOException> factory) { this((Class<T>) defaultFactory.get().getClass(), marshaller, defaultFactory, function, factory); } /** * Constructs a new single field marshaller based on single scalar marshaller. * @param marshaller the scalar marshaller used by this marshaller * @param defaultFactory generates a default value returned by {@link #readFrom(ProtoStreamReader)} if no field was written. * @param equals a predicate used to determine if {@link #writeTo(ProtoStreamWriter, Object)} should skip writing the field. * @param function a function that returns a value suitable for use by the specified scalar marshaller * @param factory a function applied to the value read from the specified scalar marshaller */ @SuppressWarnings("unchecked") public FunctionalScalarMarshaller(ScalarMarshaller<V> marshaller, Supplier<T> defaultFactory, BiPredicate<V, V> equals, ExceptionFunction<T, V, IOException> function, ExceptionFunction<V, T, IOException> factory) { this((Class<T>) defaultFactory.get().getClass(), marshaller, defaultFactory, equals, function, factory); } /** * Constructs a new single field marshaller based on single scalar marshaller. * @param marshaller the scalar marshaller used by this marshaller * @param defaultFactory generates a default value returned by {@link #readFrom(ProtoStreamReader)} if no field was written. * @param skipWrite a predicate used to determine if {@link #writeTo(ProtoStreamWriter, Object)} should skip writing the field. * @param function a function that returns a value suitable for use by the specified scalar marshaller * @param factory a function applied to the value read from the specified scalar marshaller */ @SuppressWarnings("unchecked") public FunctionalScalarMarshaller(ScalarMarshaller<V> marshaller, Supplier<T> defaultFactory, ExceptionPredicate<T, IOException> skipWrite, ExceptionFunction<T, V, IOException> function, ExceptionFunction<V, T, IOException> factory) { this((Class<T>) defaultFactory.get().getClass(), marshaller, defaultFactory, skipWrite, function, factory); } /** * Constructs a new single field marshaller based on single scalar marshaller. * @param targetClass the type of this marshaller * @param marshaller the scalar marshaller used by this marshaller * @param function a function that returns a value suitable for use by the specified scalar marshaller * @param factory a function applied to the value read from the specified scalar marshaller */ public FunctionalScalarMarshaller(Class<? extends T> targetClass, ScalarMarshaller<V> marshaller, ExceptionFunction<T, V, IOException> function, ExceptionFunction<V, T, IOException> factory) { this(targetClass, marshaller, Functions.constantSupplier(null), Objects::isNull, function, factory); } /** * Constructs a new single field marshaller based on single scalar marshaller. * @param targetClass the type of this marshaller * @param marshaller the scalar marshaller used by this marshaller * @param defaultFactory generates a default value returned by {@link #readFrom(ProtoStreamReader)} if no field was written. * @param function a function that returns a value suitable for use by the specified scalar marshaller * @param factory a function applied to the value read from the specified scalar marshaller */ public FunctionalScalarMarshaller(Class<? extends T> targetClass, ScalarMarshaller<V> marshaller, Supplier<T> defaultFactory, ExceptionFunction<T, V, IOException> function, ExceptionFunction<V, T, IOException> factory) { this(targetClass, marshaller, defaultFactory, Objects::equals, function, factory); } /** * Constructs a new single field marshaller based on single scalar marshaller. * @param targetClass the type of this marshaller * @param marshaller the scalar marshaller used by this marshaller * @param defaultFactory generates a default value returned by {@link #readFrom(ProtoStreamReader)} if no field was written. * @param equals a predicate comparing the default value with the value to write, used to determine if {@link #writeTo(ProtoStreamWriter, Object)} should skip writing the field. * @param function a function that returns a value suitable for use by the specified scalar marshaller * @param factory a function applied to the value read from the specified scalar marshaller */ public FunctionalScalarMarshaller(Class<? extends T> targetClass, ScalarMarshaller<V> marshaller, Supplier<T> defaultFactory, BiPredicate<V, V> equals, ExceptionFunction<T, V, IOException> function, ExceptionFunction<V, T, IOException> factory) { this(targetClass, marshaller, defaultFactory, new ExceptionPredicate<T, IOException>() { @Override public boolean test(T value) throws IOException { return equals.test(function.apply(value), function.apply(defaultFactory.get())); } }, function, factory); } /** * Constructs a new single field marshaller based on single scalar marshaller. * @param targetClass the type of this marshaller * @param marshaller the scalar marshaller used by this marshaller * @param defaultFactory generates a default value returned by {@link #readFrom(ProtoStreamReader)} if no field was written. * @param skipWrite a predicate used to determine if {@link #writeTo(ProtoStreamWriter, Object)} should skip writing the field. * @param function a function that returns a value suitable for use by the specified scalar marshaller * @param factory a function applied to the value read from the specified scalar marshaller */ public FunctionalScalarMarshaller(Class<? extends T> targetClass, ScalarMarshaller<V> marshaller, Supplier<T> defaultFactory, ExceptionPredicate<T, IOException> skipWrite, ExceptionFunction<T, V, IOException> function, ExceptionFunction<V, T, IOException> factory) { this.targetClass = targetClass; this.defaultFactory = defaultFactory; this.skipWrite = skipWrite; this.marshaller = marshaller; this.function = function; this.factory = factory; } @Override public T readFrom(ProtoStreamReader reader) throws IOException { T value = this.defaultFactory.get(); while (!reader.isAtEnd()) { int tag = reader.readTag(); switch (WireType.getTagFieldNumber(tag)) { case VALUE_INDEX: value = this.factory.apply(this.marshaller.readFrom(reader)); break; default: reader.skipField(tag); } } return value; } @Override public void writeTo(ProtoStreamWriter writer, T value) throws IOException { if (!this.skipWrite.test(value)) { writer.writeTag(VALUE_INDEX, this.marshaller.getWireType()); this.marshaller.writeTo(writer, this.function.apply(value)); } } @Override public Class<? extends T> getJavaClass() { return this.targetClass; } }
10,057
55.505618
270
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ScalarClass.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import java.nio.charset.StandardCharsets; import org.infinispan.protostream.BaseMarshaller; import org.infinispan.protostream.ImmutableSerializationContext; import org.infinispan.protostream.descriptors.WireType; /** * Set of scalar marshallers for marshalling a {@link Class}. * @author Paul Ferraro */ public enum ScalarClass implements ScalarMarshaller<Class<?>> { ANY(WireType.LENGTH_DELIMITED) { @Override public Class<?> readFrom(ProtoStreamReader reader) throws IOException { return reader.readObject(Class.class); } @Override public void writeTo(ProtoStreamWriter writer, Class<?> value) throws IOException { writer.writeObjectNoTag(value); } }, ID(WireType.VARINT) { @Override public Class<?> readFrom(ProtoStreamReader reader) throws IOException { int typeId = reader.readUInt32(); ImmutableSerializationContext context = reader.getSerializationContext(); String typeName = context.getDescriptorByTypeId(typeId).getFullName(); BaseMarshaller<?> marshaller = context.getMarshaller(typeName); return marshaller.getJavaClass(); } @Override public void writeTo(ProtoStreamWriter writer, Class<?> value) throws IOException { BaseMarshaller<?> marshaller = writer.findMarshaller(value); String typeName = marshaller.getTypeName(); int typeId = writer.getSerializationContext().getDescriptorByName(typeName).getTypeId(); writer.writeVarint32(typeId); } }, NAME(WireType.LENGTH_DELIMITED) { @Override public Class<?> readFrom(ProtoStreamReader reader) throws IOException { String typeName = StandardCharsets.UTF_8.decode(reader.readByteBuffer()).toString(); BaseMarshaller<?> marshaller = reader.getSerializationContext().getMarshaller(typeName); return marshaller.getJavaClass(); } @Override public void writeTo(ProtoStreamWriter writer, Class<?> value) throws IOException { BaseMarshaller<?> marshaller = writer.findMarshaller(value); String typeName = marshaller.getTypeName(); Scalar.BYTE_BUFFER.writeTo(writer, StandardCharsets.UTF_8.encode(typeName)); } }, FIELD(WireType.VARINT) { @Override public Class<?> readFrom(ProtoStreamReader reader) throws IOException { return AnyField.fromIndex(reader.readUInt32() + 1).getMarshaller().getJavaClass(); } @Override public void writeTo(ProtoStreamWriter writer, Class<?> value) throws IOException { writer.writeVarint32(AnyField.fromJavaType(value).getIndex() - 1); } }, ; private final WireType wireType; ScalarClass(WireType wireType) { this.wireType = wireType; } @SuppressWarnings("unchecked") @Override public Class<? extends Class<?>> getJavaClass() { return (Class<? extends Class<?>>) (Class<?>) Class.class; } @Override public WireType getWireType() { return this.wireType; } }
4,280
37.567568
100
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ModuleClassLoaderMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import java.io.InvalidClassException; import org.jboss.modules.Module; import org.jboss.modules.ModuleLoadException; import org.jboss.modules.ModuleLoader; /** * @author Paul Ferraro */ public class ModuleClassLoaderMarshaller implements ClassLoaderMarshaller { private static final int MODULE_INDEX = 0; private static final int FIELDS = 1; private final ModuleLoader loader; private final Module defaultModule; public ModuleClassLoaderMarshaller(Module defaultModule) { this.loader = defaultModule.getModuleLoader(); this.defaultModule = defaultModule; } public ModuleClassLoaderMarshaller(ModuleLoader loader) { this.loader = loader; try { this.defaultModule = Module.getSystemModuleLoader().loadModule("java.base"); } catch (ModuleLoadException e) { throw new IllegalStateException(e); } } @Override public ClassLoader getBuilder() { return this.defaultModule.getClassLoader(); } @Override public int getFields() { return FIELDS; } @Override public ClassLoader readField(ProtoStreamReader reader, int index, ClassLoader loader) throws IOException { switch (index) { case MODULE_INDEX: String moduleName = reader.readAny(String.class); try { Module module = this.loader.loadModule(moduleName); return module.getClassLoader(); } catch (ModuleLoadException e) { InvalidClassException exception = new InvalidClassException(e.getMessage()); exception.initCause(e); throw exception; } default: return loader; } } @Override public void writeFields(ProtoStreamWriter writer, int startIndex, ClassLoader loader) throws IOException { Module module = Module.forClassLoader(loader, false); if (module != null && !this.defaultModule.equals(module)) { writer.writeAny(startIndex + MODULE_INDEX, module.getName()); } } }
3,255
34.010753
110
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamSizeOperation.java
/* * JBoss, Home of Professional Open Source. * Copyright 2023, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import java.util.OptionalInt; import org.infinispan.protostream.descriptors.WireType; import org.infinispan.protostream.impl.TagWriterImpl; import org.wildfly.common.function.ExceptionBiConsumer; /** * A ProtoStream size operation. * @author Paul Ferraro */ public interface ProtoStreamSizeOperation extends ProtoStreamOperation { /** * Computes the size of the specified object using the specified operation. * @param <T> the source object type * @param operation a write operation used to compute the size * @param value the object to be sized * @return the computed size */ <T> OptionalInt computeSize(ExceptionBiConsumer<ProtoStreamWriter, T, IOException> operation, T value); /** * Computes the marshalled size of the protobuf tag containing the specified field index and wire type. * @param index a field index * @param type a wire type * @return the marshalled size of the protobuf tag */ default int tagSize(int index, WireType type) { return this.varIntSize(WireType.makeTag(index, type)); } /** * Computes the marshalled size of the specified variable-width integer. * @param index a variable-width integer * @return the marshalled size of the specified variable-width integer. */ default int varIntSize(int value) { TagWriterImpl writer = TagWriterImpl.newInstance(this.getSerializationContext()); try { writer.writeVarint32(value); return writer.getWrittenBytes(); } catch (IOException e) { return WireType.MAX_VARINT_SIZE; } } }
2,742
37.097222
107
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/FieldMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import org.infinispan.protostream.descriptors.WireType; /** * Marshaller for a field. * {@link #writeTo(ProtoStreamWriter, Object)} does not write a field tag, but may write additional tagged fields. * Likewise, {@link #readFrom(ProtoStreamReader)} will continue to read fields until a zero tag is reached. * @author Paul Ferraro * @param <T> the type of this marshaller */ public interface FieldMarshaller<T> extends Marshallable<T> { /** * Returns the wire type of the scalar value written by this marshaller. * @return the wire type of the scalar value written by this marshaller. */ WireType getWireType(); }
1,725
40.095238
114
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/SimpleObjectOutput.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.ObjectOutput; import java.util.function.Consumer; import org.wildfly.common.function.Functions; /** * {@link ObjectOutput} implementation used to read the unexposed fields of an {@link java.io.Externalizable} object. * @author Paul Ferraro */ public class SimpleObjectOutput extends SimpleDataOutput implements ObjectOutput { private final Consumer<Object> objects; SimpleObjectOutput(Builder builder) { super(builder); this.objects = builder.objects; } @Override public void writeObject(Object value) { this.objects.accept(value); } @Override public void flush() { throw new UnsupportedOperationException(); } @Override public void close() { throw new UnsupportedOperationException(); } public static class Builder extends SimpleDataOutput.Builder { Consumer<Object> objects = Functions.discardingConsumer(); public Builder with(Object[] values) { this.objects = new SimpleDataOutput.ArrayConsumer<>(values); return this; } @Override public Builder with(String[] values) { super.with(values); return this; } @Override public Builder with(char[] values) { super.with(values); return this; } @Override public Builder with(boolean[] values) { super.with(values); return this; } @Override public Builder with(byte[] values) { super.with(values); return this; } @Override public Builder with(short[] values) { super.with(values); return this; } @Override public Builder with(int[] values) { super.with(values); return this; } @Override public Builder with(long[] values) { super.with(values); return this; } @Override public Builder with(float[] values) { super.with(values); return this; } @Override public Builder with(double[] values) { super.with(values); return this; } @Override public ObjectOutput build() { return new SimpleObjectOutput(this); } } }
3,483
26.872
117
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/AnySerializationContextInitializer.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import org.infinispan.protostream.SerializationContext; /** * Initializer that registers protobuf schema and marshaller for {@link Any}. * @author Paul Ferraro */ public class AnySerializationContextInitializer extends AbstractSerializationContextInitializer { @Override public void registerMarshallers(SerializationContext context) { context.registerMarshaller(AnyMarshaller.INSTANCE); } }
1,497
38.421053
97
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/LangSerializationContextInitializer.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import org.infinispan.protostream.SerializationContext; /** * Initializer that registers protobuf schema for java.lang.* classes. * @author Paul Ferraro */ public class LangSerializationContextInitializer extends AbstractSerializationContextInitializer { private final ClassLoaderMarshaller loaderMarshaller; public LangSerializationContextInitializer(ClassLoaderMarshaller loaderMarshaller) { super("java.lang.proto"); this.loaderMarshaller = loaderMarshaller; } @Override public void registerMarshallers(SerializationContext context) { context.registerMarshaller(new ClassMarshaller(this.loaderMarshaller)); context.registerMarshaller(StackTraceElementMarshaller.INSTANCE); context.registerMarshaller(new ExceptionMarshaller<>(Throwable.class)); } }
1,904
39.531915
98
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ByteBufferMarshalledKeyMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2022, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import java.nio.ByteBuffer; import java.util.OptionalInt; import org.infinispan.protostream.descriptors.WireType; import org.wildfly.clustering.marshalling.spi.ByteBufferMarshalledKey; /** * {@link ProtoStreamMarshaller} for a {@link ByteBufferMarshalledKey}. * @author Paul Ferraro */ public class ByteBufferMarshalledKeyMarshaller implements ProtoStreamMarshaller<ByteBufferMarshalledKey<Object>> { private static final int BUFFER_INDEX = 1; private static final int HASH_CODE_INDEX = 2; @Override public ByteBufferMarshalledKey<Object> readFrom(ProtoStreamReader reader) throws IOException { ByteBuffer buffer = null; int hashCode = 0; while (!reader.isAtEnd()) { int tag = reader.readTag(); switch (WireType.getTagFieldNumber(tag)) { case BUFFER_INDEX: buffer = reader.readByteBuffer(); break; case HASH_CODE_INDEX: hashCode = reader.readSFixed32(); break; default: reader.skipField(tag); } } return new ByteBufferMarshalledKey<>(buffer, hashCode); } @Override public void writeTo(ProtoStreamWriter writer, ByteBufferMarshalledKey<Object> key) throws IOException { ByteBuffer buffer = key.getBuffer(); if (buffer != null) { writer.writeBytes(BUFFER_INDEX, buffer); } int hashCode = key.hashCode(); if (hashCode != 0) { writer.writeSFixed32(HASH_CODE_INDEX, hashCode); } } @Override public OptionalInt size(ProtoStreamSizeOperation operation, ByteBufferMarshalledKey<Object> key) { if (key.isEmpty()) return OptionalInt.of(0); int hashCodeSize = WireType.FIXED_32_SIZE + operation.tagSize(HASH_CODE_INDEX, WireType.FIXED32); OptionalInt size = key.size(); return size.isPresent() ? OptionalInt.of(operation.tagSize(BUFFER_INDEX, WireType.LENGTH_DELIMITED) + operation.varIntSize(size.getAsInt()) + size.getAsInt() + hashCodeSize) : OptionalInt.empty(); } @SuppressWarnings("unchecked") @Override public Class<? extends ByteBufferMarshalledKey<Object>> getJavaClass() { return (Class<ByteBufferMarshalledKey<Object>>) (Class<?>) ByteBufferMarshalledKey.class; } }
3,481
39.022989
204
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ScalarMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import org.infinispan.protostream.descriptors.WireType; /** * Marshaller for a single scalar value. * This marshaller does not write any tags, nor does it read beyond a single value. * @author Paul Ferraro * @param <T> the type of this marshaller */ public interface ScalarMarshaller<T> extends Marshallable<T> { /** * Returns the wire type of the scalar value written by this marshaller. * @return the wire type of the scalar value written by this marshaller. */ WireType getWireType(); }
1,601
38.073171
83
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/TypedArrayMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream; import java.io.IOException; import java.lang.reflect.Array; import java.util.LinkedList; import java.util.List; import org.infinispan.protostream.descriptors.WireType; /** * Marshaller for an Object array, using a repeated element field. * @author Paul Ferraro */ public class TypedArrayMarshaller implements FieldMarshaller<Object> { private final ScalarMarshaller<Class<?>> componentType; public TypedArrayMarshaller(ScalarMarshaller<Class<?>> componentType) { this.componentType = componentType; } @Override public Object readFrom(ProtoStreamReader reader) throws IOException { Class<?> componentType = this.componentType.readFrom(reader); List<Object> list = new LinkedList<>(); while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); if (index == AnyField.ANY.getIndex()) { list.add(Scalar.ANY.readFrom(reader)); } else { reader.skipField(tag); } } Object array = Array.newInstance((componentType == Any.class) ? Object.class : componentType, list.size()); int index = 0; for (Object element : list) { Array.set(array, index++, element); } return array; } @Override public void writeTo(ProtoStreamWriter writer, Object array) throws IOException { this.componentType.writeTo(writer, array.getClass().getComponentType()); for (int i = 0; i < Array.getLength(array); ++i) { Object element = Array.get(array, i); writer.writeTag(AnyField.ANY.getIndex(), Scalar.ANY.getWireType()); Scalar.ANY.writeTo(writer, element); } } @Override public Class<? extends Object> getJavaClass() { return Object.class; } @Override public WireType getWireType() { return this.componentType.getWireType(); } }
3,048
34.870588
115
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/reflect/SynchronizedDecoratorMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.reflect; import java.io.IOException; import java.util.function.UnaryOperator; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * A decorator marshaller that writes the decorated object while holding its monitor lock. * e.g. to enable iteration over a decorated collection without the risk of a ConcurrentModificationException. * @author Paul Ferraro */ public class SynchronizedDecoratorMarshaller<T> extends DecoratorMarshaller<T> { /** * Constructs a decorator marshaller. * @param decoratedClass the generalized type of the decorated object * @param decorator the decoration function * @param sample a sample object used to determine the type of the decorated object */ public SynchronizedDecoratorMarshaller(Class<T> decoratedClass, UnaryOperator<T> decorator, T sample) { super(decoratedClass, decorator, sample); } @Override public void writeTo(ProtoStreamWriter writer, T value) throws IOException { synchronized (value) { super.writeTo(writer, value); } } }
2,167
39.148148
110
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/reflect/TernaryMemberMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2022, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.reflect; import java.lang.reflect.Member; import java.util.function.BiFunction; /** * Generic marshaller based on three non-public members. * @author Paul Ferraro */ public class TernaryMemberMarshaller<T, M extends Member, M1, M2, M3> extends AbstractMemberMarshaller<T, M> { private final Class<M1> member1Type; private final Class<M2> member2Type; private final Class<M3> member3Type; private final TriFunction<M1, M2, M3, T> factory; public TernaryMemberMarshaller(Class<? extends T> type, BiFunction<Object, M, Object> accessor, BiFunction<Class<?>, Class<?>, M> memberLocator, Class<M1> member1Type, Class<M2> member2Type, Class<M3> member3Type, TriFunction<M1, M2, M3, T> factory) { super(type, accessor, memberLocator, member1Type, member2Type, member3Type); this.member1Type = member1Type; this.member2Type = member2Type; this.member3Type = member3Type; this.factory = factory; } @Override public T apply(Object[] parameters) { return this.factory.apply(this.member1Type.cast(parameters[0]), this.member2Type.cast(parameters[1]), this.member3Type.cast(parameters[2])); } }
2,253
42.346154
255
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/reflect/TernaryMethodMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2022, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.reflect; import java.lang.reflect.Method; /** * Generic marshaller based on three non-public accessor methods. * @author Paul Ferraro */ public class TernaryMethodMarshaller<T, M1, M2, M3> extends TernaryMemberMarshaller<T, Method, M1, M2, M3> { public TernaryMethodMarshaller(Class<? extends T> type, Class<M1> member1Type, Class<M2> member2Type, Class<M3> member3Type, TriFunction<M1, M2, M3, T> factory) { super(type, Reflect::invoke, Reflect::findMethod, member1Type, member2Type, member3Type, factory); } }
1,613
42.621622
166
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/reflect/ProxyMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.reflect; import java.io.IOException; import java.lang.reflect.Method; import org.wildfly.clustering.marshalling.protostream.FunctionalScalarMarshaller; import org.wildfly.clustering.marshalling.protostream.Scalar; import org.wildfly.common.function.ExceptionFunction; /** * Marshaller for proxies serialized using the writeReplace()/readResolve() pattern. * @author Paul Ferraro */ public class ProxyMarshaller<T> extends FunctionalScalarMarshaller<T, Object> { public ProxyMarshaller(Class<? extends T> targetClass) { super(targetClass, Scalar.ANY, new ExceptionFunction<T, Object, IOException>() { @Override public Object apply(T object) throws IOException { Method method = Reflect.findMethod(object.getClass(), "writeReplace"); return Reflect.invoke(object, method); } }, new ExceptionFunction<Object, T, IOException>() { @Override public T apply(Object proxy) throws IOException { Method method = Reflect.findMethod(proxy.getClass(), "readResolve"); return Reflect.invoke(proxy, method, targetClass); } }); } }
2,270
41.055556
88
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/reflect/UnaryFieldMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2022, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.reflect; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.security.PrivilegedAction; import java.util.function.Function; import org.wildfly.security.manager.WildFlySecurityManager; /** * Generic marshaller based on a single non-public field. * @author Paul Ferraro */ public class UnaryFieldMarshaller<T, F> extends UnaryMemberMarshaller<T, Field, F> { public UnaryFieldMarshaller(Class<? extends T> targetClass, Class<F> fieldClass, Function<F, T> factory) { super(targetClass, Reflect::getValue, Reflect::findField, fieldClass, factory); } public UnaryFieldMarshaller(Class<? extends T> targetClass, Class<F> fieldClass) { this(targetClass, fieldClass, Reflect.getConstructor(targetClass, fieldClass)); } private UnaryFieldMarshaller(Class<? extends T> targetClass, Class<F> fieldClass, Constructor<? extends T> constructor) { this(targetClass, fieldClass, new Function<>() { @Override public T apply(F value) { return WildFlySecurityManager.doUnchecked(new PrivilegedAction<>() { @Override public T run() { try { return constructor.newInstance(value); } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) { throw new IllegalStateException(e); } } }); } }); } }
2,684
40.307692
125
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/reflect/UnaryMemberMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2022, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.reflect; import java.lang.reflect.Member; import java.util.function.BiFunction; import java.util.function.Function; /** * Generic marshaller based on a single non-public member. * @author Paul Ferraro */ public class UnaryMemberMarshaller<T, M extends Member, M1> extends AbstractMemberMarshaller<T, M> { private final Class<M1> memberType; private final Function<M1, T> factory; public UnaryMemberMarshaller(Class<? extends T> type, BiFunction<Object, M, Object> accessor, BiFunction<Class<?>, Class<?>, M> memberLocator, Class<M1> memberType, Function<M1, T> factory) { super(type, accessor, memberLocator, memberType); this.memberType = memberType; this.factory = factory; } @Override public T apply(Object[] parameters) { return this.factory.apply(this.memberType.cast(parameters[0])); } }
1,941
38.632653
195
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/reflect/UnaryMethodMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2022, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.reflect; import java.lang.reflect.Method; import java.util.function.Function; /** * Generic marshaller based on a single non-public accessor method. * @author Paul Ferraro */ public class UnaryMethodMarshaller<T, M> extends UnaryMemberMarshaller<T, Method, M> { public UnaryMethodMarshaller(Class<? extends T> targetClass, Class<M> fieldClass, Function<M, T> factory) { super(targetClass, Reflect::invoke, Reflect::findMethod, fieldClass, factory); } }
1,554
39.921053
111
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/reflect/BinaryMemberMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2022, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.reflect; import java.lang.reflect.Member; import java.util.function.BiFunction; /** * Generic marshaller based on two non-public members. * @author Paul Ferraro */ public class BinaryMemberMarshaller<T, M extends Member, M1, M2> extends AbstractMemberMarshaller<T, M> { private final Class<M1> member1Type; private final Class<M2> member2Type; private final BiFunction<M1, M2, T> factory; public BinaryMemberMarshaller(Class<? extends T> type, BiFunction<Object, M, Object> accessor, BiFunction<Class<?>, Class<?>, M> memberLocator, Class<M1> member1Type, Class<M2> member2Type, BiFunction<M1, M2, T> factory) { super(type, accessor, memberLocator, member1Type, member2Type); this.member1Type = member1Type; this.member2Type = member2Type; this.factory = factory; } @Override public T apply(Object[] parameters) { return this.factory.apply(this.member1Type.cast(parameters[0]), this.member2Type.cast(parameters[1])); } }
2,080
40.62
226
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/reflect/BinaryMethodMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2022, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.reflect; import java.lang.reflect.Method; import java.util.function.BiFunction; /** * Generic marshaller based on two non-public accessor methods. * @author Paul Ferraro */ public class BinaryMethodMarshaller<T, M1, M2> extends BinaryMemberMarshaller<T, Method, M1, M2> { public BinaryMethodMarshaller(Class<? extends T> type, Class<M1> member1Type, Class<M2> member2Type, BiFunction<M1, M2, T> factory) { super(type, Reflect::invoke, Reflect::findMethod, member1Type, member2Type, factory); } }
1,596
42.162162
137
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/reflect/TernaryFieldMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2022, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.reflect; import java.lang.reflect.Field; /** * Generic marshaller based on three non-public fields. * @author Paul Ferraro */ public class TernaryFieldMarshaller<T, F1, F2, F3> extends TernaryMemberMarshaller<T, Field, F1, F2, F3> { public TernaryFieldMarshaller(Class<? extends T> type, Class<F1> field1Type, Class<F2> field2Type, Class<F3> field3Type, TriFunction<F1, F2, F3, T> factory) { super(type, Reflect::getValue, Reflect::findField, field1Type, field2Type, field3Type, factory); } }
1,594
42.108108
162
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/reflect/Reflect.java
/* * JBoss, Home of Professional Open Source. * Copyright 2022, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.reflect; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.security.PrivilegedAction; import org.wildfly.security.manager.WildFlySecurityManager; /** * Utility methods requiring privileged actions for use by reflection-based marshallers. * Do not change class/method visibility to avoid being called from other {@link java.security.CodeSource}s, thus granting privilege escalation to external code. * @author Paul Ferraro */ final class Reflect { static Field findField(Class<?> sourceClass, Class<?> fieldType) { return WildFlySecurityManager.doUnchecked(new PrivilegedAction<Field>() { @Override public Field run() { for (Field field : sourceClass.getDeclaredFields()) { if (field.getType().isAssignableFrom(fieldType)) { field.setAccessible(true); return field; } } Class<?> superClass = sourceClass.getSuperclass(); if ((superClass == null) || (superClass == Object.class)) { throw new IllegalArgumentException(fieldType.getName()); } return findField(superClass, fieldType); } }); } static Method findMethod(Class<?> sourceClass, Class<?> returnType) { return WildFlySecurityManager.doUnchecked(new PrivilegedAction<Method>() { @Override public Method run() { for (Method method : sourceClass.getDeclaredMethods()) { if ((method.getParameterCount() == 0) && (method.getReturnType() == returnType)) { method.setAccessible(true); return method; } } Class<?> superClass = sourceClass.getSuperclass(); if ((superClass == null) || (superClass == Object.class)) { throw new IllegalArgumentException(returnType.getName()); } return findMethod(superClass, returnType); } }); } static Method findMethod(Class<?> sourceClass, String methodName) { return WildFlySecurityManager.doUnchecked(new PrivilegedAction<Method>() { @Override public Method run() { try { Method method = sourceClass.getDeclaredMethod(methodName); method.setAccessible(true); return method; } catch (NoSuchMethodException e) { throw new IllegalStateException(e); } } }); } static <T> Constructor<T> getConstructor(Class<T> sourceClass, Class<?>... parameterTypes) { return WildFlySecurityManager.doUnchecked(new PrivilegedAction<Constructor<T>>() { @Override public Constructor<T> run() { try { Constructor<T> constructor = sourceClass.getDeclaredConstructor(parameterTypes); constructor.setAccessible(true); return constructor; } catch (NoSuchMethodException e) { throw new IllegalStateException(e); } } }); } static <T> T newInstance(Constructor<T> constructor, Object... parameters) { return WildFlySecurityManager.doUnchecked(new PrivilegedAction<T>() { @Override public T run() { try { return constructor.newInstance(parameters); } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) { throw new IllegalStateException(e); } } }); } static Object getValue(Object source, Field field) { return getValue(source, field, Object.class); } static <T> T getValue(Object source, Field field, Class<T> fieldType) { return WildFlySecurityManager.doUnchecked(new PrivilegedAction<T>() { @Override public T run() { try { return fieldType.cast(field.get(source)); } catch (IllegalAccessException e) { throw new IllegalStateException(e); } } }); } static void setValue(Object source, Field field, Object value) { WildFlySecurityManager.doUnchecked(new PrivilegedAction<Void>() { @Override public Void run() { try { field.set(source, value); return null; } catch (IllegalAccessException e) { throw new IllegalStateException(e); } } }); } static Object invoke(Object source, Method method) { return invoke(source, method, Object.class); } static <T> T invoke(Object source, Method method, Class<T> returnClass) { return WildFlySecurityManager.doUnchecked(new PrivilegedAction<T>() { @Override public T run() { try { return returnClass.cast(method.invoke(source)); } catch (IllegalAccessException | InvocationTargetException e) { throw new IllegalStateException(e); } } }); } static Object invoke(Object source, String methodName) { return invoke(source, methodName, Object.class); } static <T> T invoke(Object source, String methodName, Class<T> returnType) { return WildFlySecurityManager.doUnchecked(new PrivilegedAction<T>() { @Override public T run() { Method method = findMethod(source.getClass(), methodName); return invoke(source, method, returnType); } }); } }
7,128
37.956284
161
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/reflect/AbstractMemberMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2022, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.reflect; import java.io.IOException; import java.lang.reflect.Member; import java.util.ArrayList; import java.util.List; import java.util.function.BiFunction; import java.util.function.Function; import org.infinispan.protostream.descriptors.WireType; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Generic marshaller based on non-public members. * @author Paul Ferraro */ public abstract class AbstractMemberMarshaller<T, M extends Member> implements ProtoStreamMarshaller<T>, Function<Object[], T> { private final Class<? extends T> type; private final BiFunction<Object, M, Object> accessor; private final List<M> members; public AbstractMemberMarshaller(Class<? extends T> type, BiFunction<Object, M, Object> accessor, BiFunction<Class<?>, Class<?>, M> memberLocator, Class<?>... memberTypes) { this.type = type; this.accessor = accessor; this.members = new ArrayList<>(memberTypes.length); for (Class<?> memberType : memberTypes) { this.members.add(memberLocator.apply(type, memberType)); } } @Override public Class<? extends T> getJavaClass() { return this.type; } @Override public T readFrom(ProtoStreamReader reader) throws IOException { Object[] values = new Object[this.members.size()]; while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); if ((index > 0) || (index <= values.length)) { values[index - 1] = reader.readAny(); } else { reader.skipField(tag); } } return this.apply(values); } @Override public void writeTo(ProtoStreamWriter writer, T source) throws IOException { for (int i = 0; i < this.members.size(); ++i) { Object value = this.accessor.apply(source, this.members.get(i)); if (value != null) { writer.writeAny(i + 1, value); } } } }
3,287
37.232558
176
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/reflect/TriFunction.java
/* * JBoss, Home of Professional Open Source. * Copyright 2022, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.reflect; public interface TriFunction<P1, P2, P3, R> { R apply(P1 p1, P2 p2, P3 p3); }
1,170
42.37037
70
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/reflect/DecoratorMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.reflect; import java.util.function.UnaryOperator; /** * Marshaller for a decorator that does not provide public access to its decorated object. * @author Paul Ferraro */ public class DecoratorMarshaller<T> extends UnaryFieldMarshaller<T, T> { public DecoratorMarshaller(Class<T> decoratedClass, UnaryOperator<T> decorator, T sample) { this(decorator.apply(sample).getClass().asSubclass(decoratedClass), decoratedClass, decorator); } private DecoratorMarshaller(Class<? extends T> decoratorClass, Class<T> decoratedClass, UnaryOperator<T> decorator) { super(decoratorClass, decoratedClass, decorator); } }
1,723
42.1
121
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/reflect/FieldMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2022, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.reflect; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.util.function.Supplier; import org.infinispan.protostream.descriptors.WireType; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * A very generic marshaller for use with classes whose state is not publicly available for reading or writing except by pure reflection. * @author Paul Ferraro */ public class FieldMarshaller<T> implements ProtoStreamMarshaller<T> { private final Class<? extends T> type; private final Supplier<? extends T> factory; private final Field[] fields; public FieldMarshaller(Class<? extends T> type, Class<?>... memberTypes) { this(type, defaultFactory(type), memberTypes); } private static <T> Supplier<T> defaultFactory(Class<T> type) { Constructor<T> constructor = Reflect.getConstructor(type); return () -> Reflect.newInstance(constructor); } public FieldMarshaller(Class<? extends T> type, Supplier<? extends T> factory, Class<?>... memberTypes) { this.type = type; this.factory = factory; this.fields = new Field[memberTypes.length]; for (int i = 0; i < this.fields.length; ++i) { this.fields[i] = Reflect.findField(type, memberTypes[i]); } } @Override public Class<? extends T> getJavaClass() { return this.type; } @Override public T readFrom(ProtoStreamReader reader) throws IOException { T result = this.factory.get(); while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); if ((index > 0) || (index <= this.fields.length)) { Reflect.setValue(result, this.fields[index - 1], reader.readAny()); } else { reader.skipField(tag); } } return result; } @Override public void writeTo(ProtoStreamWriter writer, T source) throws IOException { for (int i = 0; i < this.fields.length; ++i) { Object value = Reflect.getValue(source, this.fields[i]); if (value != null) { writer.writeAny(i + 1, value); } } } }
3,514
36.795699
137
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/reflect/BinaryFieldMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2022, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.reflect; import java.lang.reflect.Field; import java.util.function.BiFunction; /** * Generic marshaller based on two non-public fields. * @author Paul Ferraro */ public class BinaryFieldMarshaller<T, F1, F2> extends BinaryMemberMarshaller<T, Field, F1, F2> { public BinaryFieldMarshaller(Class<? extends T> type, Class<F1> field1Type, Class<F2> field2Type, BiFunction<F1, F2, T> factory) { super(type, Reflect::getValue, Reflect::findField, field1Type, field2Type, factory); } }
1,580
40.605263
134
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/sql/SQLMarshallerProvider.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.sql; import java.io.IOException; import java.sql.Date; import java.sql.Time; import java.sql.Timestamp; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import org.wildfly.clustering.marshalling.protostream.FunctionalMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshallerProvider; import org.wildfly.common.function.ExceptionFunction; /** * Marshallers for java.sql.* date/time classes. * @author Paul Ferraro */ public enum SQLMarshallerProvider implements ProtoStreamMarshallerProvider { DATE(Date.class, LocalDate.class, Date::toLocalDate, Date::valueOf), TIME(Time.class, LocalTime.class, Time::toLocalTime, Time::valueOf), TIMESTAMP(Timestamp.class, LocalDateTime.class, Timestamp::toLocalDateTime, Timestamp::valueOf), ; private final ProtoStreamMarshaller<?> marshaller; <T, V> SQLMarshallerProvider(Class<T> targetClass, Class<V> sourceClass, ExceptionFunction<T, V, IOException> function, ExceptionFunction<V, T, IOException> factory) { this.marshaller = new FunctionalMarshaller<>(targetClass, sourceClass, function, factory); } @Override public ProtoStreamMarshaller<?> getMarshaller() { return this.marshaller; } }
2,420
40.033898
171
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/LinkedHashMapMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.io.IOException; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import org.infinispan.protostream.descriptors.WireType; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; import org.wildfly.clustering.marshalling.spi.util.LinkedHashMapExternalizer; /** * Marshaller for a {@link LinkedHashMap}. * @author Paul Ferraro */ public class LinkedHashMapMarshaller extends AbstractMapMarshaller<LinkedHashMap<Object, Object>> { private static final int ACCESS_ORDER_INDEX = VALUE_INDEX + 1; @SuppressWarnings("unchecked") public LinkedHashMapMarshaller() { super((Class<LinkedHashMap<Object, Object>>) (Class<?>) LinkedHashMap.class); } @Override public LinkedHashMap<Object, Object> readFrom(ProtoStreamReader reader) throws IOException { LinkedHashMap<Object, Object> map = new LinkedHashMap<>(16, 0.75f, false); List<Object> keys = new LinkedList<>(); List<Object> values = new LinkedList<>(); while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); switch (index) { case KEY_INDEX: keys.add(reader.readAny()); break; case VALUE_INDEX: values.add(reader.readAny()); break; case ACCESS_ORDER_INDEX: map = new LinkedHashMap<>(16, 0.75f, reader.readBool()); break; default: reader.skipField(tag); } } Iterator<Object> keyIterator = keys.iterator(); Iterator<Object> valueIterator = values.iterator(); while (keyIterator.hasNext() || valueIterator.hasNext()) { map.put(keyIterator.next(), valueIterator.next()); } return map; } @Override public void writeTo(ProtoStreamWriter writer, LinkedHashMap<Object, Object> map) throws IOException { synchronized (map) { // Avoid ConcurrentModificationException super.writeTo(writer, map); boolean accessOrder = LinkedHashMapExternalizer.ACCESS_ORDER.apply(map); if (accessOrder) { writer.writeBool(ACCESS_ORDER_INDEX, accessOrder); } } } }
3,555
38.511111
105
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/SortedSetMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.io.IOException; import java.util.Comparator; import java.util.SortedSet; import java.util.function.Function; import org.infinispan.protostream.descriptors.WireType; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Marshaller for a {@link SortedSet}. * @author Paul Ferraro * @param <T> the set type of this marshaller */ public class SortedSetMarshaller<T extends SortedSet<Object>> extends AbstractCollectionMarshaller<T> { private static final int COMPARATOR_INDEX = 2; private final Function<Comparator<? super Object>, T> factory; @SuppressWarnings("unchecked") public SortedSetMarshaller(Function<Comparator<? super Object>, T> factory) { super((Class<T>) factory.apply((Comparator<Object>) ComparatorMarshaller.INSTANCE.getBuilder()).getClass()); this.factory = factory; } @SuppressWarnings("unchecked") @Override public T readFrom(ProtoStreamReader reader) throws IOException { Comparator<Object> comparator = (Comparator<Object>) ComparatorMarshaller.INSTANCE.getBuilder(); T set = this.factory.apply(comparator); while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); if (index == ELEMENT_INDEX) { set.add(reader.readAny()); } else if ((index >= COMPARATOR_INDEX) && (index < COMPARATOR_INDEX + ComparatorMarshaller.INSTANCE.getFields())) { T existing = set; comparator = (Comparator<Object>) ComparatorMarshaller.INSTANCE.readField(reader, index - COMPARATOR_INDEX, comparator); set = this.factory.apply(comparator); set.addAll(existing); } else { reader.skipField(tag); } } return set; } @Override public void writeTo(ProtoStreamWriter writer, T set) throws IOException { super.writeTo(writer, set); Comparator<?> comparator = set.comparator(); if (comparator != ComparatorMarshaller.INSTANCE.getBuilder()) { ComparatorMarshaller.INSTANCE.writeFields(writer, COMPARATOR_INDEX, set.comparator()); } } }
3,388
39.831325
136
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/SingletonMapMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.util.Map; import java.io.IOException; import java.util.AbstractMap.SimpleEntry; import java.util.function.BiFunction; import java.util.function.Function; import org.wildfly.clustering.marshalling.protostream.FunctionalMarshaller; import org.wildfly.common.function.ExceptionFunction; /** * Marshaller for a singleton map. * @author Paul Ferraro */ public class SingletonMapMarshaller extends FunctionalMarshaller<Map<Object, Object>, SimpleEntry<Object, Object>> { private static final ExceptionFunction<Map<Object, Object>, SimpleEntry<Object, Object>, IOException> FUNCTION = new ExceptionFunction<>() { @Override public SimpleEntry<Object, Object> apply(Map<Object, Object> map) { return new SimpleEntry<>(map.entrySet().iterator().next()); } }; @SuppressWarnings("unchecked") public SingletonMapMarshaller(BiFunction<Object, Object, Map<Object, Object>> factory) { super((Class<Map<Object, Object>>) factory.apply(null, null).getClass(), new MapEntryMarshaller<>(Function.identity()), FUNCTION, new ExceptionFunction<SimpleEntry<Object, Object>, Map<Object, Object>, IOException>() { @Override public Map<Object, Object> apply(SimpleEntry<Object, Object> entry) { return factory.apply(entry.getKey(), entry.getValue()); } }); } }
2,462
42.982143
226
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/CalendarMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.io.IOException; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; import org.infinispan.protostream.descriptors.WireType; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Mashaller for a {@link java.util.Calendar}. * @author Paul Ferraro */ public class CalendarMarshaller implements ProtoStreamMarshaller<Calendar> { private static final int TYPE_INDEX = 1; private static final int TIME_INDEX = 2; private static final int LENIENT_INDEX = 3; private static final int TIME_ZONE_INDEX = 4; private static final int FIRST_DAY_OF_WEEK_INDEX = 5; private static final int MIN_DAYS_IN_FIRST_WEEK_INDEX = 6; private static final Calendar DEFAULT = new Calendar.Builder().setInstant(0).build(); @Override public Calendar readFrom(ProtoStreamReader reader) throws IOException { Calendar.Builder builder = new Calendar.Builder().setInstant(0); int firstDayOfWeek = DEFAULT.getFirstDayOfWeek(); int minDaysInFirstWeek = DEFAULT.getMinimalDaysInFirstWeek(); while (!reader.isAtEnd()) { int tag = reader.readTag(); switch (WireType.getTagFieldNumber(tag)) { case TYPE_INDEX: builder.setCalendarType(reader.readString()); break; case TIME_INDEX: builder.setInstant(reader.readObject(Date.class)); break; case LENIENT_INDEX: builder.setLenient(reader.readBool()); break; case TIME_ZONE_INDEX: builder.setTimeZone(TimeZone.getTimeZone(reader.readString())); break; case FIRST_DAY_OF_WEEK_INDEX: firstDayOfWeek = reader.readUInt32(); break; case MIN_DAYS_IN_FIRST_WEEK_INDEX: minDaysInFirstWeek = reader.readUInt32(); break; default: reader.skipField(tag); } } return builder.setWeekDefinition(firstDayOfWeek, minDaysInFirstWeek).build(); } @Override public void writeTo(ProtoStreamWriter writer, Calendar calendar) throws IOException { String type = calendar.getCalendarType(); if (!type.equals(DEFAULT.getCalendarType())) { writer.writeString(TYPE_INDEX, type); } Date time = calendar.getTime(); if (!time.equals(DEFAULT.getTime())) { writer.writeObject(TIME_INDEX, time); } boolean lenient = calendar.isLenient(); if (lenient != DEFAULT.isLenient()) { writer.writeBool(LENIENT_INDEX, lenient); } TimeZone zone = calendar.getTimeZone(); if (!zone.equals(DEFAULT.getTimeZone())) { writer.writeString(TIME_ZONE_INDEX, zone.getID()); } int firstDayOfWeek = calendar.getFirstDayOfWeek(); if (firstDayOfWeek != DEFAULT.getFirstDayOfWeek()) { writer.writeUInt32(FIRST_DAY_OF_WEEK_INDEX, firstDayOfWeek); } int minDaysInFirstWeek = calendar.getMinimalDaysInFirstWeek(); if (minDaysInFirstWeek != DEFAULT.getMinimalDaysInFirstWeek()) { writer.writeUInt32(MIN_DAYS_IN_FIRST_WEEK_INDEX, minDaysInFirstWeek); } } @Override public Class<? extends Calendar> getJavaClass() { return Calendar.class; } }
4,735
39.827586
89
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/EnumSetMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.util.EnumSet; import org.wildfly.clustering.marshalling.protostream.ProtoStreamBuilderFieldSetMarshaller; /** * Marshaller for an {@link EnumSet}. * @author Paul Ferraro * @param <E> the enum type of this marshaller */ public class EnumSetMarshaller<E extends Enum<E>> extends ProtoStreamBuilderFieldSetMarshaller<EnumSet<E>, EnumSetBuilder<E>> { @SuppressWarnings("unchecked") public EnumSetMarshaller() { super((Class<EnumSet<E>>) (Class<?>) EnumSet.class, new EnumSetFieldSetMarshaller<>()); } }
1,624
38.634146
127
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/MapMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.io.IOException; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.function.Supplier; import org.infinispan.protostream.descriptors.WireType; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; /** * Marshaller for a {@link Map}. * @author Paul Ferraro * @param <T> the map type of this marshaller */ public class MapMarshaller<T extends Map<Object, Object>> extends AbstractMapMarshaller<T> { private final Supplier<T> factory; @SuppressWarnings("unchecked") public MapMarshaller(Supplier<T> factory) { super((Class<T>) factory.get().getClass()); this.factory = factory; } @Override public T readFrom(ProtoStreamReader reader) throws IOException { T map = this.factory.get(); List<Object> keys = new LinkedList<>(); List<Object> values = new LinkedList<>(); while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); switch (index) { case KEY_INDEX: keys.add(reader.readAny()); break; case VALUE_INDEX: values.add(reader.readAny()); break; default: reader.skipField(tag); } } Iterator<Object> keyIterator = keys.iterator(); Iterator<Object> valueIterator = values.iterator(); while (keyIterator.hasNext() || valueIterator.hasNext()) { map.put(keyIterator.next(), valueIterator.next()); } return map; } }
2,774
35.038961
92
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/OptionalMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.io.IOException; import java.util.Optional; import java.util.OptionalDouble; import java.util.OptionalInt; import java.util.OptionalLong; import java.util.function.Supplier; import org.wildfly.clustering.marshalling.protostream.FunctionalScalarMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshallerProvider; import org.wildfly.clustering.marshalling.protostream.Scalar; import org.wildfly.clustering.marshalling.protostream.ScalarMarshaller; import org.wildfly.common.function.ExceptionFunction; import org.wildfly.common.function.ExceptionPredicate; /** * Marshallers for java.util.Optional* instances. * @author Paul Ferraro */ public enum OptionalMarshaller implements ProtoStreamMarshallerProvider { OBJECT(Scalar.ANY, Optional::empty, Optional::isPresent, Optional::get, Optional::of), DOUBLE(Scalar.DOUBLE.cast(Double.class), OptionalDouble::empty, OptionalDouble::isPresent, OptionalDouble::getAsDouble, OptionalDouble::of), INT(Scalar.INTEGER.cast(Integer.class), OptionalInt::empty, OptionalInt::isPresent, OptionalInt::getAsInt, OptionalInt::of), LONG(Scalar.LONG.cast(Long.class), OptionalLong::empty, OptionalLong::isPresent, OptionalLong::getAsLong, OptionalLong::of), ; private final ProtoStreamMarshaller<?> marshaller; <T, V> OptionalMarshaller(ScalarMarshaller<V> marshaller, Supplier<T> defaultFactory, ExceptionPredicate<T, IOException> present, ExceptionFunction<T, V, IOException> function, ExceptionFunction<V, T, IOException> factory) { this.marshaller = new FunctionalScalarMarshaller<>(marshaller, defaultFactory, present.not(), function, factory); } @Override public ProtoStreamMarshaller<?> getMarshaller() { return this.marshaller; } }
2,939
46.419355
228
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/UnmodifiableMapMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2022, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.function.Function; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.SimpleFunctionalMarshaller; import org.wildfly.common.function.ExceptionFunction; /** * Marshaller for unmodifiable maps created via {@link java.util.Map#of()} or {@link java.util.Map#ofEntries()} methods. * @author Paul Ferraro */ public class UnmodifiableMapMarshaller<T extends Map<Object, Object>> extends SimpleFunctionalMarshaller<T, Map<Object, Object>> { private static final ProtoStreamMarshaller<Map<Object, Object>> MARSHALLER = new MapMarshaller<>(HashMap::new); public UnmodifiableMapMarshaller(Class<T> targetClass, Function<Map.Entry<? extends Object, ? extends Object>[], T> factory) { super(targetClass, MARSHALLER, new ExceptionFunction<Map<Object, Object>, T, IOException>() { @Override public T apply(Map<Object, Object> map) { @SuppressWarnings("unchecked") Map.Entry<Object, Object>[] entries = new Map.Entry[0]; return factory.apply(map.entrySet().toArray(entries)); } }); } }
2,347
44.153846
130
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/UnmodifiableCollectionMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2022, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.io.IOException; import java.util.Collection; import java.util.LinkedList; import java.util.function.Function; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.SimpleFunctionalMarshaller; import org.wildfly.common.function.ExceptionFunction; /** * Marshaller for unmodifiable collections created via {@link java.util.List#of()} or {@link java.util.Set#of()} methods. * @author Paul Ferraro */ public class UnmodifiableCollectionMarshaller<E, T extends Collection<Object>> extends SimpleFunctionalMarshaller<T, Collection<Object>> { private static final ProtoStreamMarshaller<Collection<Object>> MARSHALLER = new CollectionMarshaller<>(LinkedList::new); public UnmodifiableCollectionMarshaller(Class<T> targetClass, Function<Object[], T> factory) { super(targetClass, MARSHALLER, new ExceptionFunction<>() { @Override public T apply(Collection<Object> collection) throws IOException { return factory.apply(collection.toArray()); } }); } }
2,204
42.235294
138
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/CollectionMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.io.IOException; import java.util.Collection; import java.util.function.Supplier; import org.infinispan.protostream.descriptors.WireType; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; /** * Marshaller for a basic collection. * @author Paul Ferraro * @param <T> the collection type of this marshaller */ public class CollectionMarshaller<T extends Collection<Object>> extends AbstractCollectionMarshaller<T> { private final Supplier<T> factory; @SuppressWarnings("unchecked") public CollectionMarshaller(Supplier<T> factory) { super((Class<T>) factory.get().getClass()); this.factory = factory; } @Override public T readFrom(ProtoStreamReader reader) throws IOException { T collection = this.factory.get(); while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); switch (index) { case ELEMENT_INDEX: collection.add(reader.readAny()); break; default: reader.skipField(tag); } } return collection; } }
2,292
34.828125
105
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/EnumMapMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.io.IOException; import java.lang.reflect.Field; import java.security.PrivilegedAction; import java.util.EnumMap; import java.util.EnumSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import org.infinispan.protostream.descriptors.WireType; import org.wildfly.clustering.marshalling.protostream.FieldSetMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; import org.wildfly.security.manager.WildFlySecurityManager; /** * Marshaller for an {@link EnumMap}. * @author Paul Ferraro * @param <E> the enum key type of this marshaller */ public class EnumMapMarshaller<E extends Enum<E>> implements ProtoStreamMarshaller<EnumMap<E, Object>> { static final Field ENUM_MAP_KEY_CLASS_FIELD = WildFlySecurityManager.doUnchecked(new PrivilegedAction<Field>() { @Override public Field run() { for (Field field : EnumMap.class.getDeclaredFields()) { if (field.getType() == Class.class) { field.setAccessible(true); return field; } } throw new IllegalStateException(); } }); private static final int ENUM_SET_INDEX = 1; private final FieldSetMarshaller<EnumSet<E>, EnumSetBuilder<E>> marshaller = new EnumSetFieldSetMarshaller<>(); private final int valueIndex = this.marshaller.getFields() + 1; @Override public EnumMap<E, Object> readFrom(ProtoStreamReader reader) throws IOException { EnumSetBuilder<E> builder = this.marshaller.getBuilder(); List<Object> values = new LinkedList<>(); while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); if ((index >= ENUM_SET_INDEX) && (index < ENUM_SET_INDEX + this.marshaller.getFields())) { builder = this.marshaller.readField(reader, index - ENUM_SET_INDEX, builder); } else if (index == this.valueIndex) { values.add(reader.readAny()); } else { reader.skipField(tag); } } EnumSet<E> enumSet = builder.build(); Iterator<E> enumValues = enumSet.iterator(); EnumMap<E, Object> enumMap = new EnumMap<>(builder.getEnumClass()); for (Object value : values) { enumMap.put(enumValues.next(), value); } return enumMap; } @Override public void writeTo(ProtoStreamWriter writer, EnumMap<E, Object> map) throws IOException { EnumSet<E> set = EnumSet.noneOf(this.findEnumClass(map)); set.addAll(map.keySet()); this.marshaller.writeFields(writer, ENUM_SET_INDEX, set); for (Object value : map.values()) { writer.writeAny(this.valueIndex, value); } } @SuppressWarnings("unchecked") @Override public Class<? extends EnumMap<E, Object>> getJavaClass() { return (Class<EnumMap<E, Object>>) (Class<?>) EnumMap.class; } private Class<E> findEnumClass(EnumMap<E, Object> map) { Iterator<E> values = map.keySet().iterator(); if (values.hasNext()) { return values.next().getDeclaringClass(); } // If EnumMap is empty, we need to resort to reflection to obtain the enum type return WildFlySecurityManager.doUnchecked(new PrivilegedAction<Class<E>>() { @SuppressWarnings("unchecked") @Override public Class<E> run() { try { return (Class<E>) ENUM_MAP_KEY_CLASS_FIELD.get(map); } catch (IllegalAccessException e) { throw new IllegalStateException(e); } } }); } }
5,010
38.769841
116
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/AbstractCollectionMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.io.IOException; import java.util.Collection; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Abstract collection marshaller that writes the elements of the collection. * @author Paul Ferraro * @param <T> the collection type of this marshaller */ public abstract class AbstractCollectionMarshaller<T extends Collection<Object>> implements ProtoStreamMarshaller<T> { protected static final int ELEMENT_INDEX = 1; private final Class<? extends T> collectionClass; public AbstractCollectionMarshaller(Class<? extends T> collectionClass) { this.collectionClass = collectionClass; } @Override public void writeTo(ProtoStreamWriter writer, T collection) throws IOException { synchronized (collection) { // Avoid ConcurrentModificationException for (Object element : collection) { writer.writeAny(ELEMENT_INDEX, element); } } } @Override public Class<? extends T> getJavaClass() { return this.collectionClass; } }
2,235
36.266667
118
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/ComparatorMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.io.IOException; import java.util.Comparator; import org.wildfly.clustering.marshalling.protostream.FieldSetMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Marshaller for the fields of a {@link Comparator}. * @author Paul Ferraro */ public enum ComparatorMarshaller implements FieldSetMarshaller<Comparator<?>, Comparator<?>> { INSTANCE; private static final int REVERSE_INDEX = 0; private static final int COMPARATOR_INDEX = 1; private static final int FIELDS = 2; @Override public Comparator<?> getBuilder() { return Comparator.naturalOrder(); } @Override public int getFields() { return FIELDS; } @Override public Comparator<?> readField(ProtoStreamReader reader, int index, Comparator<?> comparator) throws IOException { switch (index) { case REVERSE_INDEX: return reader.readBool() ? Comparator.reverseOrder() : Comparator.naturalOrder(); case COMPARATOR_INDEX: return reader.readAny(Comparator.class); default: throw new IllegalArgumentException(Integer.toString(index)); } } @Override public void writeFields(ProtoStreamWriter writer, int startIndex, Comparator<?> comparator) throws IOException { boolean natural = comparator == Comparator.naturalOrder(); boolean reverse = comparator == Comparator.reverseOrder(); if (natural || reverse) { writer.writeBool(startIndex + REVERSE_INDEX, reverse); } else { writer.writeAny(startIndex + COMPARATOR_INDEX, comparator); } } }
2,846
36.460526
118
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/SingletonCollectionMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.util.Collection; import java.util.function.Function; import org.wildfly.clustering.marshalling.protostream.FunctionalScalarMarshaller; import org.wildfly.clustering.marshalling.protostream.Scalar; import org.wildfly.common.function.Functions; /** * Marshaller for singleton collections. * @author Paul Ferraro * @param <T> the collection type of this marshaller */ public class SingletonCollectionMarshaller<T extends Collection<Object>> extends FunctionalScalarMarshaller<T, Object> { public SingletonCollectionMarshaller(Function<Object, T> factory) { super(Scalar.ANY, Functions.constantSupplier(factory.apply(null)), collection -> collection.iterator().next(), factory::apply); } }
1,807
41.046512
135
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/UUIDMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.io.IOException; import java.util.UUID; import org.wildfly.clustering.marshalling.protostream.FieldSetMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Marshaller for a {@link UUID} using fixed size longs. * @author Paul Ferraro */ public enum UUIDMarshaller implements FieldSetMarshaller<UUID, UUIDBuilder> { INSTANCE; private static final long DEFAULT_SIGNIFICANT_BITS = 0; private static final int MOST_SIGNIFICANT_BITS_INDEX = 0; private static final int LEAST_SIGNIFICANT_BITS_INDEX = 1; private static final int FIELDS = 2; @Override public UUIDBuilder getBuilder() { return new DefaultUUIDBuilder(); } @Override public int getFields() { return FIELDS; } @Override public UUIDBuilder readField(ProtoStreamReader reader, int index, UUIDBuilder builder) throws IOException { switch (index) { case MOST_SIGNIFICANT_BITS_INDEX: return builder.setMostSignificantBits(reader.readSFixed64()); case LEAST_SIGNIFICANT_BITS_INDEX: return builder.setLeastSignificantBits(reader.readSFixed64()); default: return builder; } } @Override public void writeFields(ProtoStreamWriter writer, int startIndex, UUID uuid) throws IOException { long mostSignificantBits = uuid.getMostSignificantBits(); if (mostSignificantBits != DEFAULT_SIGNIFICANT_BITS) { writer.writeSFixed64(startIndex + MOST_SIGNIFICANT_BITS_INDEX, mostSignificantBits); } long leastSignificantBits = uuid.getLeastSignificantBits(); if (leastSignificantBits != DEFAULT_SIGNIFICANT_BITS) { writer.writeSFixed64(startIndex + LEAST_SIGNIFICANT_BITS_INDEX, leastSignificantBits); } } static class DefaultUUIDBuilder implements UUIDBuilder { private long mostSignificantBits = DEFAULT_SIGNIFICANT_BITS; private long leastSignificantBits = DEFAULT_SIGNIFICANT_BITS; @Override public UUIDBuilder setMostSignificantBits(long bits) { this.mostSignificantBits = bits; return this; } @Override public UUIDBuilder setLeastSignificantBits(long bits) { this.leastSignificantBits = bits; return this; } @Override public UUID build() { return new UUID(this.mostSignificantBits, this.leastSignificantBits); } } }
3,688
35.524752
111
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/MapEntryMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.io.IOException; import java.util.AbstractMap.SimpleEntry; import java.util.Map; import java.util.function.Function; import org.infinispan.protostream.descriptors.WireType; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Marshaller for a {@link java.util.Map.Entry} * @author Paul Ferraro * @param <T> the map entry type of this marshaller */ public class MapEntryMarshaller<T extends Map.Entry<Object, Object>> implements ProtoStreamMarshaller<T> { private static final int KEY_INDEX = 1; private static final int VALUE_INDEX = 2; private final Class<? extends T> targetClass; private final Function<SimpleEntry<Object, Object>, T> factory; @SuppressWarnings("unchecked") public MapEntryMarshaller(Function<SimpleEntry<Object, Object>, T> factory) { this.targetClass = (Class<T>) factory.apply(new SimpleEntry<>(null, null)).getClass(); this.factory = factory; } @Override public T readFrom(ProtoStreamReader reader) throws IOException { SimpleEntry<Object, Object> entry = new SimpleEntry<>(null, null); while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); switch (index) { case KEY_INDEX: Object key = reader.readAny(); entry = new SimpleEntry<>(key, entry.getValue()); break; case VALUE_INDEX: Object value = reader.readAny(); entry.setValue(value); break; default: reader.skipField(tag); } } return this.factory.apply(entry); } @Override public void writeTo(ProtoStreamWriter writer, T entry) throws IOException { Object key = entry.getKey(); if (key != null) { writer.writeAny(KEY_INDEX, key); } Object value = entry.getValue(); if (key != null) { writer.writeAny(VALUE_INDEX, value); } } @Override public Class<? extends T> getJavaClass() { return this.targetClass; } }
3,443
36.032258
106
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/EnumSetFieldSetMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.io.IOException; import java.lang.reflect.Field; import java.security.PrivilegedAction; import java.util.BitSet; import java.util.EnumSet; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import org.wildfly.clustering.marshalling.protostream.FieldSetMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; import org.wildfly.security.manager.WildFlySecurityManager; /** * Marshaller for the fields of an {@link EnumSet}. * @author Paul Ferraro * @param <E> the enum type for this marshaller */ public class EnumSetFieldSetMarshaller<E extends Enum<E>> implements FieldSetMarshaller<EnumSet<E>, EnumSetBuilder<E>> { static final Field ENUM_SET_CLASS_FIELD = WildFlySecurityManager.doUnchecked(new PrivilegedAction<Field>() { @Override public Field run() { for (Field field : EnumSet.class.getDeclaredFields()) { if (field.getType() == Class.class) { field.setAccessible(true); return field; } } throw new IllegalStateException(); } }); private static final int CLASS_INDEX = 0; private static final int COMPLEMENT_CLASS_INDEX = 1; private static final int BITS_INDEX = 2; private static final int ELEMENT_INDEX = 3; private static final int FIELDS = 4; @Override public EnumSetBuilder<E> getBuilder() { return new DefaultEnumSetBuilder<>(); } @Override public int getFields() { return FIELDS; } @Override public EnumSetBuilder<E> readField(ProtoStreamReader reader, int index, EnumSetBuilder<E> builder) throws IOException { switch (index) { case CLASS_INDEX: return builder.setComplement(false).setEnumClass(reader.readObject(Class.class)); case COMPLEMENT_CLASS_INDEX: return builder.setComplement(true).setEnumClass(reader.readObject(Class.class)); case BITS_INDEX: return builder.setBits(BitSet.valueOf(reader.readByteArray())); case ELEMENT_INDEX: return builder.add(reader.readUInt32()); default: throw new IllegalArgumentException(Integer.toString(index)); } } @Override public void writeFields(ProtoStreamWriter writer, int startIndex, EnumSet<E> set) throws IOException { Class<?> enumClass = this.findEnumClass(set); Object[] values = enumClass.getEnumConstants(); // Marshal the smaller of the set versus the set's complement boolean complement = set.size() * 2 > values.length; writer.writeObject(startIndex + (complement ? COMPLEMENT_CLASS_INDEX : CLASS_INDEX), enumClass); EnumSet<E> targetSet = complement ? EnumSet.complementOf(set) : set; // Write as BitSet or individual elements depending on size if (((values.length + Byte.SIZE - 1) / Byte.SIZE) < targetSet.size()) { BitSet bits = new BitSet(values.length); for (int i = 0; i < values.length; ++i) { bits.set(i, targetSet.contains(values[i])); } writer.writeBytes(startIndex + BITS_INDEX, bits.toByteArray()); } else { for (E value : targetSet) { writer.writeUInt32(startIndex + ELEMENT_INDEX, value.ordinal()); } } } private Class<?> findEnumClass(EnumSet<E> set) { EnumSet<E> nonEmptySet = set.isEmpty() ? EnumSet.complementOf(set) : set; Iterator<E> values = nonEmptySet.iterator(); if (values.hasNext()) { return values.next().getDeclaringClass(); } // Java allows enums with no values - thus one could technically create an empty EnumSet for such an enum // While this is unlikely, we need to resort to reflection to obtain the enum type return WildFlySecurityManager.doUnchecked(new PrivilegedAction<Class<?>>() { @Override public Class<?> run() { try { return (Class<?>) ENUM_SET_CLASS_FIELD.get(set); } catch (IllegalAccessException e) { throw new IllegalStateException(e); } } }); } static class DefaultEnumSetBuilder<E extends Enum<E>> implements EnumSetBuilder<E> { private final List<Integer> elements = new LinkedList<>(); private Class<E> enumClass = null; private boolean complement = false; private BitSet bits = null; @Override public EnumSetBuilder<E> setEnumClass(Class<E> enumClass) { this.enumClass = enumClass; return this; } @Override public EnumSetBuilder<E> setComplement(boolean complement) { this.complement = complement; return this; } @Override public EnumSetBuilder<E> setBits(BitSet bits) { this.bits = bits; return this; } @Override public EnumSetBuilder<E> add(int ordinal) { this.elements.add(ordinal); return this; } @Override public Class<E> getEnumClass() { return this.enumClass; } @Override public EnumSet<E> build() { EnumSet<E> set = EnumSet.noneOf(this.enumClass); E[] values = this.enumClass.getEnumConstants(); if (this.bits != null) { for (int i = 0; i < values.length; ++i) { if (this.bits.get(i)) { set.add(values[i]); } } } else { for (Integer element : this.elements) { set.add(values[element]); } } return this.complement ? EnumSet.complementOf(set) : set; } } }
7,121
36.287958
123
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/UUIDBuilder.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.util.UUID; import org.wildfly.clustering.marshalling.protostream.ProtoStreamBuilder; /** * @author Paul Ferraro */ public interface UUIDBuilder extends ProtoStreamBuilder<UUID> { UUIDBuilder setMostSignificantBits(long bits); UUIDBuilder setLeastSignificantBits(long bits); }
1,388
35.552632
73
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/SortedMapMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.io.IOException; import java.util.Comparator; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.SortedMap; import java.util.function.Function; import org.infinispan.protostream.descriptors.WireType; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Marshaller for a {@link SortedMap}. * @author Paul Ferraro * @param <T> the map type of this marshaller */ public class SortedMapMarshaller<T extends SortedMap<Object, Object>> extends AbstractMapMarshaller<T> { private static final int COMPARATOR_INDEX = VALUE_INDEX + 1; private final Function<Comparator<? super Object>, T> factory; @SuppressWarnings("unchecked") public SortedMapMarshaller(Function<Comparator<? super Object>, T> factory) { super((Class<T>) factory.apply((Comparator<Object>) ComparatorMarshaller.INSTANCE.getBuilder()).getClass()); this.factory = factory; } @SuppressWarnings("unchecked") @Override public T readFrom(ProtoStreamReader reader) throws IOException { Comparator<Object> comparator = (Comparator<Object>) ComparatorMarshaller.INSTANCE.getBuilder(); T map = this.factory.apply(comparator); List<Object> keys = new LinkedList<>(); List<Object> values = new LinkedList<>(); while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); if (index == KEY_INDEX) { keys.add(reader.readAny()); } else if (index == VALUE_INDEX) { values.add(reader.readAny()); } else if ((index >= COMPARATOR_INDEX) && (index < COMPARATOR_INDEX + ComparatorMarshaller.INSTANCE.getFields())) { comparator = (Comparator<Object>) ComparatorMarshaller.INSTANCE.readField(reader, index - COMPARATOR_INDEX, comparator); map = this.factory.apply(comparator); } else { reader.skipField(tag); } } Iterator<Object> keyIterator = keys.iterator(); Iterator<Object> valueIterator = values.iterator(); while (keyIterator.hasNext() || valueIterator.hasNext()) { map.put(keyIterator.next(), valueIterator.next()); } return map; } @Override public void writeTo(ProtoStreamWriter writer, T map) throws IOException { super.writeTo(writer, map); Comparator<?> comparator = map.comparator(); if (comparator != ComparatorMarshaller.INSTANCE.getBuilder()) { ComparatorMarshaller.INSTANCE.writeFields(writer, COMPARATOR_INDEX, comparator); } } }
3,847
40.826087
136
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/AbstractMapMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.io.IOException; import java.util.Map; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Abstract marshaller for a {@link Map} that writes the entries of the map. * @author Paul Ferraro * @param <T> the map type of this marshaller */ public abstract class AbstractMapMarshaller<T extends Map<Object, Object>> implements ProtoStreamMarshaller<T> { protected static final int KEY_INDEX = 1; protected static final int VALUE_INDEX = 2; private final Class<? extends T> mapClass; public AbstractMapMarshaller(Class<? extends T> mapClass) { this.mapClass = mapClass; } @Override public void writeTo(ProtoStreamWriter writer, T map) throws IOException { synchronized (map) { // Avoid ConcurrentModificationException for (Map.Entry<Object, Object> entry : map.entrySet()) { writer.writeAny(KEY_INDEX, entry.getKey()); writer.writeAny(VALUE_INDEX, entry.getValue()); } } } @Override public Class<? extends T> getJavaClass() { return this.mapClass; } }
2,289
36.540984
112
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/EnumSetBuilder.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.util.BitSet; import java.util.EnumSet; import org.wildfly.clustering.marshalling.protostream.ProtoStreamBuilder; /** * Builder for an {@link EnumSet}. * @author Paul Ferraro * @param <E> the enum type of this builder */ public interface EnumSetBuilder<E extends Enum<E>> extends ProtoStreamBuilder<EnumSet<E>> { EnumSetBuilder<E> setEnumClass(Class<E> enumClass); EnumSetBuilder<E> setComplement(boolean complement); EnumSetBuilder<E> setBits(BitSet bits); EnumSetBuilder<E> add(int ordinal); Class<E> getEnumClass(); }
1,649
34.106383
91
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/UtilMarshallerProvider.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util; import java.time.Instant; import java.util.AbstractMap; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.BitSet; import java.util.Collection; import java.util.Collections; import java.util.Currency; import java.util.Date; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.NavigableMap; import java.util.NavigableSet; import java.util.Set; import java.util.SortedMap; import java.util.SortedSet; import java.util.TimeZone; import java.util.TreeMap; import java.util.TreeSet; import org.wildfly.clustering.marshalling.protostream.FunctionalMarshaller; import org.wildfly.clustering.marshalling.protostream.FunctionalScalarMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamBuilderFieldSetMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshallerProvider; import org.wildfly.clustering.marshalling.protostream.Scalar; import org.wildfly.clustering.marshalling.protostream.ValueMarshaller; import org.wildfly.clustering.marshalling.protostream.reflect.DecoratorMarshaller; import org.wildfly.clustering.marshalling.protostream.reflect.SynchronizedDecoratorMarshaller; import org.wildfly.common.function.Functions; /** * Enumeration of java.util marshallers. * @author Paul Ferraro */ public enum UtilMarshallerProvider implements ProtoStreamMarshallerProvider { ARRAY_DEQUE(new CollectionMarshaller<>(ArrayDeque::new)), ARRAY_LIST(new CollectionMarshaller<>(ArrayList::new)), BIT_SET(new FunctionalScalarMarshaller<>(Scalar.BYTE_ARRAY.cast(byte[].class), BitSet::new, BitSet::isEmpty, BitSet::toByteArray, BitSet::valueOf)), CALENDAR(new CalendarMarshaller()), CURRENCY(new FunctionalScalarMarshaller<>(Currency.class, Scalar.STRING.cast(String.class), Functions.constantSupplier(getDefaultCurrency()), Currency::getCurrencyCode, Currency::getInstance)), DATE(new FunctionalMarshaller<>(Date.class, Instant.class, Date::toInstant, Date::from)), EMPTY_LIST(new ValueMarshaller<>(Collections.emptyList())), EMPTY_MAP(new ValueMarshaller<>(Collections.emptyMap())), EMPTY_NAVIGABLE_MAP(new ValueMarshaller<>(Collections.emptyNavigableMap())), EMPTY_NAVIGABLE_SET(new ValueMarshaller<>(Collections.emptyNavigableSet())), EMPTY_SET(new ValueMarshaller<>(Collections.emptySet())), EMPTY_SORTED_MAP(new ValueMarshaller<>(Collections.emptySortedMap())), EMPTY_SORTED_SET(new ValueMarshaller<>(Collections.emptySortedSet())), ENUM_MAP(new EnumMapMarshaller<>()), ENUM_SET(new EnumSetMarshaller<>()), HASH_MAP(new MapMarshaller<>(HashMap::new)), HASH_SET(new CollectionMarshaller<>(HashSet::new)), LINKED_HASH_MAP(new LinkedHashMapMarshaller()), LINKED_HASH_SET(new CollectionMarshaller<>(LinkedHashSet::new)), LINKED_LIST(new CollectionMarshaller<>(LinkedList::new)), LIST12(new UnmodifiableCollectionMarshaller<>(List.of(Boolean.TRUE).getClass().asSubclass(List.class), List::of)), LISTN(new UnmodifiableCollectionMarshaller<>(List.of().getClass().asSubclass(List.class), List::of)), LOCALE(new FunctionalScalarMarshaller<>(Scalar.STRING.cast(String.class), Functions.constantSupplier(Locale.getDefault()), Locale::toLanguageTag, Locale::forLanguageTag)), MAP1(new UnmodifiableMapMarshaller<>(Map.of(Boolean.TRUE, Boolean.FALSE).getClass().asSubclass(Map.class), Map::ofEntries)), MAPN(new UnmodifiableMapMarshaller<>(Map.of().getClass().asSubclass(Map.class), Map::ofEntries)), OPTIONAL(OptionalMarshaller.OBJECT), OPTIONAL_DOUBLE(OptionalMarshaller.DOUBLE), OPTIONAL_INT(OptionalMarshaller.INT), OPTIONAL_LONG(OptionalMarshaller.LONG), SET12(new UnmodifiableCollectionMarshaller<>(Set.of(Boolean.TRUE).getClass().asSubclass(Set.class), Set::of)), SETN(new UnmodifiableCollectionMarshaller<>(Set.of().getClass().asSubclass(Set.class), Set::of)), SIMPLE_ENTRY(new MapEntryMarshaller<>(AbstractMap.SimpleEntry::new)), SIMPLE_IMMUTABLE_ENTRY(new MapEntryMarshaller<>(AbstractMap.SimpleImmutableEntry::new)), SINGLETON_LIST(new SingletonCollectionMarshaller<>(Collections::singletonList)), SINGLETON_MAP(new SingletonMapMarshaller(Collections::singletonMap)), SINGLETON_SET(new SingletonCollectionMarshaller<>(Collections::singleton)), SYNCHRONIZED_COLLECTION(new SynchronizedDecoratorMarshaller<>(Collection.class, Collections::synchronizedCollection, Collections.emptyList())), SYNCHRONIZED_LIST(new SynchronizedDecoratorMarshaller<>(List.class, Collections::synchronizedList, new LinkedList<>())), SYNCHRONIZED_MAP(new SynchronizedDecoratorMarshaller<>(Map.class, Collections::synchronizedMap, Collections.emptyMap())), SYNCHRONIZED_NAVIGABLE_MAP(new SynchronizedDecoratorMarshaller<>(NavigableMap.class, Collections::synchronizedNavigableMap, Collections.emptyNavigableMap())), SYNCHRONIZED_NAVIGABLE_SET(new SynchronizedDecoratorMarshaller<>(NavigableSet.class, Collections::synchronizedNavigableSet, Collections.emptyNavigableSet())), SYNCHRONIZED_RANDOM_ACCESS_LIST(new SynchronizedDecoratorMarshaller<>(List.class, Collections::synchronizedList, Collections.emptyList())), SYNCHRONIZED_SET(new SynchronizedDecoratorMarshaller<>(Set.class, Collections::synchronizedSet, Collections.emptySet())), SYNCHRONIZED_SORTED_MAP(new SynchronizedDecoratorMarshaller<>(SortedMap.class, Collections::synchronizedSortedMap, Collections.emptySortedMap())), SYNCHRONIZED_SORTED_SET(new SynchronizedDecoratorMarshaller<>(SortedSet.class, Collections::synchronizedSortedSet, Collections.emptySortedSet())), TIME_ZONE(new FunctionalScalarMarshaller<>(TimeZone.class, Scalar.STRING.cast(String.class), Functions.constantSupplier(TimeZone.getDefault()), TimeZone::getID, TimeZone::getTimeZone)), TREE_MAP(new SortedMapMarshaller<>(TreeMap::new)), TREE_SET(new SortedSetMarshaller<>(TreeSet::new)), UNMODIFIABLE_COLLECTION(new DecoratorMarshaller<>(Collection.class, Collections::unmodifiableCollection, Collections.emptyList())), UNMODIFIABLE_LIST(new DecoratorMarshaller<>(List.class, Collections::unmodifiableList, new LinkedList<>())), UNMODIFIABLE_MAP(new DecoratorMarshaller<>(Map.class, Collections::unmodifiableMap, Collections.emptyMap())), UNMODIFIABLE_NAVIGABLE_MAP(new DecoratorMarshaller<>(NavigableMap.class, Collections::unmodifiableNavigableMap, Collections.emptyNavigableMap())), UNMODIFIABLE_NAVIGABLE_SET(new DecoratorMarshaller<>(NavigableSet.class, Collections::unmodifiableNavigableSet, Collections.emptyNavigableSet())), UNMODIFIABLE_RANDOM_ACCESS_LIST(new DecoratorMarshaller<>(List.class, Collections::unmodifiableList, Collections.emptyList())), UNMODIFIABLE_SET(new DecoratorMarshaller<>(Set.class, Collections::unmodifiableSet, Collections.emptySet())), UNMODIFIABLE_SORTED_MAP(new DecoratorMarshaller<>(SortedMap.class, Collections::unmodifiableSortedMap, Collections.emptySortedMap())), UNMODIFIABLE_SORTED_SET(new DecoratorMarshaller<>(SortedSet.class, Collections::unmodifiableSortedSet, Collections.emptySortedSet())), UUID(new ProtoStreamBuilderFieldSetMarshaller<>(UUIDMarshaller.INSTANCE)), ; private final ProtoStreamMarshaller<?> marshaller; UtilMarshallerProvider(ProtoStreamMarshaller<?> marshaller) { this.marshaller = marshaller; } @Override public ProtoStreamMarshaller<?> getMarshaller() { return this.marshaller; } private static Currency getDefaultCurrency() { try { return Currency.getInstance(Locale.getDefault()); } catch (IllegalArgumentException e) { return null; } } }
8,926
60.993056
197
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/concurrent/CopyOnWriteCollectionMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util.concurrent; import java.io.IOException; import java.util.Collection; import java.util.LinkedList; import java.util.function.Supplier; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.SimpleFunctionalMarshaller; import org.wildfly.clustering.marshalling.protostream.util.CollectionMarshaller; import org.wildfly.common.function.ExceptionFunction; /** * Marshaller for copy-on-write implementations of {@link Collection}. * @author Paul Ferraro * @param <T> the collection type of this marshaller */ public class CopyOnWriteCollectionMarshaller<T extends Collection<Object>> extends SimpleFunctionalMarshaller<T, Collection<Object>> { private static final ProtoStreamMarshaller<Collection<Object>> MARSHALLER = new CollectionMarshaller<>(LinkedList::new); @SuppressWarnings("unchecked") public CopyOnWriteCollectionMarshaller(Supplier<T> factory) { super((Class<T>) factory.get().getClass(), MARSHALLER, new ExceptionFunction<Collection<Object>, T, IOException>() { @Override public T apply(Collection<Object> collection) { T result = factory.get(); result.addAll(collection); return result; } }); } }
2,390
42.472727
134
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/concurrent/ConcurrentSortedMapMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util.concurrent; import java.io.IOException; import java.util.Comparator; import java.util.SortedMap; import java.util.TreeMap; import java.util.concurrent.ConcurrentMap; import java.util.function.Function; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.SimpleFunctionalMarshaller; import org.wildfly.clustering.marshalling.protostream.util.SortedMapMarshaller; import org.wildfly.common.function.ExceptionFunction; /** * Marshaller for a concurrent {@link SortedMap} that does not allow null values. * @author Paul Ferraro * @param <T> the map type of this marshaller */ public class ConcurrentSortedMapMarshaller<T extends ConcurrentMap<Object, Object> & SortedMap<Object, Object>> extends SimpleFunctionalMarshaller<T, SortedMap<Object, Object>> { private static final ProtoStreamMarshaller<SortedMap<Object, Object>> MARSHALLER = new SortedMapMarshaller<>(TreeMap::new); @SuppressWarnings("unchecked") public ConcurrentSortedMapMarshaller(Function<Comparator<? super Object>, T> factory) { super((Class<T>) factory.apply((Comparator<Object>) (Comparator<?>) Comparator.naturalOrder()).getClass(), MARSHALLER, new ExceptionFunction<SortedMap<Object, Object>, T, IOException>() { @Override public T apply(SortedMap<Object, Object> map) { T result = factory.apply(map.comparator()); result.putAll(map); return result; } }); } }
2,616
44.912281
195
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/concurrent/ConcurrentMapMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util.concurrent; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.concurrent.ConcurrentMap; import java.util.function.Supplier; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.SimpleFunctionalMarshaller; import org.wildfly.clustering.marshalling.protostream.util.MapMarshaller; import org.wildfly.common.function.ExceptionFunction; /** * Marshaller for a {@link ConcurrentMap} that does not allow null values. * @author Paul Ferraro * @param <T> the map type of this marshaller */ public class ConcurrentMapMarshaller<T extends ConcurrentMap<Object, Object>> extends SimpleFunctionalMarshaller<T, Map<Object, Object>> { private static final ProtoStreamMarshaller<Map<Object, Object>> MARSHALLER = new MapMarshaller<>(HashMap::new); @SuppressWarnings("unchecked") public ConcurrentMapMarshaller(Supplier<T> factory) { super((Class<T>) factory.get().getClass(), MARSHALLER, new ExceptionFunction<Map<Object, Object>, T, IOException>() { @Override public T apply(Map<Object, Object> map) { T result = factory.get(); result.putAll(map); return result; } }); } }
2,388
41.660714
138
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/concurrent/ConcurrentMarshallerProvider.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util.concurrent; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.ConcurrentSkipListSet; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.TimeUnit; import org.wildfly.clustering.marshalling.protostream.EnumMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshallerProvider; import org.wildfly.clustering.marshalling.protostream.util.CollectionMarshaller; import org.wildfly.clustering.marshalling.protostream.util.SortedSetMarshaller; /** * @author Paul Ferraro */ public enum ConcurrentMarshallerProvider implements ProtoStreamMarshallerProvider { CONCURRENT_HASH_MAP(new ConcurrentMapMarshaller<>(ConcurrentHashMap::new)), CONCURRENT_HASH_SET(new CollectionMarshaller<>(ConcurrentHashMap::newKeySet)), CONCURRENT_LINKED_DEQUE(new CollectionMarshaller<>(ConcurrentLinkedDeque::new)), CONCURRENT_LINKED_QUEUE(new CollectionMarshaller<>(ConcurrentLinkedQueue::new)), CONCURRENT_SKIP_LIST_MAP(new ConcurrentSortedMapMarshaller<>(ConcurrentSkipListMap::new)), CONCURRENT_SKIP_LIST_SET(new SortedSetMarshaller<>(ConcurrentSkipListSet::new)), COPY_ON_WRITE_ARRAY_LIST(new CopyOnWriteCollectionMarshaller<>(CopyOnWriteArrayList::new)), COPY_ON_WRITE_ARRAY_SET(new CopyOnWriteCollectionMarshaller<>(CopyOnWriteArraySet::new)), TIME_UNIT(new EnumMarshaller<>(TimeUnit.class)), ; private final ProtoStreamMarshaller<?> marshaller; ConcurrentMarshallerProvider(ProtoStreamMarshaller<?> marshaller) { this.marshaller = marshaller; } @Override public ProtoStreamMarshaller<?> getMarshaller() { return this.marshaller; } }
3,039
45.769231
95
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/util/concurrent/atomic/AtomicMarshallerProvider.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.util.concurrent.atomic; import java.io.IOException; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Supplier; import org.wildfly.clustering.marshalling.protostream.FunctionalScalarMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshallerProvider; import org.wildfly.clustering.marshalling.protostream.Scalar; import org.wildfly.clustering.marshalling.protostream.ScalarMarshaller; import org.wildfly.common.function.ExceptionFunction; /** * ProtoStream optimized marshallers for java.util.concurrent.atomic types. * @author Paul Ferraro */ public enum AtomicMarshallerProvider implements ProtoStreamMarshallerProvider { BOOLEAN(Scalar.BOOLEAN.cast(Boolean.class), AtomicBoolean::new, AtomicBoolean::get, AtomicBoolean::new), INTEGER(Scalar.INTEGER.cast(Integer.class), AtomicInteger::new, AtomicInteger::get, AtomicInteger::new), LONG(Scalar.LONG.cast(Long.class), AtomicLong::new, AtomicLong::get, AtomicLong::new), REFERENCE(Scalar.ANY, AtomicReference::new, AtomicReference::get, AtomicReference::new), ; private final ProtoStreamMarshaller<?> marshaller; <T, V> AtomicMarshallerProvider(ScalarMarshaller<V> marshaller, Supplier<T> defaultFactory, ExceptionFunction<T, V, IOException> function, ExceptionFunction<V, T, IOException> factory) { this.marshaller = new FunctionalScalarMarshaller<>(marshaller, defaultFactory, function, factory); } @Override public ProtoStreamMarshaller<?> getMarshaller() { return this.marshaller; } }
2,867
46.016393
190
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/time/OffsetTimeMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.time; import java.io.IOException; import java.time.LocalTime; import java.time.OffsetTime; import java.time.ZoneOffset; import org.infinispan.protostream.descriptors.WireType; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Marshaller for {@link OffsetTime} instances, using the following strategy: * <ol> * <li>Marshal local time</li> * <li>Marshal zone offset</li> * </ol> * @author Paul Ferraro */ public class OffsetTimeMarshaller implements ProtoStreamMarshaller<OffsetTime> { private static final int TIME_INDEX = 1; private static final int OFFSET_INDEX = TIME_INDEX + LocalTimeMarshaller.INSTANCE.getFields(); @Override public OffsetTime readFrom(ProtoStreamReader reader) throws IOException { LocalTime time = LocalTimeMarshaller.INSTANCE.getBuilder(); ZoneOffset offset = ZoneOffsetMarshaller.INSTANCE.getBuilder(); while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); if (index >= TIME_INDEX && index < OFFSET_INDEX) { time = LocalTimeMarshaller.INSTANCE.readField(reader, index - TIME_INDEX, time); } else if (index >= OFFSET_INDEX && index < OFFSET_INDEX + ZoneOffsetMarshaller.INSTANCE.getFields()) { offset = ZoneOffsetMarshaller.INSTANCE.readField(reader, index - OFFSET_INDEX, offset); } else { reader.skipField(tag); } } return OffsetTime.of(time, offset); } @Override public void writeTo(ProtoStreamWriter writer, OffsetTime value) throws IOException { LocalTimeMarshaller.INSTANCE.writeFields(writer, TIME_INDEX, value.toLocalTime()); ZoneOffsetMarshaller.INSTANCE.writeFields(writer, OFFSET_INDEX, value.getOffset()); } @Override public Class<? extends OffsetTime> getJavaClass() { return OffsetTime.class; } }
3,182
40.337662
115
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/time/OffsetDateTimeMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.time; import java.io.IOException; import java.time.LocalDate; import java.time.LocalTime; import java.time.OffsetDateTime; import java.time.ZoneOffset; import org.infinispan.protostream.descriptors.WireType; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Marshaller for {@link OffsetDateTime} instances, using the following strategy: * <ol> * <li>Marshal local date</li> * <li>Marshal local time</li> * <li>Marshal zone offset</li> * </ol> * @author Paul Ferraro */ public class OffsetDateTimeMarshaller implements ProtoStreamMarshaller<OffsetDateTime> { private static final int DATE_INDEX = 1; private static final int TIME_INDEX = DATE_INDEX + LocalDateMarshaller.INSTANCE.getFields(); private static final int OFFSET_INDEX = TIME_INDEX + LocalTimeMarshaller.INSTANCE.getFields(); @Override public OffsetDateTime readFrom(ProtoStreamReader reader) throws IOException { LocalDate date = LocalDateMarshaller.INSTANCE.getBuilder(); LocalTime time = LocalTimeMarshaller.INSTANCE.getBuilder(); ZoneOffset offset = ZoneOffsetMarshaller.INSTANCE.getBuilder(); while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); if (index >= DATE_INDEX && index < TIME_INDEX) { date = LocalDateMarshaller.INSTANCE.readField(reader, index - DATE_INDEX, date); } else if (index >= TIME_INDEX && index < OFFSET_INDEX) { time = LocalTimeMarshaller.INSTANCE.readField(reader, index - TIME_INDEX, time); } else if (index >= OFFSET_INDEX && index < OFFSET_INDEX + ZoneOffsetMarshaller.INSTANCE.getFields()) { offset = ZoneOffsetMarshaller.INSTANCE.readField(reader, index - OFFSET_INDEX, offset); } else { reader.skipField(tag); } } return OffsetDateTime.of(date, time, offset); } @Override public void writeTo(ProtoStreamWriter writer, OffsetDateTime value) throws IOException { LocalDateMarshaller.INSTANCE.writeFields(writer, DATE_INDEX, value.toLocalDate()); LocalTimeMarshaller.INSTANCE.writeFields(writer, TIME_INDEX, value.toLocalTime()); ZoneOffsetMarshaller.INSTANCE.writeFields(writer, OFFSET_INDEX, value.getOffset()); } @Override public Class<? extends OffsetDateTime> getJavaClass() { return OffsetDateTime.class; } }
3,704
43.107143
115
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/time/LocalTimeMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.time; import java.io.IOException; import java.time.LocalTime; import java.time.temporal.ChronoField; import org.wildfly.clustering.marshalling.protostream.FieldSetMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Marshaller for {@link LocalTime} instances, using the following strategy: * <ol> * <li>Marshal {@link LocalTime#MIDNIGHT} as zero bytes</li> * <li>Marshal number of seconds in day as unsigned integer, using hours or minutes precision, if possible</li> * <li>Marshal sub-second value of day as unsigned integer, using millisecond precision if possible</li> * </ol> * @author Paul Ferraro */ public enum LocalTimeMarshaller implements FieldSetMarshaller<LocalTime, LocalTime> { INSTANCE; private static final int HOURS_OF_DAY_INDEX = 0; private static final int MINUTES_OF_DAY_INDEX = 1; private static final int SECONDS_OF_DAY_INDEX = 2; private static final int MILLIS_INDEX = 3; private static final int NANOS_INDEX = 4; private static final int FIELDS = 5; @Override public LocalTime getBuilder() { return LocalTime.MIDNIGHT; } @Override public int getFields() { return FIELDS; } @Override public LocalTime readField(ProtoStreamReader reader, int index, LocalTime time) throws IOException { switch (index) { case HOURS_OF_DAY_INDEX: return time.with(ChronoField.HOUR_OF_DAY, reader.readUInt32()); case MINUTES_OF_DAY_INDEX: return time.with(ChronoField.MINUTE_OF_DAY, reader.readUInt32()); case SECONDS_OF_DAY_INDEX: return time.with(ChronoField.SECOND_OF_DAY, reader.readUInt32()); case MILLIS_INDEX: return time.with(ChronoField.MILLI_OF_SECOND, reader.readUInt32()); case NANOS_INDEX: return time.withNano(reader.readUInt32()); default: return time; } } @Override public void writeFields(ProtoStreamWriter writer, int startIndex, LocalTime time) throws IOException { int secondOfDay = time.toSecondOfDay(); if (secondOfDay > 0) { if (secondOfDay % 60 == 0) { int minutesOfDay = secondOfDay / 60; if (minutesOfDay % 60 == 0) { int hoursOfDay = minutesOfDay / 60; writer.writeUInt32(startIndex + HOURS_OF_DAY_INDEX, hoursOfDay); } else { writer.writeUInt32(startIndex + MINUTES_OF_DAY_INDEX, minutesOfDay); } } else { writer.writeUInt32(startIndex + SECONDS_OF_DAY_INDEX, secondOfDay); } } int nanos = time.getNano(); if (nanos > 0) { // Use ms precision, if possible if (nanos % 1_000_000 == 0) { writer.writeUInt32(startIndex + MILLIS_INDEX, time.get(ChronoField.MILLI_OF_SECOND)); } else { writer.writeUInt32(startIndex + NANOS_INDEX, nanos); } } } }
4,259
38.813084
111
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/time/ZonedDateTimeMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.time; import java.io.IOException; import java.time.LocalDate; import java.time.LocalTime; import java.time.ZoneId; import java.time.ZoneOffset; import java.time.ZonedDateTime; import org.infinispan.protostream.descriptors.WireType; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Marshaller for {@link ZonedDateTime} instances, using the following strategy: * <ol> * <li>Marshal duration since epoch</li> * <li>Marshal time zone</li> * </ol> * @author Paul Ferraro */ public class ZonedDateTimeMarshaller implements ProtoStreamMarshaller<ZonedDateTime> { private static final int DATE_INDEX = 1; private static final int TIME_INDEX = DATE_INDEX + LocalDateMarshaller.INSTANCE.getFields(); private static final int OFFSET_INDEX = TIME_INDEX + LocalTimeMarshaller.INSTANCE.getFields(); private static final int ZONE_INDEX = OFFSET_INDEX + ZoneOffsetMarshaller.INSTANCE.getFields(); @Override public ZonedDateTime readFrom(ProtoStreamReader reader) throws IOException { LocalDate date = LocalDateMarshaller.INSTANCE.getBuilder(); LocalTime time = LocalTimeMarshaller.INSTANCE.getBuilder(); ZoneId zone = ZoneOffsetMarshaller.INSTANCE.getBuilder(); while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); if (index >= DATE_INDEX && index < TIME_INDEX) { date = LocalDateMarshaller.INSTANCE.readField(reader, index - DATE_INDEX, date); } else if (index >= TIME_INDEX && index < OFFSET_INDEX) { time = LocalTimeMarshaller.INSTANCE.readField(reader, index - TIME_INDEX, time); } else if (index >= OFFSET_INDEX && index < ZONE_INDEX) { zone = ZoneOffsetMarshaller.INSTANCE.readField(reader, index - OFFSET_INDEX, (ZoneOffset) zone); } else if (index == ZONE_INDEX) { zone = ZoneId.of(reader.readString()); } else { reader.skipField(tag); } } return ZonedDateTime.of(date, time, zone); } @Override public void writeTo(ProtoStreamWriter writer, ZonedDateTime value) throws IOException { LocalDateMarshaller.INSTANCE.writeFields(writer, DATE_INDEX, value.toLocalDate()); LocalTimeMarshaller.INSTANCE.writeFields(writer, TIME_INDEX, value.toLocalTime()); ZoneId zone = value.getZone(); if (zone instanceof ZoneOffset) { ZoneOffsetMarshaller.INSTANCE.writeFields(writer, OFFSET_INDEX, (ZoneOffset) zone); } else { writer.writeString(ZONE_INDEX, zone.getId()); } } @Override public Class<? extends ZonedDateTime> getJavaClass() { return ZonedDateTime.class; } }
4,023
42.73913
112
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/time/YearMonthMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.time; import java.io.IOException; import java.time.Month; import java.time.Year; import java.time.YearMonth; import org.infinispan.protostream.descriptors.WireType; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Marshaller for {@link YearMonth} instances, using the following strategy: * <ol> * <li>Marshal epoch year</li> * <li>Marshal month as enum</li> * </ol> * @author Paul Ferraro */ public class YearMonthMarshaller implements ProtoStreamMarshaller<YearMonth> { private static final Month[] MONTHS = Month.values(); private static final YearMonth DEFAULT = YearMonth.of(YearMarshaller.INSTANCE.getBuilder().getValue(), Month.JANUARY); private static final int YEAR_INDEX = 1; private static final int MONTH_INDEX = YEAR_INDEX + YearMarshaller.INSTANCE.getFields(); @Override public YearMonth readFrom(ProtoStreamReader reader) throws IOException { YearMonth result = DEFAULT; while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); if (index >= YEAR_INDEX && index < MONTH_INDEX) { result = result.withYear(YearMarshaller.INSTANCE.readField(reader, index - YEAR_INDEX, Year.of(result.getYear())).getValue()); } else if (index == MONTH_INDEX) { result = result.withMonth(MONTHS[reader.readEnum()].getValue()); } else { reader.skipField(tag); } } return result; } @Override public void writeTo(ProtoStreamWriter writer, YearMonth value) throws IOException { int year = value.getYear(); if (year != DEFAULT.getYear()) { YearMarshaller.INSTANCE.writeFields(writer, YEAR_INDEX, Year.of(year)); } Month month = value.getMonth(); if (month != DEFAULT.getMonth()) { writer.writeEnum(MONTH_INDEX, month.ordinal()); } } @Override public Class<? extends YearMonth> getJavaClass() { return YearMonth.class; } }
3,311
37.964706
142
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/time/LocalDateMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.time; import java.io.IOException; import java.time.LocalDate; import org.wildfly.clustering.marshalling.protostream.FieldSetMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Marshals a {@link LocalDate} as an epoch day. * @author Paul Ferraro */ public enum LocalDateMarshaller implements FieldSetMarshaller<LocalDate, LocalDate> { INSTANCE; private static final int POST_EPOCH_DAY = 0; private static final int PRE_EPOCH_DAY = 1; private static final int FIELDS = 2; private static final LocalDate EPOCH = LocalDate.ofEpochDay(0); @Override public LocalDate getBuilder() { return EPOCH; } @Override public int getFields() { return FIELDS; } @Override public LocalDate readField(ProtoStreamReader reader, int index, LocalDate date) throws IOException { switch (index) { case POST_EPOCH_DAY: return LocalDate.ofEpochDay(reader.readUInt64()); case PRE_EPOCH_DAY: return LocalDate.ofEpochDay(0L - reader.readUInt64()); default: return date; } } @Override public void writeFields(ProtoStreamWriter writer, int startIndex, LocalDate date) throws IOException { long epochDay = date.toEpochDay(); if (epochDay > 0) { writer.writeUInt64(startIndex + POST_EPOCH_DAY, epochDay); } else if (epochDay < 0) { writer.writeUInt64(startIndex + PRE_EPOCH_DAY, 0L - epochDay); } } }
2,710
34.207792
106
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/time/ZoneOffsetMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.time; import java.io.IOException; import java.time.ZoneOffset; import org.wildfly.clustering.marshalling.protostream.FieldSetMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Marshalling for {@link ZoneOffset} instances using the following strategy: * <ol> * <li>Marshal {@link ZoneOffset#UTC} as zero bytes</li> * <li>If offset is of form &plusmn;HH, marshal as signed integer of hours</li> * <li>If offset is of form &plusmn;HH:MM, marshal as signed integer of total minutes<li> * <li>If offset is of form &plusmn;HH:MM:SS, marshal as signed integer of total seconds<li> * </ol> * @author Paul Ferraro */ public enum ZoneOffsetMarshaller implements FieldSetMarshaller<ZoneOffset, ZoneOffset> { INSTANCE; private static final int HOURS_INDEX = 0; private static final int MINUTES_INDEX = 1; private static final int SECONDS_INDEX = 2; private static final int FIELDS = 3; @Override public ZoneOffset getBuilder() { return ZoneOffset.UTC; } @Override public int getFields() { return FIELDS; } @Override public ZoneOffset readField(ProtoStreamReader reader, int index, ZoneOffset offset) throws IOException { switch (index) { case HOURS_INDEX: return ZoneOffset.ofHours(reader.readSInt32()); case MINUTES_INDEX: return ZoneOffset.ofTotalSeconds(reader.readSInt32() * 60); case SECONDS_INDEX: return ZoneOffset.ofTotalSeconds(reader.readSInt32()); default: return offset; } } @Override public void writeFields(ProtoStreamWriter writer, int startIndex, ZoneOffset offset) throws IOException { int seconds = offset.getTotalSeconds(); if (seconds != 0) { if (seconds % 60 == 0) { int minutes = seconds / 60; if (minutes % 60 == 0) { int hours = minutes / 60; // Typical offsets writer.writeSInt32(startIndex + HOURS_INDEX, hours); } else { // Uncommon fractional hour offsets writer.writeSInt32(startIndex + MINUTES_INDEX, minutes); } } else { // Synthetic offsets writer.writeSInt32(startIndex + SECONDS_INDEX, seconds); } } } }
3,603
36.936842
109
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/time/YearMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.time; import java.io.IOException; import java.time.LocalDate; import java.time.Year; import org.wildfly.clustering.marshalling.protostream.FieldSetMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Marshals {@link Year} instances as number of years since the epoch year. * @author Paul Ferraro */ public enum YearMarshaller implements FieldSetMarshaller<Year, Year> { INSTANCE; private static final int POST_EPOCH_YEAR = 0; private static final int PRE_EPOCH_YEAR = 1; private static final int FIELDS = 2; private static final Year EPOCH = Year.of(LocalDate.ofEpochDay(0).getYear()); @Override public Year getBuilder() { return EPOCH; } @Override public int getFields() { return FIELDS; } @Override public Year readField(ProtoStreamReader reader, int index, Year year) throws IOException { switch (index) { case POST_EPOCH_YEAR: return Year.of(EPOCH.getValue() + reader.readUInt32()); case PRE_EPOCH_YEAR: return Year.of(EPOCH.getValue() - reader.readUInt32()); default: return year; } } @Override public void writeFields(ProtoStreamWriter writer, int startIndex, Year value) throws IOException { int year = value.getValue(); if (year > EPOCH.getValue()) { writer.writeUInt32(startIndex + POST_EPOCH_YEAR, year - EPOCH.getValue()); } else if (year < EPOCH.getValue()) { writer.writeUInt32(startIndex + PRE_EPOCH_YEAR, EPOCH.getValue() - year); } } }
2,794
34.833333
102
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/time/TimeMarshallerProvider.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.time; import java.time.DayOfWeek; import java.time.Month; import java.time.ZoneId; import java.time.ZoneOffset; import org.wildfly.clustering.marshalling.protostream.EnumMarshaller; import org.wildfly.clustering.marshalling.protostream.FunctionalScalarMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshallerProvider; import org.wildfly.clustering.marshalling.protostream.Scalar; import org.wildfly.clustering.marshalling.protostream.SimpleFieldSetMarshaller; import org.wildfly.common.function.Functions; /** * Provider for java.time marshallers. * @author Paul Ferraro */ public enum TimeMarshallerProvider implements ProtoStreamMarshallerProvider { DAY_OF_WEEK(new EnumMarshaller<>(DayOfWeek.class)), DURATION(new SimpleFieldSetMarshaller<>(DurationMarshaller.INSTANCE)), INSTANT(new InstantMarshaller()), LOCAL_DATE(new SimpleFieldSetMarshaller<>(LocalDateMarshaller.INSTANCE)), LOCAL_DATE_TIME(new LocalDateTimeMarshaller()), LOCAL_TIME(new SimpleFieldSetMarshaller<>(LocalTimeMarshaller.INSTANCE)), MONTH(new EnumMarshaller<>(Month.class)), MONTH_DAY(new MonthDayMarshaller()), OFFSET_DATE_TIME(new OffsetDateTimeMarshaller()), OFFSET_TIME(new OffsetTimeMarshaller()), PERIOD(new PeriodMarshaller()), YEAR(new SimpleFieldSetMarshaller<>(YearMarshaller.INSTANCE)), YEAR_MONTH(new YearMonthMarshaller()), ZONE_ID(new FunctionalScalarMarshaller<>(ZoneId.class, Scalar.STRING.cast(String.class), Functions.constantSupplier(ZoneOffset.UTC), ZoneId::getId, ZoneId::of)), ZONE_OFFSET(new SimpleFieldSetMarshaller<>(ZoneOffsetMarshaller.INSTANCE)), ZONED_DATE_TIME(new ZonedDateTimeMarshaller()), ; private final ProtoStreamMarshaller<?> marshaller; TimeMarshallerProvider(ProtoStreamMarshaller<?> marshaller) { this.marshaller = marshaller; } @Override public ProtoStreamMarshaller<?> getMarshaller() { return this.marshaller; } }
3,134
42.541667
165
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/time/DurationMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.time; import java.io.IOException; import java.time.Duration; import org.wildfly.clustering.marshalling.protostream.FieldSetMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Marshaller for {@link Duration} instances, using the following strategy: * <ol> * <li>Marshal {@link Duration#ZERO} as zero bytes</li> * <li>Marshal number of seconds of duration as unsigned long</li> * <li>Marshal sub-second value of duration as unsigned integer, using millisecond precision, if possible</li> * </ol> * @author Paul Ferraro */ public enum DurationMarshaller implements FieldSetMarshaller<Duration, Duration> { INSTANCE; private static final int POSITIVE_SECONDS_INDEX = 0; private static final int NEGATIVE_SECONDS_INDEX = 1; private static final int MILLIS_INDEX = 2; private static final int NANOS_INDEX = 3; private static final int FIELDS = 4; @Override public Duration getBuilder() { return Duration.ZERO; } @Override public int getFields() { return FIELDS; } @Override public Duration readField(ProtoStreamReader reader, int index, Duration duration) throws IOException { switch (index) { case POSITIVE_SECONDS_INDEX: return duration.withSeconds(reader.readUInt64()); case NEGATIVE_SECONDS_INDEX: return duration.withSeconds(0 - reader.readUInt64()); case MILLIS_INDEX: return duration.withNanos(reader.readUInt32() * 1_000_000); case NANOS_INDEX: return duration.withNanos(reader.readUInt32()); default: return duration; } } @Override public void writeFields(ProtoStreamWriter writer, int startIndex, Duration duration) throws IOException { long seconds = duration.getSeconds(); // Optimize for positive values if (seconds > 0) { writer.writeUInt64(startIndex + POSITIVE_SECONDS_INDEX, seconds); } else if (seconds < 0) { writer.writeUInt64(startIndex + NEGATIVE_SECONDS_INDEX, 0 - seconds); } int nanos = duration.getNano(); if (nanos > 0) { // Optimize for ms precision, if possible if (nanos % 1_000_000 == 0) { writer.writeUInt32(startIndex + MILLIS_INDEX, nanos / 1_000_000); } else { writer.writeUInt32(startIndex + NANOS_INDEX, nanos); } } } }
3,670
37.239583
110
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/time/MonthDayMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.time; import java.io.IOException; import java.time.Month; import java.time.MonthDay; import org.infinispan.protostream.descriptors.WireType; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Marshals {@link MonthDay} instances. * @author Paul Ferraro */ public class MonthDayMarshaller implements ProtoStreamMarshaller<MonthDay> { private static final Month[] MONTHS = Month.values(); private static final MonthDay DEFAULT = MonthDay.of(Month.JANUARY, 1); private static final int MONTH_INDEX = 1; private static final int DAY_OF_MONTH_INDEX = 2; @Override public MonthDay readFrom(ProtoStreamReader reader) throws IOException { MonthDay result = DEFAULT; while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); switch (index) { case MONTH_INDEX: result = result.with(MONTHS[reader.readEnum()]); break; case DAY_OF_MONTH_INDEX: result = result.withDayOfMonth(reader.readUInt32() + 1); break; default: reader.skipField(tag); } } return result; } @Override public void writeTo(ProtoStreamWriter writer, MonthDay value) throws IOException { Month month = value.getMonth(); if (month != DEFAULT.getMonth()) { writer.writeEnum(MONTH_INDEX, month.ordinal()); } int dayOfMonth = value.getDayOfMonth(); if (dayOfMonth != DEFAULT.getDayOfMonth()) { writer.writeUInt32(DAY_OF_MONTH_INDEX, dayOfMonth - 1); } } @Override public Class<? extends MonthDay> getJavaClass() { return MonthDay.class; } }
3,061
35.891566
86
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/time/LocalDateTimeMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.time; import java.io.IOException; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import org.infinispan.protostream.descriptors.WireType; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Marshaller for {@link LocalDateTime} instances, using the following strategy: * <ol> * <li>Marshal local date</li> * <li>Marshal local time</li> * </ol> * @author Paul Ferraro */ public class LocalDateTimeMarshaller implements ProtoStreamMarshaller<LocalDateTime> { private static final int DATE_INDEX = 1; private static final int TIME_INDEX = DATE_INDEX + LocalDateMarshaller.INSTANCE.getFields(); @Override public LocalDateTime readFrom(ProtoStreamReader reader) throws IOException { LocalDate date = LocalDateMarshaller.INSTANCE.getBuilder(); LocalTime time = LocalTimeMarshaller.INSTANCE.getBuilder(); while (!reader.isAtEnd()) { int tag = reader.readTag(); int index = WireType.getTagFieldNumber(tag); if (index >= DATE_INDEX && index < TIME_INDEX) { date = LocalDateMarshaller.INSTANCE.readField(reader, index - DATE_INDEX, date); } else if (index >= TIME_INDEX && index < TIME_INDEX + LocalTimeMarshaller.INSTANCE.getFields()) { time = LocalTimeMarshaller.INSTANCE.readField(reader, index - TIME_INDEX, time); } else { reader.skipField(tag); } } return LocalDateTime.of(date, time); } @Override public void writeTo(ProtoStreamWriter writer, LocalDateTime value) throws IOException { LocalDateMarshaller.INSTANCE.writeFields(writer, DATE_INDEX, value.toLocalDate()); LocalTimeMarshaller.INSTANCE.writeFields(writer, TIME_INDEX, value.toLocalTime()); } @Override public Class<? extends LocalDateTime> getJavaClass() { return LocalDateTime.class; } }
3,184
40.363636
110
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/time/PeriodMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2020, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.time; import java.io.IOException; import java.time.Period; import org.infinispan.protostream.descriptors.WireType; import org.wildfly.clustering.marshalling.protostream.ProtoStreamMarshaller; import org.wildfly.clustering.marshalling.protostream.ProtoStreamReader; import org.wildfly.clustering.marshalling.protostream.ProtoStreamWriter; /** * Marshaller for {@link Period} instances, using the following strategy: * <ol> * <li>Marshal {@link Period#ZERO} as zero bytes</li> * <li>Marshal number of years of period as signed integer</li> * <li>Marshal number of months of period as signed integer</li> * <li>Marshal number of days of period as signed integer</li> * </ol> * @author Paul Ferraro */ public class PeriodMarshaller implements ProtoStreamMarshaller<Period> { private static final int YEARS_INDEX = 1; private static final int MONTHS_INDEX = 2; private static final int DAYS_INDEX = 3; @Override public Period readFrom(ProtoStreamReader reader) throws IOException { Period period = Period.ZERO; while (!reader.isAtEnd()) { int tag = reader.readTag(); switch (WireType.getTagFieldNumber(tag)) { case YEARS_INDEX: period = period.withYears(reader.readSInt32()); break; case MONTHS_INDEX: period = period.withMonths(reader.readSInt32()); break; case DAYS_INDEX: period = period.withDays(reader.readSInt32()); break; default: reader.skipField(tag); } } return period; } @Override public void writeTo(ProtoStreamWriter writer, Period period) throws IOException { int years = period.getYears(); if (years != 0) { writer.writeSInt32(YEARS_INDEX, years); } int months = period.getMonths(); if (months != 0) { writer.writeSInt32(MONTHS_INDEX, months); } int days = period.getDays(); if (days != 0) { writer.writeSInt32(DAYS_INDEX, days); } } @Override public Class<? extends Period> getJavaClass() { return Period.class; } }
3,358
35.51087
85
java
null
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/time/InstantMarshaller.java
/* * JBoss, Home of Professional Open Source. * Copyright 2021, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.wildfly.clustering.marshalling.protostream.time; import java.io.IOException; import java.time.Duration; import java.time.Instant; import org.wildfly.clustering.marshalling.protostream.FunctionalMarshaller; import org.wildfly.common.function.ExceptionFunction; /** * @author Paul Ferraro */ public class InstantMarshaller extends FunctionalMarshaller<Instant, Duration> { private static final ExceptionFunction<Instant, Duration, IOException> DURATION_SINCE_EPOCH = new ExceptionFunction<>() { @Override public Duration apply(Instant instant) { return Duration.ofSeconds(instant.getEpochSecond(), instant.getNano()); } }; private static final ExceptionFunction<Duration, Instant, IOException> FACTORY = new ExceptionFunction<>() { @Override public Instant apply(Duration duration) { return Instant.ofEpochSecond(duration.getSeconds(), duration.getNano()); } }; public InstantMarshaller() { super(Instant.class, Duration.class, DURATION_SINCE_EPOCH, FACTORY); } }
2,109
38.074074
125
java