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/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/OptionalIntExternalizer.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.spi.util;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.OptionalInt;
import org.wildfly.clustering.marshalling.Externalizer;
/**
* Externalizer for an {@link OptionalInt}.
* @author Paul Ferraro
*/
public class OptionalIntExternalizer implements Externalizer<OptionalInt> {
@Override
public void writeObject(ObjectOutput output, OptionalInt value) throws IOException {
boolean present = value.isPresent();
output.writeBoolean(present);
if (present) {
output.writeInt(value.getAsInt());
}
}
@Override
public OptionalInt readObject(ObjectInput input) throws IOException, ClassNotFoundException {
return (input.readBoolean()) ? OptionalInt.of(input.readInt()) : OptionalInt.empty();
}
@Override
public OptionalInt size(OptionalInt value) {
return OptionalInt.of(value.isPresent() ? Integer.BYTES + Byte.BYTES : Byte.BYTES);
}
@Override
public Class<OptionalInt> getTargetClass() {
return OptionalInt.class;
}
}
| 2,157
| 33.806452
| 97
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/CollectionExternalizer.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.spi.util;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.Map;
import java.util.OptionalInt;
import java.util.function.Function;
import org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.clustering.marshalling.spi.IndexSerializer;
/**
* Generic externalizer for implementations of {@link Collection}.
* @author Paul Ferraro
*/
public class CollectionExternalizer<T extends Collection<Object>, C, CC> implements Externalizer<T> {
private final Class<T> targetClass;
private final Function<CC, T> factory;
private final Function<Map.Entry<C, Integer>, CC> constructorContext;
private final Function<T, C> context;
private final Externalizer<C> contextExternalizer;
public CollectionExternalizer(Class<T> targetClass, Function<CC, T> factory, Function<Map.Entry<C, Integer>, CC> constructorContext, Function<T, C> context, Externalizer<C> contextExternalizer) {
this.targetClass = targetClass;
this.factory = factory;
this.constructorContext = constructorContext;
this.context = context;
this.contextExternalizer = contextExternalizer;
}
@Override
public void writeObject(ObjectOutput output, T collection) throws IOException {
synchronized (collection) { // Avoid ConcurrentModificationException
C context = this.context.apply(collection);
this.contextExternalizer.writeObject(output, context);
IndexSerializer.VARIABLE.writeInt(output, collection.size());
for (Object element : collection) {
output.writeObject(element);
}
}
}
@Override
public T readObject(ObjectInput input) throws IOException, ClassNotFoundException {
C context = this.contextExternalizer.readObject(input);
int size = IndexSerializer.VARIABLE.readInt(input);
CC constructorContext = this.constructorContext.apply(new AbstractMap.SimpleImmutableEntry<>(context, size));
T collection = this.factory.apply(constructorContext);
for (int i = 0; i < size; ++i) {
collection.add(input.readObject());
}
return collection;
}
@Override
public OptionalInt size(T collection) {
if (!collection.isEmpty()) return OptionalInt.empty();
synchronized (collection) { // Avoid ConcurrentModificationException
C context = this.context.apply(collection);
OptionalInt contextSize = this.contextExternalizer.size(context);
return contextSize.isPresent() ? OptionalInt.of(contextSize.getAsInt() + IndexSerializer.VARIABLE.size(collection.size())) : OptionalInt.empty();
}
}
@Override
public Class<T> getTargetClass() {
return this.targetClass;
}
}
| 3,946
| 40.114583
| 199
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/ContextualCollectionExternalizer.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.spi.util;
import java.util.Collection;
import java.util.Map;
import java.util.function.Function;
import org.wildfly.clustering.marshalling.Externalizer;
/**
* Externalizer for implementations of {@link Collection} constructed with some context.
* @author Paul Ferraro
*/
public class ContextualCollectionExternalizer<T extends Collection<Object>, C> extends CollectionExternalizer<T, C, C> {
public ContextualCollectionExternalizer(Class<T> targetClass, Function<C, T> factory, Function<T, C> context, Externalizer<C> contextExternalizer) {
super(targetClass, factory, Map.Entry::getKey, context, contextExternalizer);
}
}
| 1,713
| 40.804878
| 152
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/HashMapExternalizer.java
|
/*
* JBoss, Home of Professional Open Source.
* Copyright 2019, 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.spi.util;
import java.util.Map;
import java.util.function.IntFunction;
import org.wildfly.clustering.marshalling.spi.ValueFunction;
import org.wildfly.clustering.marshalling.spi.util.HashSetExternalizer.CapacityFactory;
import org.wildfly.clustering.marshalling.spi.ValueExternalizer;
/**
* @author Paul Ferraro
*/
public class HashMapExternalizer<T extends Map<Object, Object>> extends MapExternalizer<T, Void, Integer> {
public HashMapExternalizer(Class<T> targetClass, IntFunction<T> factory) {
super(targetClass, new CapacityFactory<>(factory), Map.Entry::getValue, ValueFunction.voidFunction(), ValueExternalizer.VOID);
}
}
| 1,718
| 40.926829
| 134
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/SingletonCollectionExternalizer.java
|
/*
* JBoss, Home of Professional Open Source.
* Copyright 2016, 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.spi.util;
import java.util.Collection;
import java.util.function.Function;
import org.wildfly.clustering.marshalling.spi.ObjectExternalizer;
/**
* Externalizer for singleton collections.
* @author Paul Ferraro
*/
public class SingletonCollectionExternalizer<T extends Collection<Object>> extends ObjectExternalizer<T> {
private static final Function<Collection<Object>, Object> ACCESSOR = new Function<>() {
@Override
public Object apply(Collection<Object> collection) {
return collection.iterator().next();
}
};
@SuppressWarnings("unchecked")
public SingletonCollectionExternalizer(Function<Object, T> factory) {
super((Class<T>) factory.apply(null).getClass(), factory, accessor());
}
@SuppressWarnings("unchecked")
public static <T extends Collection<Object>> Function<T, Object> accessor() {
return (Function<T, Object>) ACCESSOR;
}
}
| 1,998
| 37.442308
| 106
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/SingletonMapExternalizer.java
|
/*
* JBoss, Home of Professional Open Source.
* Copyright 2016, 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.spi.util;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Collections;
import java.util.Map;
import org.wildfly.clustering.marshalling.Externalizer;
/**
* @author Paul Ferraro
*/
public class SingletonMapExternalizer implements Externalizer<Map<Object, Object>> {
@Override
public void writeObject(ObjectOutput output, Map<Object, Object> map) throws IOException {
Map.Entry<Object, Object> entry = map.entrySet().iterator().next();
output.writeObject(entry.getKey());
output.writeObject(entry.getValue());
}
@Override
public Map<Object, Object> readObject(ObjectInput input) throws IOException, ClassNotFoundException {
return Collections.singletonMap(input.readObject(), input.readObject());
}
@SuppressWarnings("unchecked")
@Override
public Class<Map<Object, Object>> getTargetClass() {
return (Class<Map<Object, Object>>) Collections.singletonMap(null, null).getClass();
}
}
| 2,088
| 36.303571
| 105
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/OptionalLongExternalizer.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.spi.util;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.OptionalInt;
import java.util.OptionalLong;
import org.wildfly.clustering.marshalling.Externalizer;
/**
* Externalizer for an {@link OptionalLong}.
* @author Paul Ferraro
*/
public class OptionalLongExternalizer implements Externalizer<OptionalLong> {
@Override
public void writeObject(ObjectOutput output, OptionalLong value) throws IOException {
boolean present = value.isPresent();
output.writeBoolean(present);
if (present) {
output.writeLong(value.getAsLong());
}
}
@Override
public OptionalLong readObject(ObjectInput input) throws IOException, ClassNotFoundException {
return (input.readBoolean()) ? OptionalLong.of(input.readLong()) : OptionalLong.empty();
}
@Override
public OptionalInt size(OptionalLong value) {
return OptionalInt.of(value.isPresent() ? Long.BYTES + Byte.BYTES : Byte.BYTES);
}
@Override
public Class<OptionalLong> getTargetClass() {
return OptionalLong.class;
}
}
| 2,198
| 33.904762
| 98
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/SortedSetExternalizer.java
|
/*
* JBoss, Home of Professional Open Source.
* Copyright 2015, 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.spi.util;
import java.util.Comparator;
import java.util.SortedSet;
import java.util.function.Function;
import org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.clustering.marshalling.spi.IdentityFunction;
import org.wildfly.clustering.marshalling.spi.ObjectExternalizer;
/**
* Externalizers for implementations of {@link SortedSet}.
* Requires additional serialization of the comparator.
* @author Paul Ferraro
*/
public class SortedSetExternalizer<T extends SortedSet<Object>> extends ContextualCollectionExternalizer<T, Comparator<Object>> {
@SuppressWarnings("unchecked")
private static final Externalizer<Comparator<Object>> COMPARATOR_EXTERNALIZER = (Externalizer<Comparator<Object>>) (Externalizer<?>) new ObjectExternalizer<>(Comparator.class, Comparator.class::cast, IdentityFunction.getInstance());
public SortedSetExternalizer(Class<T> targetClass, Function<Comparator<Object>, T> factory) {
super(targetClass, factory, SortedSet::comparator, COMPARATOR_EXTERNALIZER);
}
}
| 2,100
| 44.673913
| 236
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/EnumSetExternalizer.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.spi.util;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.lang.reflect.Field;
import java.security.PrivilegedAction;
import java.util.BitSet;
import java.util.EnumSet;
import java.util.Iterator;
import org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.security.manager.WildFlySecurityManager;
/**
* Externalizer for an {@link EnumSet}. Handles both regular and jumbo variants.
* @author Paul Ferraro
*/
public class EnumSetExternalizer<E extends Enum<E>> implements Externalizer<EnumSet<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();
}
});
@Override
public void writeObject(ObjectOutput output, EnumSet<E> set) throws IOException {
Class<?> enumClass = this.findEnumClass(set);
output.writeObject(enumClass);
Object[] enumValues = enumClass.getEnumConstants();
// Represent EnumSet as a BitSet
BitSet values = new BitSet(enumValues.length);
for (int i = 0; i < enumValues.length; ++i) {
values.set(i, set.contains(enumValues[i]));
}
UtilExternalizerProvider.BIT_SET.writeObject(output, values);
}
@SuppressWarnings("unchecked")
@Override
public EnumSet<E> readObject(ObjectInput input) throws IOException, ClassNotFoundException {
Class<E> enumClass = (Class<E>) input.readObject();
BitSet values = UtilExternalizerProvider.BIT_SET.cast(BitSet.class).readObject(input);
EnumSet<E> set = EnumSet.noneOf(enumClass);
Object[] enumValues = enumClass.getEnumConstants();
for (int i = 0; i < enumValues.length; ++i) {
if (values.get(i)) {
set.add((E) enumValues[i]);
}
}
return set;
}
@SuppressWarnings("unchecked")
@Override
public Class<EnumSet<E>> getTargetClass() {
return (Class<EnumSet<E>>) (Class<?>) EnumSet.class;
}
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);
}
}
});
}
}
| 4,286
| 37.972727
| 113
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/SortedMapExternalizer.java
|
/*
* JBoss, Home of Professional Open Source.
* Copyright 2015, 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.spi.util;
import java.util.Comparator;
import java.util.SortedMap;
import java.util.function.Function;
import org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.clustering.marshalling.spi.IdentityFunction;
import org.wildfly.clustering.marshalling.spi.ObjectExternalizer;
/**
* Externalizers for implementations of {@link SortedMap}.
* Requires additional serialization of the comparator.
* @author Paul Ferraro
*/
public class SortedMapExternalizer<T extends SortedMap<Object, Object>> extends ContextualMapExternalizer<T, Comparator<Object>> {
@SuppressWarnings("unchecked")
private static final Externalizer<Comparator<Object>> COMPARATOR_EXTERNALIZER = (Externalizer<Comparator<Object>>) (Externalizer<?>) new ObjectExternalizer<>(Comparator.class, Comparator.class::cast, IdentityFunction.getInstance());
public SortedMapExternalizer(Class<T> targetClass, Function<Comparator<Object>, T> factory) {
super(targetClass, factory, SortedMap::comparator, COMPARATOR_EXTERNALIZER);
}
}
| 2,101
| 44.695652
| 236
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/CalendarExternalizer.java
|
/*
* JBoss, Home of Professional Open Source.
* Copyright 2017, 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.spi.util;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.OptionalInt;
import java.util.TimeZone;
import org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.clustering.marshalling.spi.IndexSerializer;
import org.wildfly.clustering.marshalling.spi.IntSerializer;
/**
* @author Paul Ferraro
*/
public class CalendarExternalizer implements Externalizer<Calendar> {
private static final String[] CALENDAR_TYPE_NAMES;
private static final Map<String, Integer> CALENDAR_TYPE_IDS = new HashMap<>();
private static final IntSerializer CALENDAR_TYPE_SERIALIZER;
static {
CALENDAR_TYPE_NAMES = Calendar.getAvailableCalendarTypes().toArray(new String[0]);
CALENDAR_TYPE_SERIALIZER = IndexSerializer.select(CALENDAR_TYPE_NAMES.length);
Arrays.sort(CALENDAR_TYPE_NAMES);
for (int i = 0; i < CALENDAR_TYPE_NAMES.length; ++i) {
CALENDAR_TYPE_IDS.put(CALENDAR_TYPE_NAMES[i], Integer.valueOf(i));
}
}
@Override
public void writeObject(ObjectOutput output, Calendar calendar) throws IOException {
CALENDAR_TYPE_SERIALIZER.writeInt(output, CALENDAR_TYPE_IDS.get(calendar.getCalendarType()));
output.writeLong(calendar.getTimeInMillis());
output.writeBoolean(calendar.isLenient());
UtilExternalizerProvider.TIME_ZONE.cast(TimeZone.class).writeObject(output, calendar.getTimeZone());
IndexSerializer.UNSIGNED_BYTE.writeInt(output, calendar.getFirstDayOfWeek());
IndexSerializer.UNSIGNED_BYTE.writeInt(output, calendar.getMinimalDaysInFirstWeek());
}
@Override
public Calendar readObject(ObjectInput input) throws IOException, ClassNotFoundException {
return new Calendar.Builder()
.setCalendarType(CALENDAR_TYPE_NAMES[CALENDAR_TYPE_SERIALIZER.readInt(input)])
.setInstant(input.readLong())
.setLenient(input.readBoolean())
.setTimeZone(UtilExternalizerProvider.TIME_ZONE.cast(TimeZone.class).readObject(input))
.setWeekDefinition(IndexSerializer.UNSIGNED_BYTE.readInt(input), IndexSerializer.UNSIGNED_BYTE.readInt(input))
.build();
}
@Override
public OptionalInt size(Calendar calendar) {
int calendarTypeSize = CALENDAR_TYPE_SERIALIZER.size(CALENDAR_TYPE_IDS.get(calendar.getCalendarType()));
int timeZoneSize = UtilExternalizerProvider.TIME_ZONE.cast(TimeZone.class).size(calendar.getTimeZone()).getAsInt();
int firstDayOfWeekSize = IndexSerializer.UNSIGNED_BYTE.size(calendar.getFirstDayOfWeek());
int minimalDaysInFirstWeekSize = IndexSerializer.UNSIGNED_BYTE.size(calendar.getMinimalDaysInFirstWeek());
return OptionalInt.of(calendarTypeSize + Long.BYTES + Byte.BYTES + timeZoneSize + firstDayOfWeekSize + minimalDaysInFirstWeekSize);
}
@Override
public Class<Calendar> getTargetClass() {
return Calendar.class;
}
}
| 4,166
| 45.3
| 139
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/HashSetExternalizer.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.spi.util;
import java.util.Set;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.IntUnaryOperator;
/**
* Externalizer for hash table based sets constructed with a capacity rather than a size.
* @author Paul Ferraro
*/
public class HashSetExternalizer<T extends Set<Object>> extends BoundedCollectionExternalizer<T> {
public static final float DEFAULT_LOAD_FACTOR = 0.75f;
static final IntUnaryOperator CAPACITY = new IntUnaryOperator() {
@Override
public int applyAsInt(int size) {
// Generate a suitable capacity for a given initial size
return size * 2;
}
};
public HashSetExternalizer(Class<T> targetClass, IntFunction<T> factory) {
super(targetClass, new CapacityFactory<>(factory));
}
/**
* Creates a hash table based map or collection with an appropriate capacity given an initial size.
* @param <T> the map or collection type.
*/
public static class CapacityFactory<T> implements Function<Integer, T>, IntFunction<T> {
private final IntFunction<T> factory;
public CapacityFactory(IntFunction<T> factory) {
this.factory = factory;
}
@Override
public T apply(Integer size) {
return this.apply(size.intValue());
}
@Override
public T apply(int size) {
return this.factory.apply(CAPACITY.applyAsInt(size));
}
}
}
| 2,555
| 35.514286
| 103
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/UUIDSerializer.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.spi.util;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.UUID;
import org.wildfly.clustering.marshalling.spi.Serializer;
/**
* {@link Serializer} for a {@link UUID}.
* @author Paul Ferraro
*/
public enum UUIDSerializer implements Serializer<UUID> {
INSTANCE;
@Override
public void write(DataOutput output, UUID value) throws IOException {
output.writeLong(value.getMostSignificantBits());
output.writeLong(value.getLeastSignificantBits());
}
@Override
public UUID read(DataInput input) throws IOException {
return new UUID(input.readLong(), input.readLong());
}
}
| 1,742
| 33.86
| 73
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/DateExternalizer.java
|
/*
* JBoss, Home of Professional Open Source.
* Copyright 2016, 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.spi.util;
import java.util.Date;
import java.util.function.LongFunction;
import org.wildfly.clustering.marshalling.spi.LongExternalizer;
/**
* Externalizers for {@link Date} implementations.
* @author Paul Ferraro
*/
public class DateExternalizer<D extends Date> extends LongExternalizer<D> {
public DateExternalizer(Class<D> targetClass, LongFunction<D> factory) {
super(targetClass, factory, Date::getTime);
}
}
| 1,504
| 36.625
| 76
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/MapEntryExternalizer.java
|
/*
* JBoss, Home of Professional Open Source.
* Copyright 2015, 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.spi.util;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Map;
import java.util.function.BiFunction;
import org.wildfly.clustering.marshalling.Externalizer;
/**
* Externalizer for {@link Map.Entry} types
* @author Paul Ferraro
*/
public class MapEntryExternalizer<T extends Map.Entry<Object, Object>> implements Externalizer<T> {
private final Class<T> targetClass;
private final BiFunction<Object, Object, T> factory;
@SuppressWarnings("unchecked")
public MapEntryExternalizer(Class<?> targetClass, BiFunction<Object, Object, T> factory) {
this.targetClass = (Class<T>) targetClass;
this.factory = factory;
}
@Override
public void writeObject(ObjectOutput output, T entry) throws IOException {
output.writeObject(entry.getKey());
output.writeObject(entry.getValue());
}
@Override
public T readObject(ObjectInput input) throws IOException, ClassNotFoundException {
return this.factory.apply(input.readObject(), input.readObject());
}
@Override
public Class<T> getTargetClass() {
return this.targetClass;
}
}
| 2,244
| 34.078125
| 99
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/UnboundedCollectionExternalizer.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.spi.util;
import java.util.Collection;
import java.util.Map;
import java.util.function.Supplier;
import org.wildfly.clustering.marshalling.spi.ValueFunction;
import org.wildfly.clustering.marshalling.spi.SupplierFunction;
import org.wildfly.clustering.marshalling.spi.ValueExternalizer;
/**
* Externalizer for unbounded implementations of {@link Collection}.
* @author Paul Ferraro
*/
public class UnboundedCollectionExternalizer<T extends Collection<Object>> extends CollectionExternalizer<T, Void, Void> {
public UnboundedCollectionExternalizer(Class<T> targetClass, Supplier<T> factory) {
super(targetClass, new SupplierFunction<>(factory), Map.Entry::getKey, ValueFunction.voidFunction(), ValueExternalizer.VOID);
}
}
| 1,812
| 41.162791
| 133
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/MapExternalizer.java
|
/*
* JBoss, Home of Professional Open Source.
* Copyright 2015, 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.spi.util;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.AbstractMap;
import java.util.Map;
import java.util.OptionalInt;
import java.util.function.Function;
import org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.clustering.marshalling.spi.IndexSerializer;
/**
* Externalizers for implementations of {@link Map}.
* @author Paul Ferraro
*/
public class MapExternalizer<T extends Map<Object, Object>, C, CC> implements Externalizer<T> {
private final Class<T> targetClass;
private final Function<CC, T> factory;
private final Function<Map.Entry<C, Integer>, CC> constructorContext;
private final Function<T, C> context;
private final Externalizer<C> contextExternalizer;
protected MapExternalizer(Class<T> targetClass, Function<CC, T> factory, Function<Map.Entry<C, Integer>, CC> constructorContext, Function<T, C> context, Externalizer<C> contextExternalizer) {
this.targetClass = targetClass;
this.factory = factory;
this.constructorContext = constructorContext;
this.context = context;
this.contextExternalizer = contextExternalizer;
}
@Override
public void writeObject(ObjectOutput output, T map) throws IOException {
synchronized (map) { // Avoid ConcurrentModificationException
C context = this.context.apply(map);
this.contextExternalizer.writeObject(output, context);
IndexSerializer.VARIABLE.writeInt(output, map.size());
for (Map.Entry<Object, Object> entry : map.entrySet()) {
output.writeObject(entry.getKey());
output.writeObject(entry.getValue());
}
}
}
@Override
public T readObject(ObjectInput input) throws IOException, ClassNotFoundException {
C context = this.contextExternalizer.readObject(input);
int size = IndexSerializer.VARIABLE.readInt(input);
CC constructorContext = this.constructorContext.apply(new AbstractMap.SimpleImmutableEntry<>(context, size));
T map = this.factory.apply(constructorContext);
for (int i = 0; i < size; ++i) {
map.put(input.readObject(), input.readObject());
}
return map;
}
@Override
public OptionalInt size(T map) {
if (!map.isEmpty()) return OptionalInt.empty();
synchronized (map) { // Avoid ConcurrentModificationException
C context = this.context.apply(map);
OptionalInt contextSize = this.contextExternalizer.size(context);
return contextSize.isPresent() ? OptionalInt.of(contextSize.getAsInt() + IndexSerializer.VARIABLE.size(map.size())) : OptionalInt.empty();
}
}
@Override
public Class<T> getTargetClass() {
return this.targetClass;
}
}
| 3,911
| 39.75
| 195
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/UnmodifiableCollectionExternalizer.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.spi.util;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Collection;
import java.util.function.Function;
import org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.clustering.marshalling.spi.IndexSerializer;
/**
* Externalizer for unmodifiable collections created via {@link java.util.List#of()} or {@link java.util.Set#of()} methods.
* @author Paul Ferraro
*/
public class UnmodifiableCollectionExternalizer<T extends Collection<Object>> implements Externalizer<T> {
private final Class<T> targetClass;
private final Function<Object[], T> factory;
public UnmodifiableCollectionExternalizer(Class<T> targetClass, Function<Object[], T> factory) {
this.targetClass = targetClass;
this.factory = factory;
}
@Override
public void writeObject(ObjectOutput output, T collection) throws IOException {
IndexSerializer.VARIABLE.writeInt(output, collection.size());
for (Object object : collection) {
output.writeObject(object);
}
}
@Override
public T readObject(ObjectInput input) throws IOException, ClassNotFoundException {
Object[] elements = new Object[IndexSerializer.VARIABLE.readInt(input)];
for (int i = 0; i < elements.length; ++i) {
elements[i] = input.readObject();
}
return this.factory.apply(elements);
}
@Override
public Class<T> getTargetClass() {
return this.targetClass;
}
}
| 2,588
| 35.985714
| 123
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/UUIDExternalizer.java
|
/*
* JBoss, Home of Professional Open Source.
* Copyright 2012, 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.spi.util;
import java.util.OptionalInt;
import java.util.UUID;
import org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.clustering.marshalling.spi.SerializerExternalizer;
/**
* {@link Externalizer} for {@link UUID} instances.
* @author Paul Ferraro
*/
public class UUIDExternalizer extends SerializerExternalizer<UUID> {
public UUIDExternalizer() {
super(UUID.class, UUIDSerializer.INSTANCE);
}
@Override
public OptionalInt size(UUID object) {
return OptionalInt.of(Long.BYTES + Long.BYTES);
}
}
| 1,624
| 35.111111
| 70
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/ContextualMapExternalizer.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.spi.util;
import java.util.Map;
import java.util.function.Function;
import org.wildfly.clustering.marshalling.Externalizer;
/**
* Externalizer for implementations of {@link Map} constructed with some context.
* @author Paul Ferraro
*/
public class ContextualMapExternalizer<T extends Map<Object, Object>, C> extends MapExternalizer<T, C, C> {
public ContextualMapExternalizer(Class<T> targetClass, Function<C, T> factory, Function<T, C> context, Externalizer<C> contextExternalizer) {
super(targetClass, factory, Map.Entry::getKey, context, contextExternalizer);
}
}
| 1,657
| 40.45
| 145
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/BoundedCollectionExternalizer.java
|
/*
* JBoss, Home of Professional Open Source.
* Copyright 2015, 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.spi.util;
import java.util.Collection;
import java.util.Map;
import java.util.function.IntFunction;
import org.wildfly.clustering.marshalling.spi.ValueFunction;
import org.wildfly.clustering.marshalling.spi.ValueExternalizer;
/**
* Externalizer for bounded implementations of {@link Collection}.
* @author Paul Ferraro
*/
public class BoundedCollectionExternalizer<T extends Collection<Object>> extends CollectionExternalizer<T, Void, Integer> {
public BoundedCollectionExternalizer(Class<T> targetClass, IntFunction<T> factory) {
super(targetClass, factory::apply, Map.Entry::getValue, ValueFunction.voidFunction(), ValueExternalizer.VOID);
}
}
| 1,736
| 40.357143
| 123
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/UnmodifiableMapExternalizer.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.spi.util;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Map;
import java.util.function.Function;
import org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.clustering.marshalling.spi.IndexSerializer;
/**
* Externalizer for unmodifiable maps created via {@link java.util.Map#of()} or {@link java.util.Map#ofEntries()} methods.
* @author Paul Ferraro
*/
public class UnmodifiableMapExternalizer<T extends Map<Object, Object>> implements Externalizer<T> {
private final Class<T> targetClass;
private final Function<Map.Entry<Object, Object>[], T> factory;
public UnmodifiableMapExternalizer(Class<T> targetClass, Function<Map.Entry<Object, Object>[], T> factory) {
this.targetClass = targetClass;
this.factory = factory;
}
@Override
public void writeObject(ObjectOutput output, T map) throws IOException {
IndexSerializer.VARIABLE.writeInt(output, map.size());
for (Map.Entry<Object, Object> entry : map.entrySet()) {
output.writeObject(entry.getKey());
output.writeObject(entry.getValue());
}
}
@Override
public T readObject(ObjectInput input) throws IOException, ClassNotFoundException {
@SuppressWarnings("unchecked")
Map.Entry<Object, Object>[] entries = new Map.Entry[IndexSerializer.VARIABLE.readInt(input)];
for (int i = 0; i < entries.length; ++i) {
entries[i] = Map.entry(input.readObject(), input.readObject());
}
return this.factory.apply(entries);
}
@Override
public Class<T> getTargetClass() {
return this.targetClass;
}
}
| 2,759
| 37.333333
| 122
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/concurrent/ConcurrentExternalizerProvider.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.spi.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.Externalizer;
import org.wildfly.clustering.marshalling.spi.EnumExternalizer;
import org.wildfly.clustering.marshalling.spi.ExternalizerProvider;
import org.wildfly.clustering.marshalling.spi.util.CopyOnWriteCollectionExternalizer;
import org.wildfly.clustering.marshalling.spi.util.HashMapExternalizer;
import org.wildfly.clustering.marshalling.spi.util.HashSetExternalizer;
import org.wildfly.clustering.marshalling.spi.util.SortedMapExternalizer;
import org.wildfly.clustering.marshalling.spi.util.SortedSetExternalizer;
import org.wildfly.clustering.marshalling.spi.util.UnboundedCollectionExternalizer;
/**
* @author Paul Ferraro
*/
public enum ConcurrentExternalizerProvider implements ExternalizerProvider {
CONCURRENT_HASH_MAP(new HashMapExternalizer<>(ConcurrentHashMap.class, ConcurrentHashMap::new)),
CONCURRENT_HASH_SET(new HashSetExternalizer<>(ConcurrentHashMap.KeySetView.class, ConcurrentHashMap::newKeySet)),
CONCURRENT_LINKED_DEQUE(new UnboundedCollectionExternalizer<>(ConcurrentLinkedDeque.class, ConcurrentLinkedDeque::new)),
CONCURRENT_LINKED_QUEUE(new UnboundedCollectionExternalizer<>(ConcurrentLinkedQueue.class, ConcurrentLinkedQueue::new)),
CONCURRENT_SKIP_LIST_MAP(new SortedMapExternalizer<>(ConcurrentSkipListMap.class, ConcurrentSkipListMap::new)),
CONCURRENT_SKIP_LIST_SET(new SortedSetExternalizer<>(ConcurrentSkipListSet.class, ConcurrentSkipListSet::new)),
COPY_ON_WRITE_ARRAY_LIST(new CopyOnWriteCollectionExternalizer<>(CopyOnWriteArrayList.class, CopyOnWriteArrayList::new)),
COPY_ON_WRITE_ARRAY_SET(new CopyOnWriteCollectionExternalizer<>(CopyOnWriteArraySet.class, CopyOnWriteArraySet::new)),
TIME_UNIT(new EnumExternalizer<>(TimeUnit.class)),
;
private final Externalizer<?> externalizer;
ConcurrentExternalizerProvider(Externalizer<?> externalizer) {
this.externalizer = externalizer;
}
@Override
public Externalizer<?> getExternalizer() {
return this.externalizer;
}
}
| 3,518
| 49.271429
| 125
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/util/concurrent/atomic/AtomicExternalizerProvider.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.spi.util.concurrent.atomic;
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 org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.clustering.marshalling.spi.BooleanExternalizer;
import org.wildfly.clustering.marshalling.spi.ExternalizerProvider;
import org.wildfly.clustering.marshalling.spi.IntExternalizer;
import org.wildfly.clustering.marshalling.spi.LongExternalizer;
import org.wildfly.clustering.marshalling.spi.ObjectExternalizer;
/**
* Externalizers for the java.util.concurrent.atomic package.
* @author Paul Ferraro
*/
public enum AtomicExternalizerProvider implements ExternalizerProvider {
ATOMIC_BOOLEAN(new BooleanExternalizer<>(AtomicBoolean.class, AtomicBoolean::new, AtomicBoolean::get)),
ATOMIC_INTEGER(new IntExternalizer<>(AtomicInteger.class, AtomicInteger::new, AtomicInteger::get)),
ATOMIC_LONG(new LongExternalizer<>(AtomicLong.class, AtomicLong::new, AtomicLong::get)),
ATOMIC_REFERENCE(new ObjectExternalizer<>(AtomicReference.class, AtomicReference::new, AtomicReference::get)),
;
private final Externalizer<?> externalizer;
AtomicExternalizerProvider(Externalizer<?> externalizer) {
this.externalizer = externalizer;
}
@Override
public Externalizer<?> getExternalizer() {
return this.externalizer;
}
}
| 2,539
| 42.050847
| 114
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/time/YearMonthExternalizer.java
|
/*
* JBoss, Home of Professional Open Source.
* Copyright 2016, 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.spi.time;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.time.Month;
import java.time.YearMonth;
import java.util.OptionalInt;
import org.wildfly.clustering.marshalling.Externalizer;
/**
* Externalizer for a {@link YearMonth}.
* @author Paul Ferraro
*/
public class YearMonthExternalizer implements Externalizer<YearMonth> {
@Override
public void writeObject(ObjectOutput output, YearMonth value) throws IOException {
output.writeInt(value.getYear());
TimeExternalizerProvider.MONTH.cast(Month.class).writeObject(output, value.getMonth());
}
@Override
public YearMonth readObject(ObjectInput input) throws IOException, ClassNotFoundException {
int year = input.readInt();
Month month = TimeExternalizerProvider.MONTH.cast(Month.class).readObject(input);
return YearMonth.of(year, month);
}
@Override
public Class<YearMonth> getTargetClass() {
return YearMonth.class;
}
@Override
public OptionalInt size(YearMonth value) {
return OptionalInt.of(Integer.BYTES + TimeExternalizerProvider.MONTH.size(value.getMonth()).getAsInt());
}
}
| 2,262
| 34.920635
| 112
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/time/InstantExternalizer.java
|
/*
* JBoss, Home of Professional Open Source.
* Copyright 2015, 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.spi.time;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.time.Instant;
import java.util.OptionalInt;
import org.wildfly.clustering.marshalling.Externalizer;
/**
* Externalizer for an {@link Instant}.
* @author Paul Ferraro
*/
public class InstantExternalizer implements Externalizer<Instant> {
@Override
public void writeObject(ObjectOutput output, Instant instant) throws IOException {
output.writeLong(instant.getEpochSecond());
output.writeInt(instant.getNano());
}
@Override
public Instant readObject(ObjectInput input) throws IOException, ClassNotFoundException {
long seconds = input.readLong();
int nanos = input.readInt();
return Instant.ofEpochSecond(seconds, nanos);
}
@Override
public Class<Instant> getTargetClass() {
return Instant.class;
}
@Override
public OptionalInt size(Instant object) {
return OptionalInt.of(Long.BYTES + Integer.BYTES);
}
}
| 2,092
| 32.758065
| 93
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/time/MonthDayExternalizer.java
|
/*
* JBoss, Home of Professional Open Source.
* Copyright 2016, 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.spi.time;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.time.Month;
import java.time.MonthDay;
import java.util.OptionalInt;
import org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.clustering.marshalling.spi.IndexSerializer;
/**
* Externalizer for a {@link MonthDay}.
* @author Paul Ferraro
*/
public class MonthDayExternalizer implements Externalizer<MonthDay> {
@Override
public void writeObject(ObjectOutput output, MonthDay value) throws IOException {
TimeExternalizerProvider.MONTH.cast(Month.class).writeObject(output, value.getMonth());
IndexSerializer.UNSIGNED_BYTE.writeInt(output, value.getDayOfMonth());
}
@Override
public MonthDay readObject(ObjectInput input) throws IOException, ClassNotFoundException {
Month month = TimeExternalizerProvider.MONTH.cast(Month.class).readObject(input);
int day = IndexSerializer.UNSIGNED_BYTE.readInt(input);
return MonthDay.of(month, day);
}
@Override
public Class<MonthDay> getTargetClass() {
return MonthDay.class;
}
@Override
public OptionalInt size(MonthDay value) {
return OptionalInt.of(TimeExternalizerProvider.MONTH.size(value.getMonth()).getAsInt() + IndexSerializer.UNSIGNED_BYTE.size(value.getDayOfMonth()));
}
}
| 2,423
| 36.875
| 156
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/time/TimeExternalizerProvider.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.spi.time;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Year;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.clustering.marshalling.spi.BinaryExternalizer;
import org.wildfly.clustering.marshalling.spi.EnumExternalizer;
import org.wildfly.clustering.marshalling.spi.ExternalizerProvider;
import org.wildfly.clustering.marshalling.spi.IntExternalizer;
import org.wildfly.clustering.marshalling.spi.LongExternalizer;
import org.wildfly.clustering.marshalling.spi.StringExternalizer;
/**
* Externalizers for the java.time package
* @author Paul Ferraro
*/
public enum TimeExternalizerProvider implements ExternalizerProvider {
DAY_OF_WEEK(new EnumExternalizer<>(DayOfWeek.class)),
DURATION(new DurationExternalizer()),
INSTANT(new InstantExternalizer()),
LOCAL_DATE(new LongExternalizer<>(LocalDate.class, LocalDate::ofEpochDay, LocalDate::toEpochDay)),
LOCAL_TIME(new LongExternalizer<>(LocalTime.class, LocalTime::ofNanoOfDay, LocalTime::toNanoOfDay)),
LOCAL_DATE_TIME(new BinaryExternalizer<>(LocalDateTime.class, LOCAL_DATE.cast(LocalDate.class), LOCAL_TIME.cast(LocalTime.class), LocalDateTime::toLocalDate, LocalDateTime::toLocalTime, LocalDateTime::of)),
MONTH(new EnumExternalizer<>(Month.class)),
MONTH_DAY(new MonthDayExternalizer()),
PERIOD(new PeriodExternalizer()),
YEAR(new IntExternalizer<>(Year.class, Year::of, Year::getValue)),
YEAR_MONTH(new YearMonthExternalizer()),
ZONE_ID(new StringExternalizer<>(ZoneId.class, ZoneId::of, ZoneId::getId)),
ZONE_OFFSET(new StringExternalizer<>(ZoneOffset.class, ZoneOffset::of, ZoneOffset::getId)),
// w/offset
OFFSET_DATE_TIME(new BinaryExternalizer<>(OffsetDateTime.class, LOCAL_DATE_TIME.cast(LocalDateTime.class), ZONE_OFFSET.cast(ZoneOffset.class), OffsetDateTime::toLocalDateTime, OffsetDateTime::getOffset, OffsetDateTime::of)),
OFFSET_TIME(new BinaryExternalizer<>(OffsetTime.class, LOCAL_TIME.cast(LocalTime.class), ZONE_OFFSET.cast(ZoneOffset.class), OffsetTime::toLocalTime, OffsetTime::getOffset, OffsetTime::of)),
ZONED_DATE_TIME(new BinaryExternalizer<>(ZonedDateTime.class, LOCAL_DATE_TIME.cast(LocalDateTime.class), ZONE_ID.cast(ZoneId.class), ZonedDateTime::toLocalDateTime, ZonedDateTime::getZone, ZonedDateTime::of)),
;
private final Externalizer<?> externalizer;
TimeExternalizerProvider(Externalizer<?> externalizer) {
this.externalizer = externalizer;
}
@Override
public Externalizer<?> getExternalizer() {
return this.externalizer;
}
}
| 3,887
| 47.6
| 228
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/time/PeriodExternalizer.java
|
/*
* JBoss, Home of Professional Open Source.
* Copyright 2016, 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.spi.time;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.time.Period;
import java.util.OptionalInt;
import org.wildfly.clustering.marshalling.Externalizer;
/**
* Externalizer for a {@link Period}.
* @author Paul Ferraro
*/
public class PeriodExternalizer implements Externalizer<Period> {
@Override
public void writeObject(ObjectOutput output, Period period) throws IOException {
output.writeInt(period.getYears());
output.writeInt(period.getMonths());
output.writeInt(period.getDays());
}
@Override
public Period readObject(ObjectInput input) throws IOException, ClassNotFoundException {
int years = input.readInt();
int months = input.readInt();
int days = input.readInt();
return Period.of(years, months, days);
}
@Override
public Class<Period> getTargetClass() {
return Period.class;
}
@Override
public OptionalInt size(Period object) {
return OptionalInt.of(Integer.BYTES * 3);
}
}
| 2,134
| 32.359375
| 92
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/time/DurationExternalizer.java
|
/*
* JBoss, Home of Professional Open Source.
* Copyright 2016, 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.spi.time;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.time.Duration;
import java.util.OptionalInt;
import org.wildfly.clustering.marshalling.Externalizer;
/**
* Externalizer for a {@link Duration}.
* @author Paul Ferraro
*/
public class DurationExternalizer implements Externalizer<Duration> {
@Override
public void writeObject(ObjectOutput output, Duration duration) throws IOException {
output.writeLong(duration.getSeconds());
output.writeInt(duration.getNano());
}
@Override
public Duration readObject(ObjectInput input) throws IOException, ClassNotFoundException {
long seconds = input.readLong();
int nanos = input.readInt();
return Duration.ofSeconds(seconds, nanos);
}
@Override
public Class<Duration> getTargetClass() {
return Duration.class;
}
@Override
public OptionalInt size(Duration object) {
return OptionalInt.of(Long.BYTES + Integer.BYTES);
}
}
| 2,096
| 32.822581
| 94
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/net/InetAddressExternalizer.java
|
/*
* JBoss, Home of Professional Open Source.
* Copyright 2017, 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.spi.net;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.net.InetAddress;
import java.util.OptionalInt;
import org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.clustering.marshalling.spi.IndexSerializer;
/**
* @author Paul Ferraro
*/
public class InetAddressExternalizer<A extends InetAddress> implements Externalizer<A> {
private final Class<A> targetClass;
private final OptionalInt size;
public InetAddressExternalizer(Class<A> targetClass, OptionalInt size) {
this.targetClass = targetClass;
this.size = size;
}
@Override
public void writeObject(ObjectOutput output, A address) throws IOException {
if (!this.size.isPresent()) {
int length = (address != null) ? address.getAddress().length : 0;
IndexSerializer.UNSIGNED_BYTE.writeInt(output, length);
}
if (address != null) {
output.write(address.getAddress());
}
}
@Override
public A readObject(ObjectInput input) throws IOException, ClassNotFoundException {
int size = this.size.isPresent() ? this.size.getAsInt() : IndexSerializer.UNSIGNED_BYTE.readInt(input);
if (size == 0) return null;
byte[] bytes = new byte[size];
input.readFully(bytes);
return this.targetClass.cast(InetAddress.getByAddress(bytes));
}
@Override
public Class<A> getTargetClass() {
return this.targetClass;
}
@Override
public OptionalInt size(A address) {
if (this.size.isPresent()) return this.size;
int length = (address != null) ? address.getAddress().length : 0;
return OptionalInt.of(IndexSerializer.UNSIGNED_BYTE.size(length) + length);
}
}
| 2,842
| 34.987342
| 111
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/net/NetExternalizerProvider.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.spi.net;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.util.OptionalInt;
import org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.clustering.marshalling.spi.ExternalizerProvider;
import org.wildfly.clustering.marshalling.spi.StringExternalizer;
/**
* Externalizers for the java.net package.
* @author Paul Ferraro
*/
public enum NetExternalizerProvider implements ExternalizerProvider {
INET_ADDRESS(new InetAddressExternalizer<>(InetAddress.class, OptionalInt.empty())),
INET4_ADDRESS(new InetAddressExternalizer<>(Inet4Address.class, OptionalInt.of(4))),
INET6_ADDRESS(new InetAddressExternalizer<>(Inet6Address.class, OptionalInt.of(16))),
INET_SOCKET_ADDRESS(new InetSocketAddressExternalizer()),
URI(new StringExternalizer<>(java.net.URI.class, java.net.URI::create, java.net.URI::toString)),
URL(new URLExternalizer()),
;
private final Externalizer<?> externalizer;
NetExternalizerProvider(Externalizer<?> externalizer) {
this.externalizer = externalizer;
}
@Override
public Externalizer<?> getExternalizer() {
return this.externalizer;
}
}
| 2,264
| 38.051724
| 100
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/net/InetSocketAddressExternalizer.java
|
/*
* JBoss, Home of Professional Open Source.
* Copyright 2017, 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.spi.net;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.OptionalInt;
import org.wildfly.clustering.marshalling.Externalizer;
import org.wildfly.clustering.marshalling.spi.IndexSerializer;
/**
* @author Paul Ferraro
*/
public class InetSocketAddressExternalizer implements Externalizer<InetSocketAddress> {
@Override
public void writeObject(ObjectOutput output, InetSocketAddress socketAddress) throws IOException {
InetAddress address = socketAddress.getAddress();
NetExternalizerProvider.INET_ADDRESS.writeObject(output, address);
IndexSerializer.UNSIGNED_SHORT.writeInt(output, socketAddress.getPort());
if (address == null) {
output.writeUTF(socketAddress.getHostName());
}
}
@Override
public InetSocketAddress readObject(ObjectInput input) throws IOException, ClassNotFoundException {
InetAddress address = NetExternalizerProvider.INET_ADDRESS.cast(InetAddress.class).readObject(input);
int port = IndexSerializer.UNSIGNED_SHORT.readInt(input);
return (address != null) ? new InetSocketAddress(address, port) : InetSocketAddress.createUnresolved(input.readUTF(), port);
}
@Override
public Class<InetSocketAddress> getTargetClass() {
return InetSocketAddress.class;
}
@Override
public OptionalInt size(InetSocketAddress socketAddress) {
int size = NetExternalizerProvider.INET_ADDRESS.size(socketAddress.getAddress()).getAsInt() + IndexSerializer.UNSIGNED_SHORT.size(socketAddress.getPort());
if (socketAddress.getAddress() == null) {
size += socketAddress.getHostName().length() + 1;
}
return OptionalInt.of(size);
}
}
| 2,894
| 39.774648
| 163
|
java
|
null |
wildfly-main/clustering/marshalling/spi/src/main/java/org/wildfly/clustering/marshalling/spi/net/URLExternalizer.java
|
/*
* JBoss, Home of Professional Open Source.
* Copyright 2016, 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.spi.net;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.OptionalInt;
import org.wildfly.clustering.marshalling.Externalizer;
/**
* Externalizer for a {@link URL}.
* @author Paul Ferraro
*/
public class URLExternalizer implements Externalizer<URL> {
@Override
public void writeObject(ObjectOutput output, URL url) throws IOException {
try {
NetExternalizerProvider.URI.cast(URI.class).writeObject(output, url.toURI());
} catch (URISyntaxException e) {
throw new IOException(e);
}
}
@Override
public URL readObject(ObjectInput input) throws IOException, ClassNotFoundException {
return NetExternalizerProvider.URI.cast(URI.class).readObject(input).toURL();
}
@Override
public Class<URL> getTargetClass() {
return URL.class;
}
@Override
public OptionalInt size(URL url) {
try {
return NetExternalizerProvider.URI.cast(URI.class).size(url.toURI());
} catch (URISyntaxException e) {
return OptionalInt.empty();
}
}
}
| 2,282
| 32.086957
| 89
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/SerializationTestMarshaller.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;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InvalidClassException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.ByteBuffer;
/**
* Marshaller that uses Java serialization.
* @author Paul Ferraro
*/
public class SerializationTestMarshaller<T> implements TestMarshaller<T> {
@SuppressWarnings("unchecked")
@Override
public T read(ByteBuffer buffer) throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream(buffer.array(), buffer.arrayOffset(), buffer.limit() - buffer.arrayOffset());
try (ObjectInputStream input = new ObjectInputStream(in)) {
return (T) input.readObject();
} catch (ClassNotFoundException e) {
InvalidClassException exception = new InvalidClassException(e.getMessage());
exception.initCause(e);
throw exception;
}
}
@Override
public ByteBuffer write(T object) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try (ObjectOutputStream output = new ObjectOutputStream(out)) {
output.writeObject(object);
}
return ByteBuffer.wrap(out.toByteArray());
}
}
| 2,351
| 36.935484
| 136
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/AbstractAtomicTestCase.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;
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 org.junit.Assert;
import org.junit.Test;
/**
* Generic tests for java.util.concurrent.atomic.* classes.
* @author Paul Ferraro
*/
public abstract class AbstractAtomicTestCase {
private final MarshallingTesterFactory factory;
public AbstractAtomicTestCase(MarshallingTesterFactory factory) {
this.factory = factory;
}
@Test
public void testAtomicBoolean() throws IOException {
MarshallingTester<AtomicBoolean> tester = this.factory.createTester();
tester.test(new AtomicBoolean(false), (expected, actual) -> Assert.assertEquals(expected.get(), actual.get()));
tester.test(new AtomicBoolean(true), (expected, actual) -> Assert.assertEquals(expected.get(), actual.get()));
}
@Test
public void testAtomicInteger() throws IOException {
MarshallingTester<AtomicInteger> tester = this.factory.createTester();
tester.test(new AtomicInteger(), (expected, actual) -> Assert.assertEquals(expected.get(), actual.get()));
tester.test(new AtomicInteger(Byte.MAX_VALUE), (expected, actual) -> Assert.assertEquals(expected.get(), actual.get()));
tester.test(new AtomicInteger(Integer.MAX_VALUE), (expected, actual) -> Assert.assertEquals(expected.get(), actual.get()));
}
@Test
public void testAtomicLong() throws IOException {
MarshallingTester<AtomicLong> tester = this.factory.createTester();
tester.test(new AtomicLong(), (expected, actual) -> Assert.assertEquals(expected.get(), actual.get()));
tester.test(new AtomicLong(Short.MAX_VALUE), (expected, actual) -> Assert.assertEquals(expected.get(), actual.get()));
tester.test(new AtomicLong(Long.MAX_VALUE), (expected, actual) -> Assert.assertEquals(expected.get(), actual.get()));
}
@Test
public void testAtomicReference() throws IOException {
MarshallingTester<AtomicReference<Object>> tester = this.factory.createTester();
tester.test(new AtomicReference<>(), (expected, actual) -> Assert.assertEquals(expected.get(), actual.get()));
tester.test(new AtomicReference<>(Boolean.TRUE), (expected, actual) -> Assert.assertEquals(expected.get(), actual.get()));
}
}
| 3,497
| 45.026316
| 131
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/EnumMarshallingTester.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;
import java.io.IOException;
import java.util.EnumSet;
import org.junit.Assert;
/**
* Validates marshalling of an enum.
* @author Paul Ferraro
*/
public class EnumMarshallingTester<E extends Enum<E>> {
private final Class<E> enumClass;
private final MarshallingTester<E> tester;
public EnumMarshallingTester(Class<E> enumClass, MarshallingTester<E> tester) {
this.enumClass = enumClass;
this.tester = tester;
}
public void test() throws IOException {
for (E value : EnumSet.allOf(this.enumClass)) {
this.tester.test(value, Assert::assertSame);
}
}
}
| 1,694
| 32.9
| 83
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/AbstractMathTestCase.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;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.Random;
import org.junit.Test;
/**
* @author Paul Ferraro
*/
public abstract class AbstractMathTestCase {
private final MarshallingTesterFactory factory;
private final Random random = new Random(System.currentTimeMillis());
public AbstractMathTestCase(MarshallingTesterFactory factory) {
this.factory = factory;
}
private BigInteger probablePrime() {
return BigInteger.probablePrime(Byte.MAX_VALUE, this.random);
}
@Test
public void testBigInteger() throws IOException {
this.testBigInteger(BigInteger.ZERO);
this.testBigInteger(BigInteger.ONE);
this.testBigInteger(BigInteger.TEN);
this.testBigInteger(this.probablePrime());
}
private void testBigInteger(BigInteger value) throws IOException {
this.factory.createTester().test(value);
this.factory.createTester().test(value.negate());
}
@Test
public void testBigDecimal() throws IOException {
this.testBigDecimal(BigDecimal.ZERO);
this.testBigDecimal(BigDecimal.ONE);
this.testBigDecimal(BigDecimal.TEN);
this.testBigDecimal(new BigDecimal(this.probablePrime(), Integer.MAX_VALUE));
this.testBigDecimal(new BigDecimal(this.probablePrime(), Integer.MIN_VALUE));
}
private void testBigDecimal(BigDecimal value) throws IOException {
this.factory.createTester().test(value);
this.factory.createTester().test(value.negate());
}
@Test
public void testMathContext() throws IOException {
this.factory.createTester().test(new MathContext(0));
this.factory.createTester().test(new MathContext(10, RoundingMode.UNNECESSARY));
}
@Test
public void testRoundingMode() throws IOException {
this.factory.createTester(RoundingMode.class).test();
}
}
| 3,062
| 33.806818
| 88
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/AbstractSQLTestCase.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;
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.junit.Test;
/**
* Generic tests for java.sql.* classes.
* @author Paul Ferraro
*/
public abstract class AbstractSQLTestCase {
private final MarshallingTesterFactory factory;
public AbstractSQLTestCase(MarshallingTesterFactory factory) {
this.factory = factory;
}
@Test
public void testSQLDate() throws IOException {
MarshallingTester<Date> tester = this.factory.createTester();
tester.test(Date.valueOf(LocalDate.now()));
}
@Test
public void testSQLTime() throws IOException {
MarshallingTester<Time> tester = this.factory.createTester();
tester.test(Time.valueOf(LocalTime.now()));
}
@Test
public void testSQLTimestamp() throws IOException {
MarshallingTester<Timestamp> tester = this.factory.createTester();
tester.test(Timestamp.valueOf(LocalDateTime.now()));
}
}
| 2,148
| 32.061538
| 74
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/TestInvocationHandlerExternalizer.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;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import org.kohsuke.MetaInfServices;
/**
* @author Paul Ferraro
*/
@MetaInfServices(Externalizer.class)
public class TestInvocationHandlerExternalizer implements Externalizer<TestInvocationHandler> {
@Override
public void writeObject(ObjectOutput output, TestInvocationHandler handler) throws IOException {
output.writeObject(handler.getValue());
}
@Override
public TestInvocationHandler readObject(ObjectInput input) throws IOException, ClassNotFoundException {
return new TestInvocationHandler(input.readObject());
}
@Override
public Class<TestInvocationHandler> getTargetClass() {
return TestInvocationHandler.class;
}
}
| 1,844
| 34.480769
| 107
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/MarshallingTester.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;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.function.BiConsumer;
import org.junit.Assert;
/**
* Validates correctness of the marshalling of an object.
* @author Paul Ferraro
*/
public class MarshallingTester<T> implements Tester<T> {
private final TestMarshaller<T> serializationMarshaller = new SerializationTestMarshaller<>();
private final TestMarshaller<T> marshaller;
public MarshallingTester(TestMarshaller<T> marshaller) {
this.marshaller = marshaller;
}
@Override
public void test(T subject, BiConsumer<T, T> assertion) throws IOException {
ByteBuffer buffer = this.marshaller.write(subject);
int size = buffer.limit() - buffer.arrayOffset();
if (subject != null) {
// Uncomment to report payload size
// System.out.println(String.format("%s\t%s\t%s", (subject instanceof Enum) ? ((Enum<?>) subject).getDeclaringClass().getCanonicalName() : subject.getClass().getCanonicalName(), (subject instanceof Character) ? (int) (Character) subject : subject, size));
}
T result = this.marshaller.read(buffer);
assertion.accept(subject, result);
// If object is serializable, verify that we have improved upon default serialization size
if (subject instanceof java.io.Serializable) {
ByteBuffer serializationBuffer = this.serializationMarshaller.write(subject);
int serializationSize = serializationBuffer.limit() - serializationBuffer.arrayOffset();
Assert.assertTrue(String.format("Marshaller size = %d, Default serialization size = %d", size, serializationSize), size < serializationSize);
}
}
}
| 2,775
| 41.060606
| 267
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/ExternalizerTester.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;
/**
* A marshalling tester for a single externalizer.
* @author Paul Ferraro
*/
public class ExternalizerTester<T> extends MarshallingTester<T> {
public ExternalizerTester(Externalizer<T> externalizer) {
super(new ExternalizerMarshaller<>(externalizer));
}
}
| 1,348
| 37.542857
| 70
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/TestComparator.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;
import java.util.Comparator;
/**
* @author Paul Ferraro
*/
public class TestComparator<T> implements Comparator<T>, java.io.Serializable {
private static final long serialVersionUID = 2322453812130991741L;
@Override
public int compare(T object1, T object2) {
return object1.hashCode() - object2.hashCode();
}
}
| 1,408
| 36.078947
| 79
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/AbstractLangTestCase.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;
import java.io.IOException;
import java.lang.reflect.Proxy;
import java.util.UUID;
import org.junit.Assert;
import org.junit.Test;
/**
* Validates marshalling of java.lang* objects.
* @author Paul Ferraro
*/
public abstract class AbstractLangTestCase {
private final MarshallingTesterFactory factory;
public AbstractLangTestCase(MarshallingTesterFactory factory) {
this.factory = factory;
}
@Test
public void testBoolean() throws IOException {
this.factory.createTester().test(true);
}
@Test
public void testByte() throws IOException {
Tester<Byte> tester = this.factory.createTester();
for (int i = 0; i < Byte.SIZE; ++i) {
tester.test(Integer.valueOf((1 << i) - 1).byteValue());
tester.test(Integer.valueOf(-1 << i).byteValue());
}
}
@Test
public void testShort() throws IOException {
Tester<Short> tester = this.factory.createTester();
for (int i = 0; i < Short.SIZE; ++i) {
tester.test(Integer.valueOf((1 << i) - 1).shortValue());
tester.test(Integer.valueOf(-1 << i).shortValue());
}
}
@Test
public void testInteger() throws IOException {
Tester<Integer> tester = this.factory.createTester();
for (int i = 0; i < Integer.SIZE; ++i) {
tester.test((1 << i) - 1);
tester.test(-1 << i);
}
}
@Test
public void testLong() throws IOException {
Tester<Long> tester = this.factory.createTester();
for (int i = 0; i < Long.SIZE; ++i) {
tester.test((1L << i) - 1L);
tester.test(-1L << i);
}
}
@Test
public void testFloat() throws IOException {
Tester<Float> tester = this.factory.createTester();
tester.test(Float.NEGATIVE_INFINITY);
tester.test(Float.MIN_VALUE);
tester.test(0F);
tester.test(Float.MAX_VALUE);
tester.test(Float.POSITIVE_INFINITY);
tester.test(Float.NaN);
}
@Test
public void testDouble() throws IOException {
Tester<Double> tester = this.factory.createTester();
tester.test(Double.NEGATIVE_INFINITY);
tester.test(Double.MIN_VALUE);
tester.test(0D);
tester.test(Double.MAX_VALUE);
tester.test(Double.POSITIVE_INFINITY);
tester.test(Double.NaN);
}
@Test
public void testCharacter() throws IOException {
Tester<Character> tester = this.factory.createTester();
tester.test(Character.MIN_VALUE);
tester.test('A');
tester.test(Character.MAX_VALUE);
}
@Test
public void testString() throws IOException {
Tester<String> tester = this.factory.createTester();
tester.test("A");
tester.test(UUID.randomUUID().toString());
}
@Test
public void testBooleanArray() throws IOException {
boolean[] array = new boolean[] { true, false };
this.factory.<boolean[]>createTester().test(array, Assert::assertArrayEquals);
this.factory.<boolean[][]>createTester().test(new boolean[][] { array, array }, Assert::assertArrayEquals);
Boolean[] objectArray = new Boolean[] { true, false };
this.factory.<Boolean[]>createTester().test(objectArray, Assert::assertArrayEquals);
this.factory.<Boolean[][]>createTester().test(new Boolean[][] { objectArray, objectArray }, Assert::assertArrayEquals);
this.factory.<Object[][]>createTester().test(new Object[][] { objectArray, objectArray }, Assert::assertArrayEquals);
}
@Test
public void testByteArray() throws IOException {
byte[] array = new byte[] { Byte.MIN_VALUE, 0, Byte.MAX_VALUE };
this.factory.<byte[]>createTester().test(array, Assert::assertArrayEquals);
this.factory.<byte[][]>createTester().test(new byte[][] { array, array }, Assert::assertArrayEquals);
Byte[] objectArray = new Byte[] { Byte.MIN_VALUE, 0, Byte.MAX_VALUE };
this.factory.<Byte[]>createTester().test(objectArray, Assert::assertArrayEquals);
this.factory.<Byte[][]>createTester().test(new Byte[][] { objectArray, objectArray}, Assert::assertArrayEquals);
this.factory.<Object[][]>createTester().test(new Object[][] { objectArray, objectArray}, Assert::assertArrayEquals);
}
@Test
public void testShortArray() throws IOException {
short[] array = new short[] { Short.MIN_VALUE, 0, Short.MAX_VALUE };
this.factory.<short[]>createTester().test(array, Assert::assertArrayEquals);
this.factory.<short[][]>createTester().test(new short[][] { array, array }, Assert::assertArrayEquals);
Short[] objectArray = new Short[] { Short.MIN_VALUE, 0, Short.MAX_VALUE };
this.factory.<Short[]>createTester().test(objectArray, Assert::assertArrayEquals);
this.factory.<Short[][]>createTester().test(new Short[][] { objectArray, objectArray }, Assert::assertArrayEquals);
this.factory.<Object[][]>createTester().test(new Object[][] { objectArray, objectArray }, Assert::assertArrayEquals);
}
@Test
public void testIntegerArray() throws IOException {
int[] array = new int[] { Integer.MIN_VALUE, 0, Integer.MAX_VALUE };
this.factory.<int[]>createTester().test(array, Assert::assertArrayEquals);
this.factory.<int[][]>createTester().test(new int[][] { array, array }, Assert::assertArrayEquals);
Integer[] objectArray = new Integer[] { Integer.MIN_VALUE, 0, Integer.MAX_VALUE };
this.factory.<Integer[]>createTester().test(objectArray, Assert::assertArrayEquals);
this.factory.<Integer[][]>createTester().test(new Integer[][] { objectArray, objectArray }, Assert::assertArrayEquals);
this.factory.<Object[][]>createTester().test(new Object[][] { objectArray, objectArray }, Assert::assertArrayEquals);
}
@Test
public void testLongArray() throws IOException {
long[] array = new long[] { Long.MIN_VALUE, 0L, Long.MAX_VALUE };
this.factory.<long[]>createTester().test(array, Assert::assertArrayEquals);
this.factory.<long[][]>createTester().test(new long[][] { array, array }, Assert::assertArrayEquals);
Long[] objectArray = new Long[] { Long.MIN_VALUE, 0L, Long.MAX_VALUE };
this.factory.<Long[]>createTester().test(objectArray, Assert::assertArrayEquals);
this.factory.<Long[][]>createTester().test(new Long[][] { objectArray, objectArray }, Assert::assertArrayEquals);
this.factory.<Object[][]>createTester().test(new Object[][] { objectArray, objectArray }, Assert::assertArrayEquals);
}
@Test
public void testFloatArray() throws IOException {
float[] array = new float[] { Float.MIN_VALUE, 0f, Float.MAX_VALUE };
this.factory.<float[]>createTester().test(array, AbstractLangTestCase::assertArrayEquals);
this.factory.<float[][]>createTester().test(new float[][] { array, array }, Assert::assertArrayEquals);
Float[] objectArray = new Float[] { Float.MIN_VALUE, 0f, Float.MAX_VALUE };
this.factory.<Float[]>createTester().test(objectArray, Assert::assertArrayEquals);
this.factory.<Float[][]>createTester().test(new Float[][] { objectArray, objectArray }, Assert::assertArrayEquals);
this.factory.<Object[][]>createTester().test(new Object[][] { objectArray, objectArray }, Assert::assertArrayEquals);
}
@Test
public void testDoubleArray() throws IOException {
double[] array = new double[] { Double.MIN_VALUE, 0d, Double.MAX_VALUE };
this.factory.<double[]>createTester().test(array, AbstractLangTestCase::assertArrayEquals);
this.factory.<double[][]>createTester().test(new double[][] { array, array }, Assert::assertArrayEquals);
Double[] objectArray = new Double[] { Double.MIN_VALUE, 0d, Double.MAX_VALUE };
this.factory.<Double[]>createTester().test(objectArray, Assert::assertArrayEquals);
this.factory.<Double[][]>createTester().test(new Double[][] { objectArray, objectArray }, Assert::assertArrayEquals);
this.factory.<Object[][]>createTester().test(new Object[][] { objectArray, objectArray }, Assert::assertArrayEquals);
}
@Test
public void testCharArray() throws IOException {
char[] array = new char[] { Character.MIN_VALUE, 'A', Character.MAX_VALUE };
this.factory.<char[]>createTester().test(array, Assert::assertArrayEquals);
this.factory.<char[][]>createTester().test(new char[][] { array, array }, Assert::assertArrayEquals);
Character[] objectArray = new Character[] { Character.MIN_VALUE, 'A', Character.MAX_VALUE };
this.factory.<Character[]>createTester().test(objectArray, Assert::assertArrayEquals);
this.factory.<Character[][]>createTester().test(new Character[][] { objectArray, objectArray }, Assert::assertArrayEquals);
this.factory.<Object[][]>createTester().test(new Object[][] { objectArray, objectArray }, Assert::assertArrayEquals);
}
@Test
public void testObjectArray() throws IOException {
String string1 = "foo";
String string2 = "bar";
String[] stringArray = new String[] { string1, string2 };
this.factory.<String[]>createTester().test(stringArray, Assert::assertArrayEquals);
// Test array with shared object references
this.factory.<String[]>createTester().test(new String[] { string1, string1 }, Assert::assertArrayEquals);
this.factory.<String[][]>createTester().test(new String[][] { stringArray, stringArray }, Assert::assertArrayEquals);
this.factory.<Object[][]>createTester().test(new Object[][] { stringArray, stringArray }, Assert::assertArrayEquals);
}
@Test
public void testNull() throws IOException {
this.factory.createTester().test(null, Assert::assertSame);
}
@Test
public void testClass() throws IOException {
Tester<Class<?>> tester = this.factory.createTester();
tester.test(Object.class, Assert::assertSame);
tester.test(Integer.class, Assert::assertSame);
tester.test(Throwable.class, Assert::assertSame);
tester.test(Exception.class, Assert::assertSame);
}
@Test
public void testException() throws IOException {
try {
try {
try {
throw new Error("foo");
} catch (Throwable e) {
throw new RuntimeException("bar", e);
}
} catch (Throwable e) {
throw new Exception(e);
}
} catch (Throwable e) {
this.factory.<Throwable>createTester().test(e, AbstractLangTestCase::assertEquals);
}
}
@Test
public void testProxy() throws IOException {
Object proxy = Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { Iterable.class }, new TestInvocationHandler("foo"));
this.factory.createTester().test(proxy, AbstractLangTestCase::assertProxyEquals);
}
private static void assertProxyEquals(Object expected, Object actual) {
Assert.assertTrue(Proxy.isProxyClass(actual.getClass()));
TestInvocationHandler actualHandler = (TestInvocationHandler) Proxy.getInvocationHandler(actual);
TestInvocationHandler expectedHandler = (TestInvocationHandler) Proxy.getInvocationHandler(expected);
Assert.assertEquals(expectedHandler.getValue(), actualHandler.getValue());
}
private static void assertArrayEquals(float[] expected, float[] actual) {
Assert.assertArrayEquals(expected, actual, 0);
}
private static void assertArrayEquals(double[] expected, double[] actual) {
Assert.assertArrayEquals(expected, actual, 0);
}
private static void assertEquals(Throwable expected, Throwable actual) {
Assert.assertEquals(expected.getMessage(), actual.getMessage());
StackTraceElement[] expectedStackTrace = expected.getStackTrace();
StackTraceElement[] actualStackTrace = expected.getStackTrace();
// Java 9 adds other fields to stack trace, for which normal equality checks will fail
Assert.assertEquals(expectedStackTrace.length, actualStackTrace.length);
for (int i = 0; i < expectedStackTrace.length; ++i) {
StackTraceElement expectedElement = expectedStackTrace[i];
StackTraceElement actualElement = actualStackTrace[i];
Assert.assertEquals(expectedElement.getClassName(), actualElement.getClassName());
Assert.assertEquals(expectedElement.getMethodName(), actualElement.getMethodName());
Assert.assertEquals(expectedElement.getFileName(), actualElement.getFileName());
Assert.assertEquals(expectedElement.getLineNumber(), actualElement.getLineNumber());
}
Throwable[] expectedSuppressed = expected.getSuppressed();
Throwable[] actualSuppressed = actual.getSuppressed();
Assert.assertEquals(expectedSuppressed.length, actualSuppressed.length);
for (int i = 0; i < expectedSuppressed.length; ++i) {
assertEquals(expectedSuppressed[i], actualSuppressed[i]);
}
Throwable cause1 = expected.getCause();
Throwable cause2 = actual.getCause();
if ((cause1 != null) && (cause2 != null)) {
assertEquals(cause1, cause2);
} else {
Assert.assertSame(cause1, cause2);
}
}
}
| 14,550
| 46.397394
| 163
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/Person.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;
import java.util.Set;
import java.util.TreeSet;
/**
* @author Paul Ferraro
*/
public class Person implements Comparable<Person>, java.io.Serializable {
private static final long serialVersionUID = 7478927571966290859L;
private volatile String name;
private volatile Person parent;
private final Set<Person> children = new TreeSet<>();
public static Person create(String name) {
Person person = new Person();
person.setName(name);
return person;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void addChild(Person child) {
this.children.add(child);
child.parent = this;
}
public Person getParent() {
return this.parent;
}
public Iterable<Person> getChildren() {
return this.children;
}
@Override
public int hashCode() {
return this.name.hashCode();
}
@Override
public boolean equals(Object object) {
if (!(object instanceof Person)) return false;
return this.name.equals(((Person) object).name);
}
@Override
public String toString() {
return this.name;
}
@Override
public int compareTo(Person person) {
return this.name.compareTo(person.name);
}
}
| 2,417
| 27.116279
| 73
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/TestInvocationHandler.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;
import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
/**
* @author Paul Ferraro
*/
public class TestInvocationHandler implements InvocationHandler, Serializable {
private static final long serialVersionUID = 1903022476673232647L;
private final Object value;
public TestInvocationHandler(Object value) {
this.value = value;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return null;
}
public Object getValue() {
return this.value;
}
}
| 1,673
| 32.48
| 87
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/Tester.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;
import java.io.IOException;
import java.util.function.BiConsumer;
import org.junit.Assert;
/**
* Generic interface for various marshalling testers.
* @author Paul Ferraro
*/
public interface Tester<T> {
default void test(T subject) throws IOException {
this.test(subject, Assert::assertEquals);
}
/**
* Same as {@link #test(Object)}, but additionally validates equality of hash code.
* @param subject a test subject
* @throws IOException if marshalling of the test subject fails
*/
default void testKey(T subject) throws IOException {
this.test(subject, (value1, value2) -> {
Assert.assertEquals(value1, value2);
Assert.assertEquals(value1.hashCode(), value2.hashCode());
});
}
void test(T subject, BiConsumer<T, T> assertion) throws IOException;
}
| 1,918
| 34.537037
| 87
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/AbstractTimeTestCase.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;
import java.io.IOException;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.MonthDay;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.Period;
import java.time.Year;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Random;
import org.junit.Test;
/**
* Generic tests for java.time.* classes.
* @author Paul Ferraro
*/
public abstract class AbstractTimeTestCase {
private final MarshallingTesterFactory factory;
public AbstractTimeTestCase(MarshallingTesterFactory factory) {
this.factory = factory;
}
@Test
public void testDayOfWeek() throws IOException {
this.factory.createTester(DayOfWeek.class).test();
}
@Test
public void testDuration() throws IOException {
MarshallingTester<Duration> tester = this.factory.createTester();
tester.test(Duration.between(Instant.EPOCH, Instant.now()));
tester.test(Duration.ofMillis(1234567890));
tester.test(Duration.ofSeconds(100));
tester.test(Duration.ZERO);
}
@Test
public void testInstant() throws IOException {
MarshallingTester<Instant> tester = this.factory.createTester();
tester.test(Instant.MAX);
tester.test(Instant.MIN);
tester.test(Instant.now());
tester.test(Instant.ofEpochMilli(System.currentTimeMillis()));
}
@Test
public void testLocalDate() throws IOException {
MarshallingTester<LocalDate> tester = this.factory.createTester();
tester.test(LocalDate.MAX);
tester.test(LocalDate.MIN);
tester.test(LocalDate.now());
tester.test(LocalDate.ofEpochDay(0));
}
@Test
public void testLocalDateTime() throws IOException {
MarshallingTester<LocalDateTime> tester = this.factory.createTester();
tester.test(LocalDateTime.MAX);
tester.test(LocalDateTime.MIN);
tester.test(LocalDateTime.now());
tester.test(LocalDateTime.of(LocalDate.now(), LocalTime.of(23, 59, 59)));
tester.test(LocalDateTime.of(LocalDate.now(), LocalTime.of(23, 59)));
tester.test(LocalDateTime.of(LocalDate.now(), LocalTime.of(23, 0)));
tester.test(LocalDateTime.of(LocalDate.now(), LocalTime.MIDNIGHT));
}
@Test
public void testLocalTime() throws IOException {
MarshallingTester<LocalTime> tester = this.factory.createTester();
tester.test(LocalTime.MAX);
tester.test(LocalTime.MIN);
tester.test(LocalTime.now());
tester.test(LocalTime.of(23, 59, 59));
tester.test(LocalTime.of(23, 59));
tester.test(LocalTime.of(23, 0));
}
@Test
public void testMonth() throws IOException {
this.factory.createTester(Month.class).test();
}
@Test
public void testMonthDay() throws IOException {
MarshallingTester<MonthDay> tester = this.factory.createTester();
tester.test(MonthDay.now());
}
@Test
public void testOffsetDateTime() throws IOException {
MarshallingTester<OffsetDateTime> tester = this.factory.createTester();
tester.test(OffsetDateTime.MAX);
tester.test(OffsetDateTime.MIN);
tester.test(OffsetDateTime.now(ZoneOffset.UTC));
tester.test(OffsetDateTime.now(ZoneOffset.MIN));
tester.test(OffsetDateTime.now(ZoneOffset.MAX));
}
@Test
public void testOffsetTime() throws IOException {
MarshallingTester<OffsetTime> tester = this.factory.createTester();
tester.test(OffsetTime.MAX);
tester.test(OffsetTime.MIN);
tester.test(OffsetTime.now(ZoneOffset.UTC));
tester.test(OffsetTime.now(ZoneOffset.MIN));
tester.test(OffsetTime.now(ZoneOffset.MAX));
}
@Test
public void testZonedDateTime() throws IOException {
MarshallingTester<ZonedDateTime> tester = this.factory.createTester();
tester.test(ZonedDateTime.now(ZoneOffset.UTC));
tester.test(ZonedDateTime.now(ZoneOffset.MIN));
tester.test(ZonedDateTime.now(ZoneOffset.MAX));
tester.test(ZonedDateTime.now(ZoneId.of("America/New_York")));
}
@Test
public void testPeriod() throws IOException {
MarshallingTester<Period> tester = this.factory.createTester();
tester.test(Period.between(LocalDate.ofEpochDay(0), LocalDate.now()));
tester.test(Period.ofMonths(100));
tester.test(Period.ofYears(100));
tester.test(Period.ZERO);
}
@Test
public void testYear() throws IOException {
MarshallingTester<Year> tester = this.factory.createTester();
tester.test(Year.of(Year.MAX_VALUE));
tester.test(Year.of(Year.MIN_VALUE));
tester.test(Year.now());
tester.test(Year.of(Instant.EPOCH.atOffset(ZoneOffset.UTC).getYear()));
}
@Test
public void testYearMonth() throws IOException {
MarshallingTester<YearMonth> tester = this.factory.createTester();
tester.test(YearMonth.of(Year.MAX_VALUE, Month.DECEMBER));
tester.test(YearMonth.of(Year.MIN_VALUE, Month.JANUARY));
tester.test(YearMonth.now());
tester.test(YearMonth.of(Instant.EPOCH.atOffset(ZoneOffset.UTC).getYear(), Instant.EPOCH.atOffset(ZoneOffset.UTC).getMonth()));
}
@Test
public void testZoneId() throws IOException {
MarshallingTester<ZoneId> tester = this.factory.createTester();
tester.test(ZoneId.of("America/New_York"));
}
@Test
public void testZoneOffset() throws IOException {
MarshallingTester<ZoneOffset> tester = this.factory.createTester();
tester.test(ZoneOffset.MIN);
tester.test(ZoneOffset.MAX);
tester.test(ZoneOffset.of("-10")); // Hawaii Standard Time
tester.test(ZoneOffset.of("+12:45")); // New Zealand's Chatham Islands
Random random = new Random(System.currentTimeMillis());
tester.test(ZoneOffset.ofHoursMinutesSeconds(random.nextInt(18), random.nextInt(60), random.nextInt(60)));
tester.test(ZoneOffset.ofHoursMinutesSeconds(0 - random.nextInt(18), 0 - random.nextInt(60), 0 - random.nextInt(60)));
tester.test(ZoneOffset.UTC);
}
}
| 7,450
| 36.442211
| 135
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/AbstractCircularReferenceTestCase.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;
import java.io.IOException;
import org.junit.Assert;
/**
* @author Paul Ferraro
*/
public abstract class AbstractCircularReferenceTestCase {
private final MarshallingTesterFactory factory;
public AbstractCircularReferenceTestCase(MarshallingTesterFactory factory) {
this.factory = factory;
}
@org.junit.Test
public void test() throws IOException {
Person parent = Person.create("parent");
Person self = Person.create("self");
parent.addChild(self);
parent.addChild(Person.create("sibling"));
self.addChild(Person.create("son"));
self.addChild(Person.create("daughter"));
Tester<Person> tester = this.factory.createTester();
tester.test(self, (expected, actual) -> {
Assert.assertEquals(expected, actual);
Assert.assertEquals(expected.getParent(), actual.getParent());
Assert.assertEquals(expected.getChildren(), actual.getChildren());
// Validate referential integrity
for (Person child : actual.getParent().getChildren()) {
Assert.assertSame(actual.getParent(), child.getParent());
}
for (Person child : actual.getChildren()) {
Assert.assertSame(actual, child.getParent());
}
});
}
}
| 2,394
| 36.421875
| 80
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/TestMarshaller.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;
import java.io.IOException;
import java.nio.ByteBuffer;
/**
* Encapsulates the marshalling of an object.
* This allows us to run a set of marshalling tests across different marshallers.
* @author Paul Ferraro
*/
public interface TestMarshaller<T> {
T read(ByteBuffer buffer) throws IOException;
ByteBuffer write(T object) throws IOException;
}
| 1,428
| 35.641026
| 81
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/TestComparatorExternalizer.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;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import org.kohsuke.MetaInfServices;
/**
* @author Paul Ferraro
*/
@MetaInfServices(Externalizer.class)
public class TestComparatorExternalizer implements Externalizer<TestComparator<Object>> {
@Override
public void writeObject(ObjectOutput output, TestComparator<Object> object) throws IOException {
}
@Override
public TestComparator<Object> readObject(ObjectInput input) throws IOException, ClassNotFoundException {
return new TestComparator<>();
}
@SuppressWarnings("unchecked")
@Override
public Class<TestComparator<Object>> getTargetClass() {
return (Class<TestComparator<Object>>) (Class<?>) TestComparator.class;
}
}
| 1,840
| 34.403846
| 108
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/AbstractUtilTestCase.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;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.AbstractMap;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Currency;
import java.util.Date;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
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.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
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 java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Assert;
import org.junit.Test;
/**
* Generic tests for java.util.* classes.
* @author Paul Ferraro
*/
public abstract class AbstractUtilTestCase {
private static final Map<Object, Object> BASIS = Stream.of(1, 2, 3, 4, 5).collect(Collectors.toMap(i -> i, i -> Integer.toString(-i)));
private final MarshallingTesterFactory factory;
public AbstractUtilTestCase(MarshallingTesterFactory factory) {
this.factory = factory;
}
@Test
public void testArrayDeque() throws IOException {
MarshallingTester<ArrayDeque<Object>> tester = this.factory.createTester();
tester.test(new ArrayDeque<>(BASIS.keySet()), AbstractUtilTestCase::assertCollectionEquals);
}
@Test
public void testArrayList() throws IOException {
MarshallingTester<ArrayList<Object>> tester = this.factory.createTester();
tester.test(new ArrayList<>(BASIS.keySet()), AbstractUtilTestCase::assertCollectionEquals);
}
@Test
public void testBitSet() throws IOException {
MarshallingTester<BitSet> tester = this.factory.createTester();
tester.test(new BitSet(0));
BitSet set = new BitSet(7);
set.set(1);
set.set(3);
set.set(5);
tester.test(set);
}
@Test
public void testCalendar() throws IOException {
MarshallingTester<Calendar> tester = this.factory.createTester();
LocalDateTime time = LocalDateTime.now();
// Validate default calendar w/date only
tester.test(new Calendar.Builder().setDate(time.getYear(), time.getMonthValue(), time.getDayOfMonth()).build());
// Validate Gregorian calendar w/locale and date + time
tester.test(new Calendar.Builder().setLenient(false).setLocale(Locale.FRANCE).setDate(time.getYear(), time.getMonthValue() - 1, time.getDayOfMonth()).setTimeOfDay(time.getHour(), time.getMinute(), time.getSecond()).build());
// Validate Japanese Imperial calendar w/full date/time
tester.test(new Calendar.Builder().setLocale(Locale.JAPAN).setTimeZone(TimeZone.getTimeZone("Asia/Tokyo")).setInstant(Date.from(time.toInstant(ZoneOffset.UTC))).build());
// Validate Buddhist calendar
tester.test(new Calendar.Builder().setLocale(Locale.forLanguageTag("th_TH")).setTimeZone(TimeZone.getTimeZone("Asia/Bangkok")).build());
}
@Test
public void testCurrency() throws IOException {
MarshallingTester<Currency> tester = this.factory.createTester();
tester.test(Currency.getInstance(Locale.getDefault()));
tester.test(Currency.getInstance(Locale.UK));
}
@Test
public void testDate() throws IOException {
MarshallingTester<Date> tester = this.factory.createTester();
tester.test(Date.from(Instant.EPOCH));
tester.test(Date.from(Instant.now()));
}
@Test
public void testEnumMap() throws IOException {
MarshallingTester<EnumMap<Thread.State, String>> tester = this.factory.createTester();
EnumMap<Thread.State, String> map = new EnumMap<>(Thread.State.class);
tester.test(map, AbstractUtilTestCase::assertMapEquals);
for (Thread.State state : EnumSet.allOf(Thread.State.class)) {
map.put(state, ((state.ordinal() % 2) == 0) ? state.name() : null);
tester.test(map, AbstractUtilTestCase::assertMapEquals);
}
}
@Test
public void testEnumSet() throws IOException {
MarshallingTester<EnumSet<Thread.State>> tester = this.factory.createTester();
EnumSet<Thread.State> set = EnumSet.noneOf(Thread.State.class);
tester.test(set, AbstractUtilTestCase::assertCollectionEquals);
for (Thread.State state : EnumSet.allOf(Thread.State.class)) {
set.add(state);
tester.test(set, AbstractUtilTestCase::assertCollectionEquals);
}
}
@Test
public void testJumboEnumSet() throws IOException {
MarshallingTester<EnumSet<Character.UnicodeScript>> tester = this.factory.createTester();
tester.test(EnumSet.noneOf(Character.UnicodeScript.class), AbstractUtilTestCase::assertCollectionEquals);
tester.test(EnumSet.of(Character.UnicodeScript.UNKNOWN), AbstractUtilTestCase::assertCollectionEquals);
tester.test(EnumSet.of(Character.UnicodeScript.ARABIC, Character.UnicodeScript.ARMENIAN, Character.UnicodeScript.AVESTAN, Character.UnicodeScript.BALINESE, Character.UnicodeScript.BAMUM, Character.UnicodeScript.BATAK, Character.UnicodeScript.BENGALI, Character.UnicodeScript.BOPOMOFO, Character.UnicodeScript.BRAHMI, Character.UnicodeScript.BRAILLE, Character.UnicodeScript.BUGINESE, Character.UnicodeScript.BUHID, Character.UnicodeScript.CANADIAN_ABORIGINAL, Character.UnicodeScript.CARIAN), AbstractUtilTestCase::assertCollectionEquals);
tester.test(EnumSet.complementOf(EnumSet.of(Character.UnicodeScript.UNKNOWN)), AbstractUtilTestCase::assertCollectionEquals);
tester.test(EnumSet.allOf(Character.UnicodeScript.class), AbstractUtilTestCase::assertCollectionEquals);
}
@Test
public void testEmptyEnumSet() throws IOException {
MarshallingTester<EnumSet<Empty>> tester = this.factory.createTester();
EnumSet<Empty> set = EnumSet.noneOf(Empty.class);
tester.test(set, AbstractUtilTestCase::assertCollectionEquals);
}
@Test
public void testHashMap() throws IOException {
MarshallingTester<HashMap<Object, Object>> tester = this.factory.createTester();
tester.test(new HashMap<>(BASIS), AbstractUtilTestCase::assertMapEquals);
}
@Test
public void testHashSet() throws IOException {
MarshallingTester<HashSet<Object>> tester = this.factory.createTester();
tester.test(new HashSet<>(BASIS.keySet()), AbstractUtilTestCase::assertCollectionEquals);
}
@Test
public void testLinkedHashMap() throws IOException {
MarshallingTester<LinkedHashMap<Object, Object>> tester = this.factory.createTester();
tester.test(new LinkedHashMap<>(BASIS), AbstractUtilTestCase::assertLinkedMapEquals);
LinkedHashMap<Object, Object> accessOrderMap = new LinkedHashMap<>(5, 1, true);
accessOrderMap.putAll(BASIS);
tester.test(new LinkedHashMap<>(accessOrderMap), AbstractUtilTestCase::assertLinkedMapEquals);
}
@Test
public void testLinkedHashSet() throws IOException {
MarshallingTester<LinkedHashSet<Object>> tester = this.factory.createTester();
tester.test(new LinkedHashSet<>(BASIS.keySet()), AbstractUtilTestCase::assertCollectionEquals);
}
@Test
public void testLinkedList() throws IOException {
MarshallingTester<LinkedList<Object>> tester = this.factory.createTester();
tester.test(new LinkedList<>(BASIS.keySet()), AbstractUtilTestCase::assertCollectionEquals);
}
@Test
public void testLocale() throws IOException {
MarshallingTester<Locale> tester = this.factory.createTester();
tester.test(Locale.getDefault());
tester.test(Locale.ENGLISH);
tester.test(Locale.CANADA_FRENCH);
}
@Test
public void testOptional() throws IOException {
MarshallingTester<Optional<Object>> tester = this.factory.createTester();
tester.test(Optional.empty());
tester.test(Optional.of("foo"));
}
@Test
public void testOptionalDouble() throws IOException {
MarshallingTester<OptionalDouble> tester = this.factory.createTester();
tester.test(OptionalDouble.empty());
tester.test(OptionalDouble.of(Double.MAX_VALUE));
}
@Test
public void testOptionalInt() throws IOException {
MarshallingTester<OptionalInt> tester = this.factory.createTester();
tester.test(OptionalInt.empty());
tester.test(OptionalInt.of(Integer.MAX_VALUE));
}
@Test
public void testOptionalLong() throws IOException {
MarshallingTester<OptionalLong> tester = this.factory.createTester();
tester.test(OptionalLong.empty());
tester.test(OptionalLong.of(Long.MAX_VALUE));
}
@Test
public void testSimpleEntry() throws IOException {
MarshallingTester<AbstractMap.SimpleEntry<Object, Object>> tester = this.factory.createTester();
String key = "key";
String value = "value";
tester.test(new AbstractMap.SimpleEntry<>(null, null));
tester.test(new AbstractMap.SimpleEntry<>(key, null));
tester.test(new AbstractMap.SimpleEntry<>(key, value));
tester.test(new AbstractMap.SimpleEntry<>(value, value));
}
@Test
public void testSimpleImmutableEntry() throws IOException {
MarshallingTester<AbstractMap.SimpleImmutableEntry<Object, Object>> tester = this.factory.createTester();
String key = "key";
String value = "value";
tester.test(new AbstractMap.SimpleImmutableEntry<>(null, null));
tester.test(new AbstractMap.SimpleImmutableEntry<>(key, null));
tester.test(new AbstractMap.SimpleImmutableEntry<>(key, value));
tester.test(new AbstractMap.SimpleImmutableEntry<>(value, value));
}
@Test
public void testTimeZone() throws IOException {
MarshallingTester<TimeZone> tester = this.factory.createTester();
tester.test(TimeZone.getDefault());
tester.test(TimeZone.getTimeZone("GMT"));
}
@SuppressWarnings("unchecked")
@Test
public void testTreeMap() throws IOException {
MarshallingTester<TreeMap<Object, Object>> tester = this.factory.createTester();
TreeMap<Object, Object> map = new TreeMap<>();
map.putAll(BASIS);
tester.test(map, AbstractUtilTestCase::assertMapEquals);
map = new TreeMap<>((Comparator<Object>) (Comparator<?>) Comparator.reverseOrder());
map.putAll(BASIS);
tester.test(map, AbstractUtilTestCase::assertMapEquals);
map = new TreeMap<>(new TestComparator<>());
map.putAll(BASIS);
tester.test(map, AbstractUtilTestCase::assertMapEquals);
}
@SuppressWarnings("unchecked")
@Test
public void testTreeSet() throws IOException {
MarshallingTester<TreeSet<Object>> tester = this.factory.createTester();
TreeSet<Object> set = new TreeSet<>();
set.addAll(BASIS.keySet());
tester.test(set, AbstractUtilTestCase::assertCollectionEquals);
set = new TreeSet<>((Comparator<Object>) (Comparator<?>) Comparator.reverseOrder());
set.addAll(BASIS.keySet());
tester.test(set, AbstractUtilTestCase::assertCollectionEquals);
set = new TreeSet<>(new TestComparator<>());
set.addAll(BASIS.keySet());
tester.test(set, AbstractUtilTestCase::assertCollectionEquals);
}
@Test
public void testUUID() throws IOException {
MarshallingTester<UUID> tester = this.factory.createTester();
tester.test(UUID.randomUUID());
}
// java.util.Collections.emptyXXX() methods
@Test
public void testEmptyList() throws IOException {
MarshallingTester<List<Object>> tester = this.factory.createTester();
tester.test(Collections.emptyList(), Assert::assertSame);
}
@Test
public void testEmptyMap() throws IOException {
MarshallingTester<Map<Object, Object>> tester = this.factory.createTester();
tester.test(Collections.emptyMap(), Assert::assertSame);
}
@Test
public void testEmptyNavigableMap() throws IOException {
MarshallingTester<NavigableMap<Object, Object>> tester = this.factory.createTester();
tester.test(Collections.emptyNavigableMap(), Assert::assertSame);
}
@Test
public void testEmptyNavigableSet() throws IOException {
MarshallingTester<NavigableSet<Object>> tester = this.factory.createTester();
tester.test(Collections.emptyNavigableSet(), Assert::assertSame);
}
@Test
public void testEmptySet() throws IOException {
MarshallingTester<Set<Object>> tester = this.factory.createTester();
tester.test(Collections.emptySet(), Assert::assertSame);
}
@Test
public void testEmptySortedMap() throws IOException {
MarshallingTester<SortedMap<Object, Object>> tester = this.factory.createTester();
tester.test(Collections.emptySortedMap(), Assert::assertSame);
}
@Test
public void testEmptySortedSet() throws IOException {
MarshallingTester<SortedSet<Object>> tester = this.factory.createTester();
tester.test(Collections.emptySortedSet(), Assert::assertSame);
}
// java.util.Collections.synchronizedXXX(...) methods
@Test
public void testSynchronizedCollection() throws IOException {
MarshallingTester<Collection<Object>> tester = this.factory.createTester();
tester.test(Collections.synchronizedCollection(new LinkedList<>(BASIS.keySet())), AbstractUtilTestCase::assertCollectionEquals);
}
@Test
public void testSynchronizedList() throws IOException {
MarshallingTester<List<Object>> tester = this.factory.createTester();
tester.test(Collections.synchronizedList(new LinkedList<>(BASIS.keySet())), AbstractUtilTestCase::assertCollectionEquals);
}
@Test
public void testSynchronizedMap() throws IOException {
MarshallingTester<Map<Object, Object>> tester = this.factory.createTester();
tester.test(Collections.synchronizedMap(new HashMap<>(BASIS)), AbstractUtilTestCase::assertMapEquals);
}
@Test
public void testSynchronizedNavigableMap() throws IOException {
MarshallingTester<Map<Object, Object>> tester = this.factory.createTester();
TreeMap<Object, Object> map = new TreeMap<>();
map.putAll(BASIS);
tester.test(Collections.synchronizedNavigableMap(map), AbstractUtilTestCase::assertMapEquals);
}
@Test
public void testSynchronizedNavigableSet() throws IOException {
MarshallingTester<Set<Object>> tester = this.factory.createTester();
TreeSet<Object> set = new TreeSet<>();
set.addAll(BASIS.keySet());
tester.test(Collections.synchronizedNavigableSet(set), AbstractUtilTestCase::assertCollectionEquals);
}
@Test
public void testSynchronizedRandomAccessList() throws IOException {
MarshallingTester<List<Object>> tester = this.factory.createTester();
tester.test(Collections.synchronizedList(new ArrayList<>(BASIS.keySet())), AbstractUtilTestCase::assertCollectionEquals);
}
@Test
public void testSynchronizedSet() throws IOException {
MarshallingTester<Set<Object>> tester = this.factory.createTester();
tester.test(Collections.synchronizedSet(new HashSet<>(BASIS.keySet())), AbstractUtilTestCase::assertCollectionEquals);
}
@Test
public void testSynchronizedSortedMap() throws IOException {
MarshallingTester<Map<Object, Object>> tester = this.factory.createTester();
TreeMap<Object, Object> map = new TreeMap<>();
map.putAll(BASIS);
tester.test(Collections.synchronizedSortedMap(map), AbstractUtilTestCase::assertMapEquals);
}
@Test
public void testSynchronizedSortedSet() throws IOException {
MarshallingTester<Set<Object>> tester = this.factory.createTester();
TreeSet<Object> set = new TreeSet<>();
set.addAll(BASIS.keySet());
tester.test(Collections.synchronizedSortedSet(set), AbstractUtilTestCase::assertCollectionEquals);
}
// java.util.Collections.singletonXXX(...) methods
@Test
public void testSingletonList() throws IOException {
MarshallingTester<List<Object>> tester = this.factory.createTester();
tester.test(Collections.singletonList(null), AbstractUtilTestCase::assertCollectionEquals);
tester.test(Collections.singletonList("foo"), AbstractUtilTestCase::assertCollectionEquals);
}
@Test
public void testSingletonMap() throws IOException {
MarshallingTester<Map<Object, Object>> tester = this.factory.createTester();
tester.test(Collections.singletonMap(null, null), AbstractUtilTestCase::assertMapEquals);
tester.test(Collections.singletonMap("foo", null), AbstractUtilTestCase::assertMapEquals);
tester.test(Collections.singletonMap("foo", "bar"), AbstractUtilTestCase::assertMapEquals);
}
@Test
public void testSingletonSet() throws IOException {
MarshallingTester<Set<Object>> tester = this.factory.createTester();
tester.test(Collections.singleton(null), AbstractUtilTestCase::assertCollectionEquals);
tester.test(Collections.singleton("foo"), AbstractUtilTestCase::assertCollectionEquals);
}
// java.util.Collections.unmodifiableXXX(...) methods
@Test
public void testUnmodifiableCollection() throws IOException {
MarshallingTester<Collection<Object>> tester = this.factory.createTester();
tester.test(Collections.synchronizedCollection(new LinkedList<>(BASIS.keySet())), AbstractUtilTestCase::assertCollectionEquals);
}
@Test
public void testUnmodifiableList() throws IOException {
MarshallingTester<List<Object>> tester = this.factory.createTester();
tester.test(Collections.synchronizedList(new LinkedList<>(BASIS.keySet())), AbstractUtilTestCase::assertCollectionEquals);
tester.test(List.of(), AbstractUtilTestCase::assertCollectionEquals);
tester.test(List.of(0), AbstractUtilTestCase::assertCollectionEquals);
tester.test(List.of(0, 1), AbstractUtilTestCase::assertCollectionEquals);
tester.test(List.of(0, 1, 2, 3), AbstractUtilTestCase::assertCollectionEquals);
tester.test(List.of(0, 1, 2, 3, 4), AbstractUtilTestCase::assertCollectionEquals);
tester.test(List.of(0, 1, 2, 3, 4, 5), AbstractUtilTestCase::assertCollectionEquals);
tester.test(List.of(0, 1, 2, 3, 4, 5, 6), AbstractUtilTestCase::assertCollectionEquals);
tester.test(List.of(0, 1, 2, 3, 4, 5, 6, 7), AbstractUtilTestCase::assertCollectionEquals);
tester.test(List.of(0, 1, 2, 3, 4, 5, 6, 7), AbstractUtilTestCase::assertCollectionEquals);
tester.test(List.of(0, 1, 2, 3, 4, 5, 6, 7, 8), AbstractUtilTestCase::assertCollectionEquals);
tester.test(List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), AbstractUtilTestCase::assertCollectionEquals);
tester.test(List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), AbstractUtilTestCase::assertCollectionEquals);
}
@Test
public void testUnmodifiableMap() throws IOException {
MarshallingTester<Map<Object, Object>> tester = this.factory.createTester();
tester.test(Collections.synchronizedMap(new HashMap<>(BASIS)), AbstractUtilTestCase::assertMapEquals);
tester.test(Map.of(), AbstractUtilTestCase::assertMapEquals);
tester.test(Map.of(0, "0"), AbstractUtilTestCase::assertMapEquals);
tester.test(Map.of(0, "0", 1, "1"), AbstractUtilTestCase::assertMapEquals);
tester.test(Map.of(0, "0", 1, "1", 2, "2"), AbstractUtilTestCase::assertMapEquals);
tester.test(Map.of(0, "0", 1, "1", 2, "2", 3, "3"), AbstractUtilTestCase::assertMapEquals);
tester.test(Map.of(0, "0", 1, "1", 2, "2", 3, "3", 4, "4"), AbstractUtilTestCase::assertMapEquals);
tester.test(Map.of(0, "0", 1, "1", 2, "2", 3, "3", 4, "4", 5, "5"), AbstractUtilTestCase::assertMapEquals);
tester.test(Map.of(0, "0", 1, "1", 2, "2", 3, "3", 4, "4", 5, "5", 6, "6"), AbstractUtilTestCase::assertMapEquals);
tester.test(Map.of(0, "0", 1, "1", 2, "2", 3, "3", 4, "4", 5, "5", 6, "6", 7, "7"), AbstractUtilTestCase::assertMapEquals);
tester.test(Map.of(0, "0", 1, "1", 2, "2", 3, "3", 4, "4", 5, "5", 6, "6", 7, "7", 8, "8"), AbstractUtilTestCase::assertMapEquals);
tester.test(Map.of(0, "0", 1, "1", 2, "2", 3, "3", 4, "4", 5, "5", 6, "6", 7, "7", 8, "8", 9, "9"), AbstractUtilTestCase::assertMapEquals);
tester.test(Map.ofEntries(Map.entry(0, "1")), AbstractUtilTestCase::assertMapEquals);
tester.test(Map.ofEntries(Map.entry(0, "0"), Map.entry(1, "1")), AbstractUtilTestCase::assertMapEquals);
tester.test(Map.ofEntries(Map.entry(0, "0"), Map.entry(1, "1"), Map.entry(2, "2")), AbstractUtilTestCase::assertMapEquals);
}
@Test
public void testUnmodifiableNavigableMap() throws IOException {
MarshallingTester<Map<Object, Object>> tester = this.factory.createTester();
TreeMap<Object, Object> map = new TreeMap<>();
map.putAll(BASIS);
tester.test(Collections.synchronizedNavigableMap(map), AbstractUtilTestCase::assertMapEquals);
}
@Test
public void testUnmodifiableNavigableSet() throws IOException {
MarshallingTester<Set<Object>> tester = this.factory.createTester();
TreeSet<Object> set = new TreeSet<>();
set.addAll(BASIS.keySet());
tester.test(Collections.synchronizedNavigableSet(set), AbstractUtilTestCase::assertCollectionEquals);
}
@Test
public void testUnmodifiableRandomAccessList() throws IOException {
MarshallingTester<List<Object>> tester = this.factory.createTester();
tester.test(Collections.synchronizedList(new ArrayList<>(BASIS.keySet())), AbstractUtilTestCase::assertCollectionEquals);
}
@Test
public void testUnmodifiableSet() throws IOException {
MarshallingTester<Set<Object>> tester = this.factory.createTester();
tester.test(Collections.synchronizedSet(new HashSet<>(BASIS.keySet())), AbstractUtilTestCase::assertCollectionEquals);
tester.test(Set.of(), AbstractUtilTestCase::assertCollectionEquals);
tester.test(Set.of(0), AbstractUtilTestCase::assertCollectionEquals);
tester.test(Set.of(0, 1), AbstractUtilTestCase::assertCollectionEquals);
tester.test(Set.of(0, 1, 2, 3), AbstractUtilTestCase::assertCollectionEquals);
tester.test(Set.of(0, 1, 2, 3, 4), AbstractUtilTestCase::assertCollectionEquals);
tester.test(Set.of(0, 1, 2, 3, 4, 5), AbstractUtilTestCase::assertCollectionEquals);
tester.test(Set.of(0, 1, 2, 3, 4, 5, 6), AbstractUtilTestCase::assertCollectionEquals);
tester.test(Set.of(0, 1, 2, 3, 4, 5, 6, 7), AbstractUtilTestCase::assertCollectionEquals);
tester.test(Set.of(0, 1, 2, 3, 4, 5, 6, 7), AbstractUtilTestCase::assertCollectionEquals);
tester.test(Set.of(0, 1, 2, 3, 4, 5, 6, 7, 8), AbstractUtilTestCase::assertCollectionEquals);
tester.test(Set.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9), AbstractUtilTestCase::assertCollectionEquals);
tester.test(Set.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), AbstractUtilTestCase::assertCollectionEquals);
}
@Test
public void testUnmodifiableSortedMap() throws IOException {
MarshallingTester<Map<Object, Object>> tester = this.factory.createTester();
TreeMap<Object, Object> map = new TreeMap<>();
map.putAll(BASIS);
tester.test(Collections.synchronizedSortedMap(map), AbstractUtilTestCase::assertMapEquals);
}
@Test
public void testUnmodifiableSortedSet() throws IOException {
MarshallingTester<Set<Object>> tester = this.factory.createTester();
TreeSet<Object> set = new TreeSet<>();
set.addAll(BASIS.keySet());
tester.test(Collections.synchronizedSortedSet(set), AbstractUtilTestCase::assertCollectionEquals);
}
static <T extends Collection<?>> void assertCollectionEquals(T expected, T actual) {
Assert.assertSame(expected.getClass(), actual.getClass());
Assert.assertEquals(expected.size(), actual.size());
Assert.assertTrue(expected.containsAll(actual));
}
static <T extends Map<?, ?>> void assertMapEquals(T expected, T actual) {
Assert.assertSame(expected.getClass(), actual.getClass());
Assert.assertEquals(expected.size(), actual.size());
Assert.assertTrue(actual.keySet().toString(), expected.keySet().containsAll(actual.keySet()));
for (Map.Entry<?, ?> entry : expected.entrySet()) {
Assert.assertEquals(entry.getValue(), actual.get(entry.getKey()));
}
}
static <T extends Map<?, ?>> void assertLinkedMapEquals(T expected, T actual) {
Assert.assertSame(expected.getClass(), actual.getClass());
Assert.assertEquals(expected.size(), actual.size());
// Change access order
expected.get(expected.keySet().iterator().next());
actual.get(actual.keySet().iterator().next());
@SuppressWarnings("unchecked")
Iterator<Map.Entry<?, ?>> expectedEntries = (Iterator<Map.Entry<?, ?>>) (Iterator<?>) expected.entrySet().iterator();
@SuppressWarnings("unchecked")
Iterator<Map.Entry<?, ?>> actualEntries = (Iterator<Map.Entry<?, ?>>) (Iterator<?>) actual.entrySet().iterator();
while (expectedEntries.hasNext() && actualEntries.hasNext()) {
Map.Entry<?, ?> expectedEntry = expectedEntries.next();
Map.Entry<?, ?> actualEntry = actualEntries.next();
Assert.assertEquals(expectedEntry.getKey(), actualEntry.getKey());
Assert.assertEquals(expectedEntry.getValue(), actualEntry.getValue());
}
}
}
| 27,356
| 46.167241
| 547
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/MarshallingTesterFactory.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;
/**
* Factory for creating marshalling testers.
* @author Paul Ferraro
*/
public interface MarshallingTesterFactory {
<T> MarshallingTester<T> createTester();
default <E extends Enum<E>> EnumMarshallingTester<E> createTester(Class<E> enumClass) {
return new EnumMarshallingTester<>(enumClass, this.createTester());
}
}
| 1,412
| 38.25
| 91
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/Empty.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;
/**
* A test enum with no values.
* @author Paul Ferraro
*/
public enum Empty {
}
| 1,155
| 35.125
| 70
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/AbstractNetTestCase.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;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URL;
import org.junit.Test;
/**
* Generic tests for java.net.* classes.
* @author Paul Ferraro
*/
public abstract class AbstractNetTestCase {
private final MarshallingTesterFactory factory;
public AbstractNetTestCase(MarshallingTesterFactory factory) {
this.factory = factory;
}
@Test
public void testURI() throws IOException {
MarshallingTester<URI> tester = this.factory.createTester();
tester.test(URI.create("http://wildfly.org/news/"));
}
@Test
public void testURL() throws IOException {
MarshallingTester<URL> tester = this.factory.createTester();
tester.test(new URL("http://wildfly.org/news/"));
}
@Test
public void testInetAddress() throws IOException {
MarshallingTester<InetAddress> tester = this.factory.createTester();
tester.test(InetAddress.getLoopbackAddress());
tester.test(InetAddress.getLocalHost());
tester.test(InetAddress.getByName("127.0.0.1"));
tester.test(InetAddress.getByName("::1"));
tester.test(InetAddress.getByName("0.0.0.0"));
tester.test(InetAddress.getByName("::"));
}
@Test
public void testInetSocketAddress() throws IOException {
MarshallingTester<InetSocketAddress> tester = this.factory.createTester();
tester.test(InetSocketAddress.createUnresolved("foo.bar", 0));
tester.test(InetSocketAddress.createUnresolved("foo.bar", Short.MAX_VALUE));
tester.test(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
tester.test(new InetSocketAddress(InetAddress.getLoopbackAddress(), Short.MAX_VALUE));
tester.test(new InetSocketAddress(InetAddress.getLocalHost(), 0));
tester.test(new InetSocketAddress(InetAddress.getLocalHost(), Short.MAX_VALUE));
tester.test(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0));
tester.test(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), Short.MAX_VALUE));
tester.test(new InetSocketAddress(InetAddress.getByName("::"), 0));
tester.test(new InetSocketAddress(InetAddress.getByName("::"), Short.MAX_VALUE));
}
}
| 3,356
| 39.445783
| 94
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/ExternalizerMarshaller.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;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Modifier;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* Marshaller using Java serialization via an externalizer.
* @author Paul Ferraro
*/
public class ExternalizerMarshaller<T> implements TestMarshaller<T> {
private final Map<Class<?>, Externalizer<?>> externalizers;
private Externalizer<T> currentExternalizer;
ExternalizerMarshaller(Externalizer<T> externalizer) {
this.externalizers = Collections.singletonMap(externalizer.getTargetClass(), externalizer);
}
ExternalizerMarshaller(Iterable<? extends Externalizer<?>> externalizers) {
this.externalizers = new HashMap<>();
for (Externalizer<?> externalizer : externalizers) {
this.externalizers.put(externalizer.getTargetClass(), externalizer);
}
}
@Override
public T read(ByteBuffer buffer) throws IOException {
try (ObjectInputStream input = new ObjectInputStream(new ByteArrayInputStream(buffer.array(), buffer.arrayOffset(), buffer.limit() - buffer.arrayOffset()))) {
return this.currentExternalizer.readObject(input);
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
}
}
@SuppressWarnings("unchecked")
@Override
public ByteBuffer write(T object) throws IOException {
Class<?> targetClass = object.getClass().isEnum() ? ((Enum<?>) object).getDeclaringClass() : object.getClass();
Class<?> superClass = targetClass.getSuperclass();
// If implementation class has no externalizer, search any abstract superclasses
while (!this.externalizers.containsKey(targetClass) && (superClass != null) && Modifier.isAbstract(superClass.getModifiers())) {
targetClass = superClass;
superClass = targetClass.getSuperclass();
}
this.currentExternalizer = (Externalizer<T>) this.externalizers.get(targetClass);
ByteArrayOutputStream externalizedOutput = new ByteArrayOutputStream();
try (ObjectOutputStream output = new ObjectOutputStream(externalizedOutput)) {
this.currentExternalizer.writeObject(output, object);
}
return ByteBuffer.wrap(externalizedOutput.toByteArray());
}
}
| 3,545
| 41.722892
| 166
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/AbstractConcurrentTestCase.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;
import java.io.IOException;
import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
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 java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Assert;
import org.junit.Test;
/**
* Generic tests for java.util.concurrent.* classes.
* @author Paul Ferraro
*/
public abstract class AbstractConcurrentTestCase {
private static final Map<Object, Object> BASIS = Stream.of(1, 2, 3, 4, 5).collect(Collectors.<Integer, Object, Object>toMap(i -> i, i -> Integer.toString(-i)));
private final MarshallingTesterFactory factory;
public AbstractConcurrentTestCase(MarshallingTesterFactory factory) {
this.factory = factory;
}
@Test
public void testConcurrentHashMap() throws IOException {
MarshallingTester<ConcurrentHashMap<Object, Object>> tester = this.factory.createTester();
tester.test(new ConcurrentHashMap<>(BASIS), AbstractConcurrentTestCase::assertMapEquals);
}
@Test
public void testConcurrentHashSet() throws IOException {
ConcurrentHashMap.KeySetView<Object, Boolean> keySetView = ConcurrentHashMap.newKeySet();
keySetView.addAll(BASIS.keySet());
MarshallingTester<ConcurrentHashMap.KeySetView<Object, Boolean>> tester = this.factory.createTester();
tester.test(keySetView, AbstractConcurrentTestCase::assertCollectionEquals);
}
@Test
public void testConcurrentLinkedDeque() throws IOException {
MarshallingTester<ConcurrentLinkedDeque<Object>> tester = this.factory.createTester();
tester.test(new ConcurrentLinkedDeque<>(BASIS.keySet()), AbstractConcurrentTestCase::assertCollectionEquals);
}
@Test
public void testConcurrentLinkedQueue() throws IOException {
MarshallingTester<ConcurrentLinkedQueue<Object>> tester = this.factory.createTester();
tester.test(new ConcurrentLinkedQueue<>(BASIS.keySet()), AbstractConcurrentTestCase::assertCollectionEquals);
}
@SuppressWarnings("unchecked")
@Test
public void testConcurrentSkipListMap() throws IOException {
MarshallingTester<ConcurrentSkipListMap<Object, Object>> tester = this.factory.createTester();
ConcurrentSkipListMap<Object, Object> map = new ConcurrentSkipListMap<>();
map.putAll(BASIS);
tester.test(map, AbstractConcurrentTestCase::assertMapEquals);
map = new ConcurrentSkipListMap<>((Comparator<Object>) (Comparator<?>) Comparator.reverseOrder());
map.putAll(BASIS);
tester.test(map, AbstractConcurrentTestCase::assertMapEquals);
map = new ConcurrentSkipListMap<>(new TestComparator<>());
map.putAll(BASIS);
tester.test(map, AbstractConcurrentTestCase::assertMapEquals);
tester.test(new ConcurrentSkipListMap<>(BASIS), AbstractConcurrentTestCase::assertMapEquals);
}
@SuppressWarnings("unchecked")
@Test
public void testConcurrentSkipListSet() throws IOException {
MarshallingTester<ConcurrentSkipListSet<Object>> tester = this.factory.createTester();
ConcurrentSkipListSet<Object> set = new ConcurrentSkipListSet<>();
set.addAll(BASIS.keySet());
tester.test(set, AbstractUtilTestCase::assertCollectionEquals);
set = new ConcurrentSkipListSet<>((Comparator<Object>) (Comparator<?>) Comparator.reverseOrder());
set.addAll(BASIS.keySet());
tester.test(set, AbstractUtilTestCase::assertCollectionEquals);
set = new ConcurrentSkipListSet<>(new TestComparator<>());
set.addAll(BASIS.keySet());
tester.test(set, AbstractUtilTestCase::assertCollectionEquals);
}
@Test
public void testCopyOnWriteArrayList() throws IOException {
MarshallingTester<CopyOnWriteArrayList<Object>> tester = this.factory.createTester();
tester.test(new CopyOnWriteArrayList<>(BASIS.keySet()), AbstractConcurrentTestCase::assertCollectionEquals);
}
@Test
public void testCopyOnWriteArraySet() throws IOException {
MarshallingTester<CopyOnWriteArraySet<Object>> tester = this.factory.createTester();
tester.test(new CopyOnWriteArraySet<>(BASIS.keySet()), AbstractConcurrentTestCase::assertCollectionEquals);
}
@Test
public void testTimeUnit() throws IOException {
this.factory.createTester(TimeUnit.class).test();
}
static <T extends Map<?, ?>> void assertMapEquals(T expected, T actual) {
Assert.assertEquals(expected.size(), actual.size());
Assert.assertTrue(expected.keySet().containsAll(actual.keySet()));
for (Map.Entry<?, ?> entry : expected.entrySet()) {
Assert.assertEquals(entry.getValue(), actual.get(entry.getKey()));
}
}
static <T extends Collection<?>> void assertCollectionEquals(T expected, T actual) {
Assert.assertEquals(expected.size(), actual.size());
Assert.assertTrue(expected.containsAll(actual));
}
}
| 6,389
| 41.6
| 164
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/test/java/org/wildfly/clustering/marshalling/ExternalizerTesterFactory.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;
import java.util.Arrays;
import java.util.EnumSet;
/**
* @author Paul Ferraro
*/
public class ExternalizerTesterFactory implements MarshallingTesterFactory {
private final Iterable<? extends Externalizer<?>> externalizers;
public <E extends Enum<E> & Externalizer<Object>> ExternalizerTesterFactory(Class<E> enumClass) {
this(EnumSet.allOf(enumClass));
}
public ExternalizerTesterFactory(Externalizer<?>... externalizers) {
this(Arrays.asList(externalizers));
}
public ExternalizerTesterFactory(Iterable<? extends Externalizer<?>> externalizers) {
this.externalizers = externalizers;
}
@Override
public <T> MarshallingTester<T> createTester() {
return new MarshallingTester<>(new ExternalizerMarshaller<T>(this.externalizers));
}
}
| 1,881
| 35.192308
| 101
|
java
|
null |
wildfly-main/clustering/marshalling/api/src/main/java/org/wildfly/clustering/marshalling/Externalizer.java
|
/*
* JBoss, Home of Professional Open Source.
* Copyright 2015, 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;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.OptionalInt;
/**
* Service provider interface for custom externalizers.
* @author Paul Ferraro
*/
public interface Externalizer<T> {
/**
* Writes the object reference to the stream.
*
* @param output the object output to write to
* @param object the object reference to write
* @throws IOException if an I/O error occurs
*/
void writeObject(ObjectOutput output, T object) throws IOException;
/**
* Read an instance from the stream. The instance will have been written by the
* {@link #writeObject(ObjectOutput, Object)} method. Implementations are free
* to create instances of the object read from the stream in any way that they
* feel like. This could be via constructor, factory or reflection.
*
* @param input the object input from which to read
* @return the object instance
* @throws IOException if an I/O error occurs
* @throws ClassNotFoundException if a class could not be found
*/
T readObject(ObjectInput input) throws IOException, ClassNotFoundException;
/**
* Returns the target class of the object to externalize.
* @return a class to be externalized
*/
Class<T> getTargetClass();
/**
* Returns the size of the buffer to use for marshalling the specified object, if known.
* @return the buffer size (in bytes), or empty if unknown.
*/
default OptionalInt size(T object) {
return OptionalInt.empty();
}
}
| 2,654
| 35.875
| 92
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/test/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamCircularReferenceTestCase.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.wildfly.clustering.marshalling.AbstractCircularReferenceTestCase;
/**
* @author Paul Ferraro
*/
public class ProtoStreamCircularReferenceTestCase extends AbstractCircularReferenceTestCase {
public ProtoStreamCircularReferenceTestCase() {
super(ProtoStreamTesterFactory.INSTANCE);
}
}
| 1,396
| 37.805556
| 93
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/test/java/org/wildfly/clustering/marshalling/protostream/SimpleObjectOutputTestCase.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.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.UUID;
import org.junit.Assert;
import org.junit.Test;
/**
* @author Paul Ferraro
*/
public class SimpleObjectOutputTestCase {
@Test
public void test() throws IOException {
Object[] objects = new Object[2];
String[] strings = new String[2];
int[] ints = new int[2];
long[] longs = new long[2];
double[] doubles = new double[2];
ObjectOutput output = new SimpleObjectOutput.Builder().with(objects).with(strings).with(ints).with(longs).with(doubles).build();
TestExternalizable test = new TestExternalizable();
test.writeExternal(output);
for (int i = 0; i < 2; ++i) {
Assert.assertSame(test.objects[i], objects[i]);
Assert.assertSame(test.strings[i], strings[i]);
Assert.assertEquals(test.ints[i], ints[i]);
Assert.assertEquals(test.longs[i], longs[i]);
Assert.assertEquals(test.doubles[i], doubles[i], 0);
}
}
static class TestExternalizable implements Externalizable {
final Object[] objects = new Object[] { UUID.randomUUID(), UUID.randomUUID() };
final String[] strings = new String[] { "foo", "bar" };
final int[] ints = new int[] { Integer.MIN_VALUE, Integer.MAX_VALUE };
final long[] longs = new long[] { Long.MIN_VALUE, Long.MAX_VALUE };
final double[] doubles = new double[] { Double.MIN_VALUE, Double.MAX_VALUE };
@Override
public void writeExternal(ObjectOutput output) throws IOException {
for (int i = 0; i < 2; ++i) {
output.writeObject(this.objects[i]);
output.writeUTF(this.strings[i]);
output.writeInt(this.ints[i]);
output.writeLong(this.longs[i]);
output.writeDouble(this.doubles[i]);
}
}
@Override
public void readExternal(ObjectInput input) {
}
}
}
| 3,146
| 36.023529
| 136
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/test/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamByteBufferMarshalledKeyFactoryTestCase.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.wildfly.clustering.marshalling.spi.ByteBufferMarshalledKeyFactoryTestCase;
/**
* @author Paul Ferraro
*/
public class ProtoStreamByteBufferMarshalledKeyFactoryTestCase extends ByteBufferMarshalledKeyFactoryTestCase {
public ProtoStreamByteBufferMarshalledKeyFactoryTestCase() {
super(TestProtoStreamByteBufferMarshaller.INSTANCE);
}
}
| 1,447
| 39.222222
| 111
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/test/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamNetTestCase.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.wildfly.clustering.marshalling.AbstractNetTestCase;
/**
* ProtoStream tests for java.net package.
* @author Paul Ferraro
*/
public class ProtoStreamNetTestCase extends AbstractNetTestCase {
public ProtoStreamNetTestCase() {
super(ProtoStreamTesterFactory.INSTANCE);
}
}
| 1,383
| 36.405405
| 70
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/test/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamAtomicTestCase.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.wildfly.clustering.marshalling.AbstractAtomicTestCase;
/**
* ProtoStream tests for java.util.concurrent.atomic package.
* @author Paul Ferraro
*/
public class ProtoStreamAtomicTestCase extends AbstractAtomicTestCase {
public ProtoStreamAtomicTestCase() {
super(ProtoStreamTesterFactory.INSTANCE);
}
}
| 1,414
| 37.243243
| 71
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/test/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamLangTestCase.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.wildfly.clustering.marshalling.AbstractLangTestCase;
/**
* ProtoStream tests for primitives and arrays.
* @author Paul Ferraro
*/
public class ProtoStreamLangTestCase extends AbstractLangTestCase {
public ProtoStreamLangTestCase() {
super(ProtoStreamTesterFactory.INSTANCE);
}
}
| 1,392
| 36.648649
| 70
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/test/java/org/wildfly/clustering/marshalling/protostream/PersonMarshaller.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;
import org.wildfly.clustering.marshalling.Person;
/**
* @author Paul Ferraro
*/
public class PersonMarshaller implements ProtoStreamMarshaller<Person> {
private static final int NAME_INDEX = 1;
private static final int PARENT_INDEX = 2;
private static final int CHILD_INDEX = 3;
@Override
public Class<? extends Person> getJavaClass() {
return Person.class;
}
@Override
public Person readFrom(ProtoStreamReader reader) throws IOException {
Person person = new Person();
reader.getContext().record(person);
while (!reader.isAtEnd()) {
int tag = reader.readTag();
switch (WireType.getTagFieldNumber(tag)) {
case NAME_INDEX:
person.setName(reader.readString());
break;
case PARENT_INDEX:
Person parent = reader.readAny(Person.class);
parent.addChild(person);
break;
case CHILD_INDEX:
person.addChild(reader.readAny(Person.class));
break;
default:
reader.skipField(tag);
}
}
return person;
}
@Override
public void writeTo(ProtoStreamWriter writer, Person person) throws IOException {
writer.getContext().record(person);
String name = person.getName();
if (name != null) {
writer.writeString(NAME_INDEX, person.getName());
}
Person parent = person.getParent();
if (parent != null) {
writer.writeAny(PARENT_INDEX, parent);
}
for (Person child : person.getChildren()) {
writer.writeAny(CHILD_INDEX, child);
}
}
}
| 2,943
| 34.047619
| 85
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/test/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamConcurrentTestCase.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.wildfly.clustering.marshalling.AbstractConcurrentTestCase;
/**
* ProtoStream tests for java.util.concurrent package.
* @author Paul Ferraro
*/
public class ProtoStreamConcurrentTestCase extends AbstractConcurrentTestCase {
public ProtoStreamConcurrentTestCase() {
super(ProtoStreamTesterFactory.INSTANCE);
}
}
| 1,423
| 37.486486
| 79
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/test/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamUtilTestCase.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.wildfly.clustering.marshalling.AbstractUtilTestCase;
/**
* ProtoStream tests for java.util package.
* @author Paul Ferraro
*/
public class ProtoStreamUtilTestCase extends AbstractUtilTestCase {
public ProtoStreamUtilTestCase() {
super(ProtoStreamTesterFactory.INSTANCE);
}
}
| 1,388
| 36.540541
| 70
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/test/java/org/wildfly/clustering/marshalling/protostream/NativeProtoStreamTestCase.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 org.infinispan.protostream.SerializationContextInitializer;
import org.infinispan.protostream.annotations.AutoProtoSchemaBuilder;
import org.infinispan.protostream.annotations.ProtoAdapter;
import org.infinispan.protostream.annotations.ProtoEnumValue;
import org.infinispan.protostream.annotations.ProtoFactory;
import org.infinispan.protostream.annotations.ProtoField;
import org.junit.Assert;
import org.junit.Test;
import org.wildfly.clustering.marshalling.MarshallingTesterFactory;
/**
* Test marshalling of class that use native ProtoStream annotations.
* @author Paul Ferraro
*/
public class NativeProtoStreamTestCase {
@Test
public void test() throws IOException {
MarshallingTesterFactory factory = ProtoStreamTesterFactory.INSTANCE;
factory.createTester(Sex.class).test();
Employee head = new Employee(1, new Name("Allegra", "Coleman"), Sex.FEMALE, null);
Employee manager = new Employee(2, new Name("John", "Barron"), Sex.MALE, head);
Employee employee = new Employee(3, new Name("Alan", "Smithee"), Sex.MALE, manager);
factory.<Employee>createTester().test(employee, NativeProtoStreamTestCase::equals);
}
static void equals(Employee expected, Employee actual) {
Assert.assertEquals(expected, actual);
Assert.assertEquals(expected.getName(), actual.getName());
Assert.assertSame(expected.getSex(), actual.getSex());
Assert.assertEquals(expected.isHead(), actual.isHead());
if (!expected.isHead()) {
equals(expected.getManager(), actual.getManager());
}
}
static enum Sex {
@ProtoEnumValue(0) MALE,
@ProtoEnumValue(1) FEMALE,
;
}
static class Employee {
private final Integer id;
private final Name name;
private final Sex sex;
private final Employee manager;
@ProtoFactory
Employee(Integer id, Name name, Sex sex, Employee manager) {
this.id = id;
this.name = name;
this.sex = sex;
this.manager = manager;
}
@ProtoField(1)
Integer getId() {
return this.id;
}
@ProtoField(2)
Name getName() {
return this.name;
}
@ProtoField(3)
Sex getSex() {
return this.sex;
}
@ProtoField(4)
Employee getManager() {
return this.manager;
}
boolean isHead() {
return this.manager == null;
}
@Override
public int hashCode() {
return this.id.hashCode();
}
@Override
public boolean equals(Object object) {
if (!(object instanceof Employee)) return false;
Employee employee = (Employee) object;
return Objects.equals(this.id, employee.id);
}
}
static class Name {
private final String first;
private final String last;
Name(String first, String last) {
this.first = first;
this.last = last;
}
String getFirst() {
return this.first;
}
String getLast() {
return this.last;
}
@Override
public boolean equals(Object object) {
if (!(object instanceof Name)) return false;
Name name = (Name) object;
return Objects.equals(this.first, name.first) && Objects.equals(this.last, name.last);
}
@Override
public int hashCode() {
return Objects.hash(this.last, this.first);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
if (this.last != null) {
builder.append(this.last);
if (this.first != null) {
builder.append(", ").append(this.first);
}
}
return builder.toString();
}
}
@ProtoAdapter(Name.class)
static class NameFactory {
@ProtoFactory
static Name create(String first, String last) {
return new Name(first, last);
}
@ProtoField(1)
static String getFirst(Name name) {
return name.getFirst();
}
@ProtoField(2)
static String getLast(Name name) {
return name.getLast();
}
}
@AutoProtoSchemaBuilder(includeClasses = { Sex.class, NameFactory.class, Employee.class }, service = false)
static interface EmployeeInitializer extends SerializationContextInitializer {
}
}
| 5,779
| 29.744681
| 111
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/test/java/org/wildfly/clustering/marshalling/protostream/TestProtoStreamByteBufferMarshaller.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.InputStream;
import java.io.OutputStream;
import java.util.OptionalInt;
import org.infinispan.protostream.ImmutableSerializationContext;
import org.wildfly.clustering.marshalling.spi.ByteBufferMarshaller;
/**
* @author Paul Ferraro
*/
public enum TestProtoStreamByteBufferMarshaller implements ByteBufferMarshaller {
INSTANCE;
private final ByteBufferMarshaller marshaller;
TestProtoStreamByteBufferMarshaller() {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
ImmutableSerializationContext context = new SerializationContextBuilder(new SimpleClassLoaderMarshaller(loader)).load(loader).build();
this.marshaller = new ProtoStreamByteBufferMarshaller(context);
}
@Override
public boolean isMarshallable(Object object) {
return this.marshaller.isMarshallable(object);
}
@Override
public Object readFrom(InputStream input) throws IOException {
return this.marshaller.readFrom(input);
}
@Override
public void writeTo(OutputStream output, Object object) throws IOException {
this.marshaller.writeTo(output, object);
}
@Override
public OptionalInt size(Object object) {
return this.marshaller.size(object);
}
}
| 2,381
| 34.552239
| 142
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/test/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamTimeTestCase.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.wildfly.clustering.marshalling.AbstractTimeTestCase;
/**
* ProtoStream tests for java.time package.
* @author Paul Ferraro
*/
public class ProtoStreamTimeTestCase extends AbstractTimeTestCase {
public ProtoStreamTimeTestCase() {
super(ProtoStreamTesterFactory.INSTANCE);
}
}
| 1,388
| 36.540541
| 70
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/test/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamMathTestCase.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.wildfly.clustering.marshalling.AbstractMathTestCase;
/**
* @author Paul Ferraro
*/
public class ProtoStreamMathTestCase extends AbstractMathTestCase {
public ProtoStreamMathTestCase() {
super(ProtoStreamTesterFactory.INSTANCE);
}
}
| 1,344
| 36.361111
| 70
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/test/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamTesterFactory.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.wildfly.clustering.marshalling.MarshallingTester;
import org.wildfly.clustering.marshalling.MarshallingTesterFactory;
import org.wildfly.clustering.marshalling.spi.ByteBufferTestMarshaller;
/**
* @author Paul Ferraro
*/
public enum ProtoStreamTesterFactory implements MarshallingTesterFactory {
INSTANCE;
@Override
public <T> MarshallingTester<T> createTester() {
return new MarshallingTester<>(new ByteBufferTestMarshaller<>(TestProtoStreamByteBufferMarshaller.INSTANCE));
}
}
| 1,598
| 38.975
| 117
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/test/java/org/wildfly/clustering/marshalling/protostream/SimpleObjectInputTestCase.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.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.nio.ByteBuffer;
import java.util.UUID;
import org.junit.Assert;
import org.junit.Test;
/**
* @author Paul Ferraro
*/
public class SimpleObjectInputTestCase {
@Test
public void test() throws IOException, ClassNotFoundException {
final Object[] objects = new Object[] { UUID.randomUUID(), UUID.randomUUID() };
final String[] strings = new String[] { "foo", "bar" };
final int[] ints = new int[] { Integer.MIN_VALUE, Integer.MAX_VALUE };
final long[] longs = new long[] { Long.MIN_VALUE, Long.MAX_VALUE };
final double[] doubles = new double[] { Double.MIN_VALUE, Double.MAX_VALUE };
ObjectInput input = new SimpleObjectInput.Builder().with(objects).with(strings).with(ints).with(longs).with(doubles).build();
TestExternalizable test = new TestExternalizable();
test.readExternal(input);
for (int i = 0; i < 2; ++i) {
Assert.assertSame(objects[i], test.objects[i]);
Assert.assertSame(strings[i], test.strings[i]);
Assert.assertEquals(ints[i], test.ints[i]);
Assert.assertEquals(longs[i], test.longs[i]);
Assert.assertEquals(doubles[i], test.doubles[i], 0);
}
}
static class TestExternalizable implements Externalizable {
final Object[] objects = new Object[2];
final String[] strings = new String[2];
final ByteBuffer[] buffers = new ByteBuffer[2];
final int[] ints = new int[2];
final long[] longs = new long[2];
final double[] doubles = new double[2];
@Override
public void writeExternal(ObjectOutput output) {
}
@Override
public void readExternal(ObjectInput input) throws ClassNotFoundException, IOException {
for (int i = 0; i < 2; ++i) {
this.objects[i] = input.readObject();
this.strings[i] = input.readUTF();
this.ints[i] = input.readInt();
this.longs[i] = input.readLong();
this.doubles[i] = input.readDouble();
}
}
}
}
| 3,307
| 37.022989
| 133
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/test/java/org/wildfly/clustering/marshalling/protostream/TestSerializationContextInitializer.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;
import org.wildfly.clustering.marshalling.Empty;
import org.wildfly.clustering.marshalling.TestComparator;
import org.wildfly.clustering.marshalling.TestInvocationHandler;
/**
* @author Paul Ferraro
*/
public class TestSerializationContextInitializer extends AbstractSerializationContextInitializer {
public TestSerializationContextInitializer() {
super("org.wildfly.clustering.marshalling.proto");
}
@Override
public void registerMarshallers(SerializationContext context) {
context.registerMarshaller(new ValueMarshaller<>(new TestComparator<>()));
context.registerMarshaller(new EnumMarshaller<>(Empty.class));
context.registerMarshaller(new FunctionalScalarMarshaller<>(TestInvocationHandler.class, Scalar.ANY, TestInvocationHandler::getValue, TestInvocationHandler::new));
context.registerMarshaller(new PersonMarshaller());
}
}
| 2,035
| 42.319149
| 171
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/test/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamSQLTestCase.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.wildfly.clustering.marshalling.AbstractSQLTestCase;
/**
* ProtoStream tests for java.sql package.
* @author Paul Ferraro
*/
public class ProtoStreamSQLTestCase extends AbstractSQLTestCase {
public ProtoStreamSQLTestCase() {
super(ProtoStreamTesterFactory.INSTANCE);
}
}
| 1,383
| 36.405405
| 70
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/test/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamByteBufferMarshalledValueFactoryTestCase.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.wildfly.clustering.marshalling.spi.ByteBufferMarshalledValueFactoryTestCase;
/**
* @author Paul Ferraro
*/
public class ProtoStreamByteBufferMarshalledValueFactoryTestCase extends ByteBufferMarshalledValueFactoryTestCase {
public ProtoStreamByteBufferMarshalledValueFactoryTestCase() {
super(TestProtoStreamByteBufferMarshaller.INSTANCE);
}
}
| 1,455
| 39.444444
| 115
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/Marshallable.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;
/**
* Interface inherited by marshallable components.
* @author Paul Ferraro
* @param <T> the type of this marshaller
*/
public interface Marshallable<T> {
/**
* Reads an object from the specified reader.
* @param reader a ProtoStream reader
* @return the read object
* @throws IOException if the object could not be read
*/
T readFrom(ProtoStreamReader reader) throws IOException;
/**
* Writes the specified object to the specified writer.
* @param writer a ProtoStream writer
* @param value the object to be written
* @throws IOException if the object could not be written
*/
void writeTo(ProtoStreamWriter writer, T value) throws IOException;
/**
* Computes the size of the specified object.
* @param context the marshalling operation
* @param value the value whose size is to be calculated
* @return an optional buffer size, only present if the buffer size could be computed
*/
default OptionalInt size(ProtoStreamSizeOperation operation, T value) {
return operation.computeSize(this::writeTo, value);
}
/**
* Returns the type of object handled by this marshallable instance.
* @return the type of object handled by this marshallable instance.
*/
Class<? extends T> getJavaClass();
}
| 2,470
| 35.880597
| 89
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/SimpleDataInput.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.DataInput;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.PrimitiveIterator;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
/**
* {@link DataInput} implementation used to write the unexposed fields of an {@link org.jgroups.util.Streamable} object.
* @author Paul Ferraro
*/
public class SimpleDataInput implements DataInput {
private final Iterator<String> strings;
private final Iterator<ByteBuffer> buffers;
private final Iterator<Character> characters;
private final Iterator<Boolean> booleans;
private final Iterator<Byte> bytes;
private final Iterator<Short> shorts;
private final PrimitiveIterator.OfInt integers;
private final PrimitiveIterator.OfLong longs;
private final Iterator<Float> floats;
private final PrimitiveIterator.OfDouble doubles;
SimpleDataInput(Builder builder) {
this.strings = builder.strings.iterator();
this.buffers = builder.buffers.iterator();
this.characters = builder.characters.iterator();
this.booleans = builder.booleans.iterator();
this.bytes = builder.bytes.iterator();
this.shorts = builder.shorts.iterator();
this.integers = builder.integers.iterator();
this.longs = builder.longs.iterator();
this.floats = builder.floats.iterator();
this.doubles = builder.doubles.iterator();
}
ByteBuffer nextBuffer() {
return this.buffers.next();
}
@Override
public String readUTF() {
return this.strings.next();
}
@Override
public int readInt() {
return this.integers.nextInt();
}
@Override
public long readLong() {
return this.longs.nextLong();
}
@Override
public double readDouble() {
return this.doubles.nextDouble();
}
@Override
public void readFully(byte[] bytes) {
this.nextBuffer().get(bytes);
}
@Override
public void readFully(byte[] bytes, int offset, int length) {
this.nextBuffer().get(bytes, offset, length);
}
@Override
public int skipBytes(int n) {
throw new UnsupportedOperationException();
}
@Override
public boolean readBoolean() {
return this.booleans.next();
}
@Override
public byte readByte() {
return this.bytes.next();
}
@Override
public int readUnsignedByte() {
return Byte.toUnsignedInt(this.bytes.next());
}
@Override
public short readShort() {
return this.shorts.next();
}
@Override
public int readUnsignedShort() {
return Short.toUnsignedInt(this.shorts.next());
}
@Override
public char readChar() {
return this.characters.next();
}
@Override
public float readFloat() {
return this.floats.next();
}
@Override
public String readLine() {
return this.strings.next();
}
public static class Builder {
Iterable<String> strings = Collections.emptyList();
Iterable<ByteBuffer> buffers = Collections.emptyList();
Iterable<Character> characters = Collections.emptyList();
Iterable<Boolean> booleans = Collections.emptyList();
Iterable<Byte> bytes = Collections.emptyList();
Iterable<Short> shorts = Collections.emptyList();
IntStream integers = IntStream.empty();
LongStream longs = LongStream.empty();
Iterable<Float> floats = Collections.emptyList();
DoubleStream doubles = DoubleStream.empty();
public Builder with(String... values) {
this.strings = Arrays.asList(values);
return this;
}
public Builder with(ByteBuffer... buffers) {
this.buffers = Arrays.asList(buffers);
return this;
}
public Builder with(char... values) {
this.characters = new ArrayIterable<>(values);
return this;
}
public Builder with(boolean... values) {
this.booleans = new ArrayIterable<>(values);
return this;
}
public Builder with(byte... values) {
this.bytes = new ArrayIterable<>(values);
return this;
}
public Builder with(short... values) {
this.shorts = new ArrayIterable<>(values);
return this;
}
public Builder with(int... values) {
this.integers = IntStream.of(values);
return this;
}
public Builder with(long... values) {
this.longs = LongStream.of(values);
return this;
}
public Builder with(float... values) {
this.floats = new ArrayIterable<>(values);
return this;
}
public Builder with(double... values) {
this.doubles = DoubleStream.of(values);
return this;
}
public DataInput build() {
return new SimpleDataInput(this);
}
}
private static class ArrayIterable<T> implements Iterable<T> {
private final Object array;
ArrayIterable(Object array) {
this.array = array;
}
@Override
public Iterator<T> iterator() {
Object array = this.array;
return new Iterator<>() {
private int index = 0;
@Override
public boolean hasNext() {
return Array.getLength(array) > this.index;
}
@SuppressWarnings("unchecked")
@Override
public T next() {
return (T) Array.get(array, this.index++);
}
};
}
}
}
| 6,961
| 28.129707
| 120
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ProviderSerializationContextInitializer.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.EnumSet;
import org.infinispan.protostream.SerializationContext;
import org.wildfly.security.manager.WildFlySecurityManager;
/**
* A {@link org.infinispan.protostream.SerializationContextInitializer} that registers enumerated marshallers.
* @author Paul Ferraro
* @param <E> the marshaller provider provider type
*/
public class ProviderSerializationContextInitializer<E extends Enum<E> & ProtoStreamMarshallerProvider> extends AbstractSerializationContextInitializer {
private final Class<E> providerClass;
public ProviderSerializationContextInitializer(String resourceName, Class<E> providerClass) {
super(resourceName, WildFlySecurityManager.getClassLoaderPrivileged(providerClass));
this.providerClass = providerClass;
}
@Override
public void registerMarshallers(SerializationContext context) {
for (E provider : EnumSet.allOf(this.providerClass)) {
context.registerMarshaller(provider.getMarshaller());
}
}
}
| 2,089
| 39.980392
| 153
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamWriter.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.TagWriter;
import org.infinispan.protostream.descriptors.WireType;
/**
* A {@link TagWriter} with the additional ability to write an arbitrary embedded object.
* @author Paul Ferraro
*/
public interface ProtoStreamWriter extends ProtoStreamOperation, TagWriter {
/**
* Writes the specified object of an abitrary type using the specified index.
* Object will be read via {@link ProtoStreamReader#readAny()}.
* @param index a field index
* @param value a value to be written
* @throws IOException if no marshaller is associated with the type of the specified object, or if the marshaller fails to write the specified object
*/
default void writeAny(int index, Object value) throws IOException {
this.writeTag(index, WireType.LENGTH_DELIMITED);
this.writeAnyNoTag(value);
}
/**
* Writes the specified object of a specific type using the specified index.
* Object will be read via {@link ProtoStreamReader#readObject(Class)}.
* @param index a field index
* @param value a value to be written
* @throws IOException if no marshaller is associated with the type of the specified object, or if the marshaller fails to write the specified object
*/
default void writeObject(int index, Object value) throws IOException {
this.writeTag(index, WireType.LENGTH_DELIMITED);
this.writeObjectNoTag(value);
}
/**
* Writes the specified object of an arbitrary type. Must be preceded by {{@link #writeTag(int, org.infinispan.protostream.descriptors.WireType)}.
* @param value a value to be written
* @throws IOException if no marshaller is associated with the type of the specified object, or if the marshaller fails to write the specified object
*/
void writeAnyNoTag(Object value) throws IOException;
/**
* Writes the specified object of a specific type. Must be preceded by {{@link #writeTag(int, org.infinispan.protostream.descriptors.WireType)}.
* @param value a value to be written
* @throws IOException if no marshaller is associated with the type of the specified object, or if the marshaller fails to write the specified object
*/
void writeObjectNoTag(Object value) throws IOException;
/**
* Writes the specified enum field using the specified index.
* Object will be read via {@link ProtoStreamReader#readEnum(Class)}.
* @param index a field index
* @param value an enum to be written
* @throws IOException if no marshaller is associated with the type of the specified object, or if the marshaller fails to write the specified object
*/
default <E extends Enum<E>> void writeEnum(int index, E value) throws IOException {
EnumMarshaller<E> marshaller = (EnumMarshaller<E>) this.getSerializationContext().getMarshaller(value.getDeclaringClass());
this.writeEnum(index, marshaller.encode(value));
}
/**
* Deprecated to discourage use.
* @deprecated Use {@link #writeTag(int, WireType)} instead.
*/
@Deprecated
@Override
default void writeTag(int index, int wireType) throws IOException {
this.writeTag(index, WireType.fromValue(wireType));
}
/**
* Deprecated to discourage use.
* @deprecated Use {@link #writeUInt32(int, int)} or {@link #writeSInt32(int, int)}
*/
@Deprecated
@Override
void writeInt32(int index, int value) throws IOException;
/**
* Deprecated to discourage use.
* @deprecated Use {@link #writeUInt64(int, int)} or {@link #writeSInt64(int, int)}
*/
@Deprecated
@Override
void writeInt64(int index, long value) throws IOException;
/**
* Deprecated to discourage use.
* @deprecated Use {@link #writeSFixed32(int, int)} instead.
*/
@Deprecated
@Override
void writeFixed32(int index, int value) throws IOException;
/**
* Deprecated to discourage use.
* @deprecated Use {@link #writeSFixed64(int, int)} instead.
*/
@Deprecated
@Override
void writeFixed64(int index, long value) throws IOException;
}
| 5,258
| 40.085938
| 153
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/TypedEnumMarshaller.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 enumeration.
* @author Paul Ferraro
* @param <E> the enum type of this marshaller
*/
public class TypedEnumMarshaller<E extends Enum<E>> implements FieldMarshaller<E> {
// Optimize for singleton enums
private static final int DEFAULT_ORDINAL = 0;
private final ScalarMarshaller<Class<?>> type;
public TypedEnumMarshaller(ScalarMarshaller<Class<?>> type) {
this.type = type;
}
@Override
public E readFrom(ProtoStreamReader reader) throws IOException {
@SuppressWarnings("unchecked")
Class<E> enumClass = (Class<E>) this.type.readFrom(reader);
E result = enumClass.getEnumConstants()[DEFAULT_ORDINAL];
while (!reader.isAtEnd()) {
int tag = reader.readTag();
int index = WireType.getTagFieldNumber(tag);
if (index == AnyField.ANY.getIndex()) {
result = reader.readEnum(enumClass);
} else {
reader.skipField(tag);
}
}
return result;
}
@Override
public void writeTo(ProtoStreamWriter writer, E value) throws IOException {
Class<E> enumClass = value.getDeclaringClass();
this.type.writeTo(writer, enumClass);
if (value.ordinal() != DEFAULT_ORDINAL) {
writer.writeEnum(AnyField.ANY.getIndex(), value);
}
}
@SuppressWarnings("unchecked")
@Override
public Class<? extends E> getJavaClass() {
return (Class<E>) (Class<?>) Enum.class;
}
@Override
public WireType getWireType() {
return this.type.getWireType();
}
}
| 2,791
| 33.04878
| 83
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/DefaultProtoStreamWriter.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.OptionalInt;
import java.util.function.Function;
import org.infinispan.protostream.ImmutableSerializationContext;
import org.infinispan.protostream.ProtobufTagMarshaller.WriteContext;
import org.infinispan.protostream.impl.TagWriterImpl;
import org.wildfly.clustering.marshalling.spi.ByteBufferOutputStream;
/**
* {@link ProtoStreamWriter} implementation that writes to a {@link TagWriterImpl}.
* @author Paul Ferraro
*/
public class DefaultProtoStreamWriter extends AbstractProtoStreamWriter implements Function<Object, OptionalInt> {
private final ProtoStreamWriterContext context;
/**
* Creates a default ProtoStream writer.
* @param context the write context
*/
public DefaultProtoStreamWriter(WriteContext context) {
this(context, new DefaultProtoStreamWriterContext());
}
private DefaultProtoStreamWriter(WriteContext context, ProtoStreamWriterContext writerContext) {
super(context, writerContext);
this.context = writerContext;
}
@Override
public ProtoStreamOperation.Context getContext() {
return this.context;
}
@Override
public void writeObjectNoTag(Object value) throws IOException {
ImmutableSerializationContext context = this.getSerializationContext();
ProtoStreamMarshaller<Object> marshaller = this.findMarshaller(value.getClass());
OptionalInt size = this.context.computeSize(value, this);
if (size.isPresent()) {
// If size is known, we can marshal directly to our output stream
int length = size.getAsInt();
this.writeVarint32(length);
if (length > 0) {
marshaller.writeTo(this, value);
}
} else {
// If size is unknown, marshal to an expandable temporary buffer
// This should only be the case if delegating to JBoss Marshalling or Java Serialization
try (ByteBufferOutputStream output = new ByteBufferOutputStream()) {
TagWriterImpl writer = TagWriterImpl.newInstanceNoBuffer(context, output);
marshaller.writeTo(new DefaultProtoStreamWriter(writer, this.context), value);
// Byte buffer is array backed
ByteBuffer buffer = output.getBuffer();
int offset = buffer.arrayOffset();
int length = buffer.limit() - offset;
this.writeVarint32(length);
if (length > 0) {
this.writeRawBytes(buffer.array(), offset, length);
}
}
}
}
@Override
public OptionalInt apply(Object value) {
ProtoStreamMarshaller<Object> marshaller = this.findMarshaller(value.getClass());
// Retain reference integrity by using a copy of the current context during size operation
return marshaller.size(new DefaultProtoStreamSizeOperation(this.getSerializationContext(), this.context.clone()), value);
}
}
| 4,126
| 41.546392
| 129
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/SimpleDataOutput.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.DataOutput;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.util.function.Consumer;
import java.util.function.DoubleConsumer;
import java.util.function.IntConsumer;
import java.util.function.LongConsumer;
import org.wildfly.common.function.Functions;
/**
* {@link DataOutput} implementation used to read the unexposed fields of an {@link org.jgroups.util.Streamable} object.
* @author Paul Ferraro
*/
public class SimpleDataOutput implements DataOutput {
private final Consumer<String> stringConsumer;
private final Consumer<ByteBuffer> bufferConsumer;
private final Consumer<Character> charConsumer;
private final Consumer<Boolean> booleanConsumer;
private final Consumer<Byte> byteConsumer;
private final Consumer<Short> shortConsumer;
private final IntConsumer intConsumer;
private final LongConsumer longConsumer;
private final Consumer<Float> floatConsumer;
private final DoubleConsumer doubleConsumer;
SimpleDataOutput(Builder builder) {
this.stringConsumer = builder.stringConsumer;
this.bufferConsumer = builder.bufferConsumer;
this.charConsumer = builder.charConsumer;
this.booleanConsumer = builder.booleanConsumer;
this.byteConsumer = builder.byteConsumer;
this.shortConsumer = builder.shortConsumer;
this.intConsumer = builder.intConsumer;
this.longConsumer = builder.longConsumer;
this.floatConsumer = builder.floatConsumer;
this.doubleConsumer = builder.doubleConsumer;
}
@Override
public void writeInt(int value) {
this.intConsumer.accept(value);
}
@Override
public void writeLong(long value) {
this.longConsumer.accept(value);
}
@Override
public void writeDouble(double value) {
this.doubleConsumer.accept(value);
}
@Override
public void writeUTF(String value) {
this.stringConsumer.accept(value);
}
@Override
public void write(byte[] buffer) {
this.bufferConsumer.accept(ByteBuffer.wrap(buffer));
}
@Override
public void write(byte[] buffer, int offset, int length) {
this.bufferConsumer.accept(ByteBuffer.wrap(buffer, offset, length));
}
@Override
public void write(int value) {
this.writeByte(value);
}
@Override
public void writeBoolean(boolean value) {
this.booleanConsumer.accept(value);
}
@Override
public void writeByte(int value) {
this.byteConsumer.accept((byte) value);
}
@Override
public void writeShort(int value) {
this.shortConsumer.accept((short) value);
}
@Override
public void writeChar(int value) {
this.charConsumer.accept((char) value);
}
@Override
public void writeFloat(float value) {
this.floatConsumer.accept(value);
}
@Override
public void writeBytes(String value) {
this.stringConsumer.accept(value);
}
@Override
public void writeChars(String value) {
this.stringConsumer.accept(value);
}
public static class Builder {
Consumer<String> stringConsumer = Functions.discardingConsumer();
Consumer<Character> charConsumer = Functions.discardingConsumer();
Consumer<ByteBuffer> bufferConsumer = Functions.discardingConsumer();
Consumer<Boolean> booleanConsumer = Functions.discardingConsumer();
Consumer<Byte> byteConsumer = Functions.discardingConsumer();
Consumer<Short> shortConsumer = Functions.discardingConsumer();
IntConsumer intConsumer = DiscardingConsumer.INSTANCE;
LongConsumer longConsumer = DiscardingConsumer.INSTANCE;
Consumer<Float> floatConsumer = Functions.discardingConsumer();
DoubleConsumer doubleConsumer = DiscardingConsumer.INSTANCE;
public Builder with(String[] values) {
this.stringConsumer = new ArrayConsumer<>(values);
return this;
}
public Builder with(char[] values) {
this.charConsumer = new GenericArrayConsumer<>(values);
return this;
}
public Builder with(ByteBuffer[] values) {
this.bufferConsumer = new ArrayConsumer<>(values);
return this;
}
public Builder with(boolean[] values) {
this.booleanConsumer = new GenericArrayConsumer<>(values);
return this;
}
public Builder with(byte[] values) {
this.byteConsumer = new GenericArrayConsumer<>(values);
return this;
}
public Builder with(short[] values) {
this.shortConsumer = new GenericArrayConsumer<>(values);
return this;
}
public Builder with(int[] values) {
this.intConsumer = new IntArrayConsumer(values);
return this;
}
public Builder with(long[] values) {
this.longConsumer = new LongArrayConsumer(values);
return this;
}
public Builder with(float[] values) {
this.floatConsumer = new GenericArrayConsumer<>(values);
return this;
}
public Builder with(double[] values) {
this.doubleConsumer = new DoubleArrayConsumer(values);
return this;
}
public DataOutput build() {
return new SimpleDataOutput(this);
}
}
enum DiscardingConsumer implements IntConsumer, LongConsumer, DoubleConsumer {
INSTANCE;
@Override
public void accept(long value) {
}
@Override
public void accept(int value) {
}
@Override
public void accept(double value) {
}
}
public static class ArrayConsumer<T> implements Consumer<T> {
private T[] values;
private int index = 0;
ArrayConsumer(T[] values) {
this.values = values;
}
@Override
public void accept(T value) {
this.values[this.index++] = value;
}
}
public static class GenericArrayConsumer<T> implements Consumer<T> {
private Object values;
private int index = 0;
GenericArrayConsumer(Object values) {
this.values = values;
}
@Override
public void accept(T value) {
Array.set(this.values, this.index++, value);
}
}
static class IntArrayConsumer implements IntConsumer {
private int[] values;
private int index = 0;
IntArrayConsumer(int[] values) {
this.values = values;
}
@Override
public void accept(int value) {
this.values[this.index++] = value;
}
}
static class LongArrayConsumer implements LongConsumer {
private long[] values;
private int index = 0;
LongArrayConsumer(long[] values) {
this.values = values;
}
@Override
public void accept(long value) {
this.values[this.index++] = value;
}
}
static class DoubleArrayConsumer implements DoubleConsumer {
private double[] values;
private int index = 0;
DoubleArrayConsumer(double[] values) {
this.values = values;
}
@Override
public void accept(double value) {
this.values[this.index++] = value;
}
}
}
| 8,532
| 28.628472
| 120
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ByteBufferMarshalledValueMarshaller.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.nio.ByteBuffer;
import java.util.OptionalInt;
import org.infinispan.protostream.descriptors.WireType;
import org.wildfly.clustering.marshalling.spi.ByteBufferMarshalledValue;
/**
* {@link ProtoStreamMarshaller} for a {@link ByteBufferMarshalledValue}.
* @author Paul Ferraro
*/
public class ByteBufferMarshalledValueMarshaller implements ProtoStreamMarshaller<ByteBufferMarshalledValue<Object>> {
private static final int BUFFER_INDEX = 1;
@Override
public ByteBufferMarshalledValue<Object> readFrom(ProtoStreamReader reader) throws IOException {
ByteBuffer buffer = null;
while (!reader.isAtEnd()) {
int tag = reader.readTag();
switch (WireType.getTagFieldNumber(tag)) {
case BUFFER_INDEX:
buffer = reader.readByteBuffer();
break;
default:
reader.skipField(tag);
}
}
return new ByteBufferMarshalledValue<>(buffer);
}
@Override
public void writeTo(ProtoStreamWriter writer, ByteBufferMarshalledValue<Object> key) throws IOException {
ByteBuffer buffer = key.getBuffer();
if (buffer != null) {
writer.writeBytes(BUFFER_INDEX, buffer);
}
}
@Override
public OptionalInt size(ProtoStreamSizeOperation operation, ByteBufferMarshalledValue<Object> value) {
if (value.isEmpty()) return OptionalInt.of(0);
OptionalInt size = value.size();
return size.isPresent() ? OptionalInt.of(operation.tagSize(BUFFER_INDEX, WireType.LENGTH_DELIMITED) + operation.varIntSize(size.getAsInt()) + size.getAsInt()) : OptionalInt.empty();
}
@SuppressWarnings("unchecked")
@Override
public Class<? extends ByteBufferMarshalledValue<Object>> getJavaClass() {
return (Class<ByteBufferMarshalledValue<Object>>) (Class<?>) ByteBufferMarshalledValue.class;
}
}
| 3,044
| 38.545455
| 189
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamMarshaller.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.ProtobufTagMarshaller;
/**
* A {@link ProtobufTagMarshaller} that include a facility for computing buffer sizes.
* @author Paul Ferraro
* @param <T> the type of this marshaller.
*/
public interface ProtoStreamMarshaller<T> extends ProtobufTagMarshaller<T>, Marshallable<T> {
@Override
default String getTypeName() {
Class<?> targetClass = this.getJavaClass();
Package targetPackage = targetClass.getPackage();
return (targetPackage != null) ? (targetPackage.getName() + '.' + targetClass.getSimpleName()) : targetClass.getSimpleName();
}
@Override
default T read(ReadContext context) throws IOException {
return this.readFrom(new DefaultProtoStreamReader(context));
}
@Override
default void write(WriteContext context, T value) throws IOException {
this.writeTo(new DefaultProtoStreamWriter(context), value);
}
}
| 2,041
| 37.528302
| 133
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/Reference.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.util.function.IntSupplier;
/**
* Encapsulates an object reference.
* @author Paul Ferraro
*/
public class Reference implements IntSupplier {
private final int reference;
public Reference(int reference) {
this.reference = reference;
}
@Override
public int getAsInt() {
return this.reference;
}
@Override
public int hashCode() {
return this.reference;
}
@Override
public boolean equals(Object object) {
return (object instanceof Reference) ? this.reference == ((Reference) object).reference : false;
}
@Override
public String toString() {
return Integer.toString(this.reference);
}
}
| 1,784
| 29.775862
| 104
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/AnyField.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.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.BitSet;
import java.util.IdentityHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.infinispan.protostream.descriptors.WireType;
import org.wildfly.security.manager.WildFlySecurityManager;
/**
* A set of fields used by {@link AnyMarshaller}.
* @author Paul Ferraro
*/
public enum AnyField implements Field<Object> {
ANY(Scalar.ANY), // For re-use by other fields
REFERENCE(Scalar.REFERENCE),
BOOLEAN(Scalar.BOOLEAN),
BYTE(Scalar.BYTE),
SHORT(Scalar.SHORT),
INTEGER(Scalar.INTEGER),
LONG(Scalar.LONG),
FLOAT(Scalar.FLOAT),
DOUBLE(Scalar.DOUBLE),
CHARACTER(Scalar.CHARACTER),
STRING(Scalar.STRING),
IDENTIFIED_OBJECT(new TypedObjectMarshaller(ScalarClass.ID)),
IDENTIFIED_ENUM(new TypedEnumMarshaller<>(ScalarClass.ID)),
IDENTIFIED_ARRAY(new TypedArrayMarshaller(ScalarClass.ID)),
FIELD_ARRAY(new TypedArrayMarshaller(ScalarClass.FIELD)),
// Prioritize the fields above: https://developers.google.com/protocol-buffers/docs/proto#assigning_field_numbers
NAMED_OBJECT(new TypedObjectMarshaller(ScalarClass.NAME)),
NAMED_ENUM(new TypedEnumMarshaller<>(ScalarClass.NAME)),
NAMED_ARRAY(new TypedArrayMarshaller(ScalarClass.NAME)),
BOOLEAN_ARRAY(new FieldMarshaller<boolean[]>() {
// Optimize using a BitSet, rather than a packed array of booleans
@Override
public boolean[] readFrom(ProtoStreamReader reader) throws IOException {
byte[] bytes = Scalar.BYTE_ARRAY.cast(byte[].class).readFrom(reader);
int length = bytes.length;
while (!reader.isAtEnd()) {
int tag = reader.readTag();
int index = WireType.getTagFieldNumber(tag);
if (index == INTEGER.getIndex()) {
// Adjust length of boolean[]
length = ((bytes.length - 1) * Byte.SIZE) + reader.readUInt32();
} else {
reader.skipField(tag);
}
}
BitSet set = BitSet.valueOf(bytes);
boolean[] values = new boolean[length];
for (int i = 0; i < length; ++i) {
values[i] = set.get(i);
}
return values;
}
@Override
public void writeTo(ProtoStreamWriter writer, boolean[] values) throws IOException {
int length = values.length;
// Pack boolean values into BitSet
BitSet set = new BitSet(length);
for (int i = 0; i < length; ++i) {
set.set(i, values[i]);
}
byte[] bytes = set.toByteArray();
Scalar.BYTE_ARRAY.cast(byte[].class).writeTo(writer, bytes);
// boolean[] length might be shorter than the BitSet, if so, write the difference
int remainder = length % Byte.SIZE;
if (remainder > 0) {
writer.writeUInt32(INTEGER.getIndex(), remainder);
}
}
@Override
public Class<? extends boolean[]> getJavaClass() {
return boolean[].class;
}
@Override
public WireType getWireType() {
return Scalar.BYTE_ARRAY.getWireType();
}
}),
BYTE_ARRAY(Scalar.BYTE_ARRAY),
SHORT_ARRAY(new PackedArrayMarshaller<>(Short.TYPE, Scalar.SHORT.cast(Short.class))),
INTEGER_ARRAY(new PackedArrayMarshaller<>(Integer.TYPE, Scalar.INTEGER.cast(Integer.class))),
LONG_ARRAY(new PackedArrayMarshaller<>(Long.TYPE, Scalar.LONG.cast(Long.class))),
FLOAT_ARRAY(new PackedArrayMarshaller<>(Float.TYPE, Scalar.FLOAT.cast(Float.class))),
DOUBLE_ARRAY(new PackedArrayMarshaller<>(Double.TYPE, Scalar.DOUBLE.cast(Double.class))),
CHAR_ARRAY(new PackedArrayMarshaller<>(Character.TYPE, Scalar.CHARACTER.cast(Character.class))),
ANY_ARRAY(new TypedArrayMarshaller(ScalarClass.ANY)),
PROXY(new FieldMarshaller<Object>() {
@Override
public Object readFrom(ProtoStreamReader reader) throws IOException {
InvocationHandler handler = (InvocationHandler) Scalar.ANY.readFrom(reader);
List<Class<?>> interfaces = new LinkedList<>();
while (!reader.isAtEnd()) {
int tag = reader.readTag();
int index = WireType.getTagFieldNumber(tag);
if (index == ANY.getIndex()) {
interfaces.add(ScalarClass.ANY.readFrom(reader));
} else {
reader.skipField(tag);
}
}
return Proxy.newProxyInstance(WildFlySecurityManager.getClassLoaderPrivileged(handler.getClass()), interfaces.toArray(new Class<?>[0]), handler);
}
@Override
public void writeTo(ProtoStreamWriter writer, Object proxy) throws IOException {
Scalar.ANY.writeTo(writer, Proxy.getInvocationHandler(proxy));
for (Class<?> interfaceClass : proxy.getClass().getInterfaces()) {
writer.writeTag(ANY.getIndex(), ScalarClass.ANY.getWireType());
ScalarClass.ANY.writeTo(writer, interfaceClass);
}
}
@Override
public Class<? extends Object> getJavaClass() {
return Object.class;
}
@Override
public WireType getWireType() {
return WireType.LENGTH_DELIMITED;
}
}),
;
private final FieldMarshaller<Object> marshaller;
AnyField(ScalarMarshaller<?> marshaller) {
this(new ScalarFieldMarshaller<>(marshaller));
}
@SuppressWarnings("unchecked")
AnyField(FieldMarshaller<?> marshaller) {
this.marshaller = (FieldMarshaller<Object>) marshaller;
}
@Override
public int getIndex() {
return this.ordinal() + 1;
}
@Override
public FieldMarshaller<Object> getMarshaller() {
return this.marshaller;
}
private static final AnyField[] VALUES = AnyField.values();
static AnyField fromIndex(int index) {
return (index > 0) && (index <= VALUES.length) ? VALUES[index - 1] : null;
}
private static final Map<Class<?>, AnyField> FIELDS = new IdentityHashMap<>();
static {
for (AnyField field : VALUES) {
Class<?> fieldClass = field.getMarshaller().getJavaClass();
if ((fieldClass != Object.class) && (fieldClass != Enum.class) && fieldClass != Class.class) {
FIELDS.put(fieldClass, field);
}
}
}
static AnyField fromJavaType(Class<?> targetClass) {
return FIELDS.get(targetClass);
}
}
| 7,799
| 38.393939
| 157
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/SimpleObjectInput.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.ObjectInput;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
/**
* {@link ObjectInput} implementation used to write the unexposed fields of an {@link java.io.Externalizable} object.
* @author Paul Ferraro
*/
public class SimpleObjectInput extends SimpleDataInput implements ObjectInput {
private final Iterator<Object> objects;
SimpleObjectInput(Builder builder) {
super(builder);
this.objects = builder.objects.iterator();
}
@Override
public Object readObject() {
return this.objects.next();
}
@Override
public int read() {
return this.readByte();
}
@Override
public int read(byte[] bytes) {
ByteBuffer buffer = this.nextBuffer();
int start = buffer.position();
return buffer.get(bytes).position() - start;
}
@Override
public int read(byte[] bytes, int offset, int length) {
ByteBuffer buffer = this.nextBuffer();
int start = buffer.position();
return buffer.get(bytes, offset, length).position() - start;
}
@Override
public long skip(long n) {
throw new UnsupportedOperationException();
}
@Override
public int available() {
throw new UnsupportedOperationException();
}
@Override
public void close() {
// Nothing to close
}
public static class Builder extends SimpleDataInput.Builder {
List<Object> objects = Collections.emptyList();
public Builder with(Object... values) {
this.objects = Arrays.asList(values);
return this;
}
@Override
public Builder with(String... values) {
super.with(values);
return this;
}
@Override
public Builder with(ByteBuffer... buffers) {
super.with(buffers);
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 ObjectInput build() {
return new SimpleObjectInput(this);
}
}
}
| 4,234
| 25.803797
| 117
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/EnumMarshaller.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;
/**
* ProtoStream marshaller for enums.
* @author Paul Ferraro
* @param <E> the enum type of this marshaller
*/
public class EnumMarshaller<E extends Enum<E>> implements org.infinispan.protostream.EnumMarshaller<E>, ProtoStreamMarshaller<E> {
// Optimize for singleton enums
private static final int DEFAULT_ORDINAL = 0;
private static final int ORDINAL_INDEX = 1;
private final Class<E> enumClass;
private final E[] values;
public EnumMarshaller(Class<E> enumClass) {
this.enumClass = enumClass;
this.values = enumClass.getEnumConstants();
}
@Override
public Class<? extends E> getJavaClass() {
return this.enumClass;
}
@Override
public E decode(int ordinal) {
return this.values[ordinal];
}
@Override
public int encode(E value) {
return value.ordinal();
}
@Override
public E readFrom(ProtoStreamReader reader) throws IOException {
E result = this.values[DEFAULT_ORDINAL];
while (!reader.isAtEnd()) {
int tag = reader.readTag();
switch (WireType.getTagFieldNumber(tag)) {
case ORDINAL_INDEX:
result = reader.readEnum(this.enumClass);
break;
default:
reader.skipField(tag);
}
}
return result;
}
@Override
public void writeTo(ProtoStreamWriter writer, E value) throws IOException {
if (value.ordinal() != DEFAULT_ORDINAL) {
writer.writeEnum(ORDINAL_INDEX, value);
}
}
}
| 2,764
| 31.151163
| 130
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/AbstractProtoStreamOperation.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.ImmutableSerializationContext;
import org.infinispan.protostream.ProtobufTagMarshaller.OperationContext;
import org.infinispan.protostream.impl.TagWriterImpl;
/**
* @author Paul Ferraro
*/
public abstract class AbstractProtoStreamOperation implements ProtoStreamOperation, OperationContext {
private final OperationContext context;
public AbstractProtoStreamOperation(ImmutableSerializationContext context) {
this(TagWriterImpl.newInstance(context));
}
public AbstractProtoStreamOperation(OperationContext context) {
this.context = context;
}
@Override
public ImmutableSerializationContext getSerializationContext() {
return this.context.getSerializationContext();
}
@Override
public Object getParam(Object key) {
return this.context.getParam(key);
}
@Override
public void setParam(Object key, Object value) {
this.context.setParam(key, value);
}
}
| 2,069
| 34.084746
| 102
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/DefaultSerializationContext.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.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import org.infinispan.protostream.BaseMarshaller;
import org.infinispan.protostream.DescriptorParserException;
import org.infinispan.protostream.FileDescriptorSource;
import org.infinispan.protostream.ImmutableSerializationContext;
import org.infinispan.protostream.ProtobufTagMarshaller;
import org.infinispan.protostream.SerializationContext;
import org.infinispan.protostream.config.Configuration;
import org.infinispan.protostream.descriptors.Descriptor;
import org.infinispan.protostream.descriptors.EnumDescriptor;
import org.infinispan.protostream.descriptors.FileDescriptor;
import org.infinispan.protostream.descriptors.GenericDescriptor;
import org.infinispan.protostream.impl.SerializationContextImpl;
/**
* Decorates {@link SerializationContextImpl}, ensuring that all registered marshallers implement {@link ProtoStreamMarshaller}.
* We have to use the decorator pattern since SerializationContextImpl is final.
* @author Paul Ferraro
*/
public class DefaultSerializationContext implements SerializationContext, Supplier<ImmutableSerializationContext> {
private final SerializationContext context = new SerializationContextImpl(Configuration.builder().build());
@Override
public ImmutableSerializationContext get() {
return this.context;
}
@Override
public Configuration getConfiguration() {
return this.context.getConfiguration();
}
@Override
public Map<String, FileDescriptor> getFileDescriptors() {
return this.context.getFileDescriptors();
}
@Override
public Map<String, GenericDescriptor> getGenericDescriptors() {
return this.context.getGenericDescriptors();
}
@Override
public Descriptor getMessageDescriptor(String fullTypeName) {
return this.context.getMessageDescriptor(fullTypeName);
}
@Override
public EnumDescriptor getEnumDescriptor(String fullTypeName) {
return this.context.getEnumDescriptor(fullTypeName);
}
@Override
public boolean canMarshall(Class<?> javaClass) {
return this.context.canMarshall(javaClass);
}
@Override
public boolean canMarshall(String fullTypeName) {
return this.context.canMarshall(fullTypeName);
}
@Override
public boolean canMarshall(Object object) {
return this.context.canMarshall(object);
}
@Override
public <T> BaseMarshaller<T> getMarshaller(T object) {
return this.context.getMarshaller(object);
}
@Override
public <T> BaseMarshaller<T> getMarshaller(String fullTypeName) {
return this.context.getMarshaller(fullTypeName);
}
@Override
public <T> BaseMarshaller<T> getMarshaller(Class<T> clazz) {
return this.context.getMarshaller(clazz);
}
@Deprecated
@Override
public String getTypeNameById(Integer typeId) {
return this.context.getTypeNameById(typeId);
}
@Deprecated
@Override
public Integer getTypeIdByName(String fullTypeName) {
return this.context.getTypeIdByName(fullTypeName);
}
@Override
public GenericDescriptor getDescriptorByTypeId(Integer typeId) {
return this.context.getDescriptorByTypeId(typeId);
}
@Override
public GenericDescriptor getDescriptorByName(String fullTypeName) {
return this.context.getDescriptorByName(fullTypeName);
}
@Override
public void registerProtoFiles(FileDescriptorSource source) throws DescriptorParserException {
this.context.registerProtoFiles(source);
}
@Override
public void unregisterProtoFile(String fileName) {
this.context.unregisterProtoFile(fileName);
}
@Override
public void unregisterProtoFiles(Set<String> fileNames) {
this.context.unregisterProtoFiles(fileNames);
}
@Override
public void registerMarshaller(BaseMarshaller<?> marshaller) {
this.context.registerMarshaller(this.adapt(marshaller));
}
@Override
public void unregisterMarshaller(BaseMarshaller<?> marshaller) {
this.context.unregisterMarshaller(marshaller);
}
@Deprecated
@Override
public void registerMarshallerProvider(MarshallerProvider provider) {
this.context.registerMarshallerProvider(this.adapt(provider));
}
@Deprecated
@Override
public void unregisterMarshallerProvider(MarshallerProvider provider) {
this.context.unregisterMarshallerProvider(provider);
}
@Override
public void registerMarshallerProvider(InstanceMarshallerProvider<?> provider) {
this.context.registerMarshallerProvider(this.adapt(provider));
}
@Override
public void unregisterMarshallerProvider(InstanceMarshallerProvider<?> provider) {
this.context.unregisterMarshallerProvider(provider);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
<T> BaseMarshaller<T> adapt(BaseMarshaller<T> marshaller) {
if (marshaller instanceof ProtoStreamMarshaller) {
return marshaller;
}
if (marshaller instanceof ProtobufTagMarshaller) {
return new ProtoStreamMarshallerAdapter<>((ProtobufTagMarshaller<T>) marshaller);
}
if (marshaller instanceof org.infinispan.protostream.EnumMarshaller) {
return new EnumMarshallerAdapter<>((org.infinispan.protostream.EnumMarshaller) marshaller);
}
throw new IllegalArgumentException(marshaller.getTypeName());
}
private <T> InstanceMarshallerProvider<T> adapt(InstanceMarshallerProvider<T> provider) {
return new InstanceMarshallerProvider<>() {
@Override
public Class<T> getJavaClass() {
return provider.getJavaClass();
}
@Override
public Set<String> getTypeNames() {
return provider.getTypeNames();
}
@Override
public String getTypeName(T instance) {
return provider.getTypeName(instance);
}
@Override
public BaseMarshaller<T> getMarshaller(T instance) {
BaseMarshaller<T> marshaller = provider.getMarshaller(instance);
return (marshaller != null) ? DefaultSerializationContext.this.adapt(marshaller) : null;
}
@Override
public BaseMarshaller<T> getMarshaller(String typeName) {
BaseMarshaller<T> marshaller = provider.getMarshaller(typeName);
return (marshaller != null) ? DefaultSerializationContext.this.adapt(marshaller) : null;
}
};
}
@Deprecated
private MarshallerProvider adapt(MarshallerProvider provider) {
return new MarshallerProvider() {
@Override
public BaseMarshaller<?> getMarshaller(String typeName) {
BaseMarshaller<?> marshaller = provider.getMarshaller(typeName);
return (marshaller != null) ? DefaultSerializationContext.this.adapt(marshaller) : null;
}
@Override
public BaseMarshaller<?> getMarshaller(Class<?> javaClass) {
BaseMarshaller<?> marshaller = provider.getMarshaller(javaClass);
return (marshaller != null) ? DefaultSerializationContext.this.adapt(marshaller) : null;
}
};
}
}
| 8,494
| 34.103306
| 128
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/SimpleFunctionalMarshaller.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.wildfly.common.function.ExceptionFunction;
/**
* Functional marshaller whose marshalled type is a subclass of the mapped marshaller.
* @author Paul Ferraro
* @param <T> the type of this marshaller
* @param <V> the type of the mapped marshaller
*/
public class SimpleFunctionalMarshaller<T extends V, V> extends FunctionalMarshaller<T, V> {
public SimpleFunctionalMarshaller(Class<T> targetClass, ProtoStreamMarshaller<V> marshaller, ExceptionFunction<V, T, IOException> factory) {
super(targetClass, marshaller, value -> value, factory);
}
}
| 1,686
| 40.146341
| 144
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/DefaultProtoStreamReader.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.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import org.infinispan.protostream.ProtobufTagMarshaller.ReadContext;
import org.infinispan.protostream.TagReader;
/**
* {@link ProtoStreamWriter} implementation that reads from a {@link TagReader}.
* @author Paul Ferraro
*/
public class DefaultProtoStreamReader extends AbstractProtoStreamOperation implements ProtoStreamReader, ReadContext {
interface ProtoStreamReaderContext extends ProtoStreamOperation.Context {
/**
* Resolves an object from the specified reference.
* @param reference an object reference
* @return the resolved object
*/
Object resolve(Reference reference);
}
private final TagReader reader;
private final ProtoStreamReaderContext context;
public DefaultProtoStreamReader(ReadContext context) {
this(context, new DefaultProtoStreamReaderContext());
}
private DefaultProtoStreamReader(ReadContext context, ProtoStreamReaderContext readerContext) {
super(context);
this.reader = context.getReader();
this.context = readerContext;
}
@Override
public Object readAny() throws IOException {
Object result = this.readObject(Any.class).get();
if (result instanceof Reference) {
Reference reference = (Reference) result;
result = this.context.resolve(reference);
} else {
this.context.record(result);
}
return result;
}
@Override
public Context getContext() {
return this.context;
}
@Override
public TagReader getReader() {
return this.reader;
}
@Override
public <T> T readObject(Class<T> targetClass) throws IOException {
int limit = this.reader.readUInt32();
int oldLimit = this.reader.pushLimit(limit);
try {
ProtoStreamMarshaller<T> marshaller = this.findMarshaller(targetClass);
T result = marshaller.readFrom(this);
// Ensure marshaller reached limit
this.reader.checkLastTagWas(0);
return result;
} finally {
this.reader.popLimit(oldLimit);
}
}
@Override
public int pushLimit(int limit) throws IOException {
return this.reader.pushLimit(limit);
}
@Override
public void popLimit(int oldLimit) {
this.reader.popLimit(oldLimit);
}
@Override
public boolean isAtEnd() throws IOException {
return this.reader.isAtEnd();
}
@Override
public int readTag() throws IOException {
return this.reader.readTag();
}
@Override
public void checkLastTagWas(int tag) throws IOException {
this.reader.checkLastTagWas(tag);
}
@Override
public boolean skipField(int tag) throws IOException {
return this.reader.skipField(tag);
}
@Override
public boolean readBool() throws IOException {
return this.reader.readBool();
}
@Override
public int readEnum() throws IOException {
return this.reader.readEnum();
}
@Deprecated
@Override
public int readInt32() throws IOException {
return this.reader.readInt32();
}
@Deprecated
@Override
public int readFixed32() throws IOException {
return this.reader.readFixed32();
}
@Override
public int readUInt32() throws IOException {
return this.reader.readUInt32();
}
@Override
public int readSInt32() throws IOException {
return this.reader.readSInt32();
}
@Override
public int readSFixed32() throws IOException {
return this.reader.readSFixed32();
}
@Deprecated
@Override
public long readInt64() throws IOException {
return this.reader.readInt64();
}
@Deprecated
@Override
public long readFixed64() throws IOException {
return this.reader.readFixed64();
}
@Override
public long readUInt64() throws IOException {
return this.reader.readUInt64();
}
@Override
public long readSInt64() throws IOException {
return this.reader.readSInt64();
}
@Override
public long readSFixed64() throws IOException {
return this.reader.readSFixed64();
}
@Override
public float readFloat() throws IOException {
return this.reader.readFloat();
}
@Override
public double readDouble() throws IOException {
return this.reader.readDouble();
}
@Override
public byte[] readByteArray() throws IOException {
return this.reader.readByteArray();
}
@Override
public ByteBuffer readByteBuffer() throws IOException {
return this.reader.readByteBuffer();
}
@Override
public String readString() throws IOException {
return this.reader.readString();
}
@Override
public byte[] fullBufferArray() throws IOException {
return this.reader.fullBufferArray();
}
@Override
public InputStream fullBufferInputStream() throws IOException {
return this.reader.fullBufferInputStream();
}
@Override
public boolean isInputStream() {
return this.reader.isInputStream();
}
private static class DefaultProtoStreamReaderContext implements ProtoStreamReaderContext {
private final Map<Object, Boolean> objects = new IdentityHashMap<>(128);
private final List<Object> references = new ArrayList<>(128);
@Override
public void record(Object object) {
if (object != null) {
if (this.objects.putIfAbsent(object, Boolean.TRUE) == null) {
this.references.add(object);
}
}
}
@Override
public Object resolve(Reference reference) {
return this.references.get(reference.getAsInt());
}
}
}
| 7,128
| 27.066929
| 118
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/SimpleFieldSetMarshaller.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.util.function.Function;
/**
* Marshaller for an object whose fields are marshalled via a {@link FieldSetMarshaller} whose construction is sufficiently simple as to not require a separate builder.
* @author Paul Ferraro
* @param <T> the type of this marshaller
*/
public class SimpleFieldSetMarshaller<T> extends FunctionalFieldSetMarshaller<T, T> {
@SuppressWarnings("unchecked")
public SimpleFieldSetMarshaller(FieldSetMarshaller<T, T> marshaller) {
this((Class<T>) marshaller.getBuilder().getClass(), marshaller);
}
public SimpleFieldSetMarshaller(Class<? extends T> targetClass, FieldSetMarshaller<T, T> marshaller) {
super(targetClass, marshaller, Function.identity());
}
}
| 1,814
| 41.209302
| 168
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/SizeComputingProtoStreamWriter.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 java.util.function.Function;
import java.util.function.Supplier;
import org.infinispan.protostream.impl.TagWriterImpl;
/**
* A {@link ProtoStreamWriter} implementation used to compute the number of bytes that would be written to a stream.
* @author Paul Ferraro
*/
public class SizeComputingProtoStreamWriter extends AbstractProtoStreamWriter implements Supplier<OptionalInt>, Function<Object, OptionalInt> {
private final TagWriterImpl writer;
private final ProtoStreamWriterContext context;
private boolean present = true;
public SizeComputingProtoStreamWriter(ProtoStreamSizeOperation operation, ProtoStreamWriterContext context) {
// Creates a TagWriter using a NoopEncoder
this(TagWriterImpl.newInstance(operation.getSerializationContext()), context);
}
private SizeComputingProtoStreamWriter(TagWriterImpl writer, ProtoStreamWriterContext writerContext) {
super(writer, writerContext);
this.writer = writer;
this.context = writerContext;
}
@Override
public ProtoStreamOperation.Context getContext() {
return this.context;
}
@Override
public OptionalInt get() {
return this.present ? OptionalInt.of(this.writer.getWrittenBytes()) : OptionalInt.empty();
}
@Override
public void writeObjectNoTag(Object value) throws IOException {
OptionalInt size = this.context.computeSize(value, this);
if (this.present && size.isPresent()) {
int length = size.getAsInt();
this.writeVarint32(length);
if (length > 0) {
this.writeRawBytes(null, 0, length);
}
} else {
this.present = false;
}
}
@Override
public OptionalInt apply(Object value) {
ProtoStreamMarshaller<Object> marshaller = this.findMarshaller(value.getClass());
return marshaller.size(new DefaultProtoStreamSizeOperation(this.getSerializationContext(), this.context), value);
}
}
| 3,147
| 36.927711
| 143
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamByteBufferMarshaller.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.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.lang.reflect.Proxy;
import java.util.OptionalInt;
import org.infinispan.protostream.ImmutableSerializationContext;
import org.infinispan.protostream.ProtobufTagMarshaller.ReadContext;
import org.infinispan.protostream.ProtobufTagMarshaller.WriteContext;
import org.infinispan.protostream.impl.TagReaderImpl;
import org.infinispan.protostream.impl.TagWriterImpl;
import org.wildfly.clustering.marshalling.spi.ByteBufferMarshaller;
/**
* @author Paul Ferraro
*/
public class ProtoStreamByteBufferMarshaller implements ByteBufferMarshaller {
private final ImmutableSerializationContext context;
public ProtoStreamByteBufferMarshaller(ImmutableSerializationContext context) {
this.context = context;
}
@Override
public OptionalInt size(Object object) {
ProtoStreamSizeOperation operation = new DefaultProtoStreamSizeOperation(this.context);
ProtoStreamMarshaller<Any> marshaller = operation.findMarshaller(Any.class);
return marshaller.size(operation, new Any(object));
}
@Override
public boolean isMarshallable(Object object) {
if ((object == null) || (object instanceof Class)) return true;
Class<?> targetClass = object.getClass();
if (AnyField.fromJavaType(targetClass) != null) return true;
if (targetClass.isArray()) {
for (int i = 0; i < Array.getLength(object); ++i) {
if (!this.isMarshallable(Array.get(object, i))) return false;
}
return true;
}
if (Proxy.isProxyClass(targetClass)) {
return this.isMarshallable(Proxy.getInvocationHandler(object));
}
while (targetClass != null) {
if (this.context.canMarshall(targetClass)) {
return true;
}
targetClass = targetClass.getSuperclass();
}
return false;
}
@Override
public Object readFrom(InputStream input) throws IOException {
ReadContext context = TagReaderImpl.newInstance(this.context, input);
ProtoStreamReader reader = new DefaultProtoStreamReader(context);
ProtoStreamMarshaller<Any> marshaller = reader.findMarshaller(Any.class);
return marshaller.readFrom(reader).get();
}
@Override
public void writeTo(OutputStream output, Object object) throws IOException {
WriteContext context = TagWriterImpl.newInstanceNoBuffer(this.context, output);
ProtoStreamWriter writer = new DefaultProtoStreamWriter(context);
ProtoStreamMarshaller<Any> marshaller = writer.findMarshaller(Any.class);
marshaller.writeTo(writer, new Any(object));
}
}
| 3,863
| 39.25
| 95
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/PackedArrayMarshaller.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 org.infinispan.protostream.descriptors.WireType;
/**
* Marshaller for packed repeated fields, e.g. primitive arrays.
* See https://developers.google.com/protocol-buffers/docs/encoding?hl=id#packed
* @author Paul Ferraro
* @param <T> the component type of this marshaller
*/
public class PackedArrayMarshaller<T> implements ScalarMarshaller<Object> {
private final Class<T> componentType;
private final ScalarMarshaller<T> element;
private final Class<? extends Object> arrayClass;
public PackedArrayMarshaller(Class<T> componentType, ScalarMarshaller<T> element) {
this.componentType = componentType;
this.element = element;
this.arrayClass = Array.newInstance(this.componentType, 0).getClass();
}
@Override
public Object readFrom(ProtoStreamReader reader) throws IOException {
int length = reader.readUInt32();
Object array = Array.newInstance(this.componentType, length);
for (int i = 0; i < length; ++i) {
Object element = this.element.readFrom(reader);
Array.set(array, i, element);
}
return array;
}
@Override
public void writeTo(ProtoStreamWriter writer, Object array) throws IOException {
int length = Array.getLength(array);
writer.writeVarint32(length);
for (int i = 0; i < length; ++i) {
@SuppressWarnings("unchecked")
T element = (T) Array.get(array, i);
this.element.writeTo(writer, element);
}
}
@Override
public Class<? extends Object> getJavaClass() {
return this.arrayClass;
}
@Override
public WireType getWireType() {
return WireType.LENGTH_DELIMITED;
}
}
| 2,874
| 34.9375
| 87
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/ProtoStreamOperation.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.ImmutableSerializationContext;
/**
* Common interface of {@link ProtoStreamReader} and {@link ProtoStreamWriter}.
* @author Paul Ferraro
*/
public interface ProtoStreamOperation {
interface Context {
/**
* Records the specified object, so that it can be referenced later within the same stream
* @param object an object
*/
void record(Object object);
}
/**
* Returns the context of this operation
* @return the operation context
*/
ProtoStreamOperation.Context getContext();
/**
* Returns the serialization context of the associated marshaller.
* @return an immutable serialization context
*/
ImmutableSerializationContext getSerializationContext();
/**
* Returns a marshaller suitable of marshalling an object of the specified type.
* @param <T> the type of the associated marshaller
* @param <V> the type of the object to be marshalled
* @param javaClass the type of the value to be written.
* @return a marshaller suitable for the specified type
* @throws IllegalArgumentException if no suitable marshaller exists
*/
@SuppressWarnings("unchecked")
default <T, V extends T> ProtoStreamMarshaller<T> findMarshaller(Class<V> javaClass) {
ImmutableSerializationContext context = this.getSerializationContext();
Class<?> targetClass = javaClass;
IllegalArgumentException exception = null;
while (targetClass != null) {
try {
return (ProtoStreamMarshaller<T>) context.getMarshaller((Class<T>) targetClass);
} catch (IllegalArgumentException e) {
// If no marshaller was found, check super class
if (exception == null) {
exception = e;
}
targetClass = targetClass.getSuperclass();
}
}
throw exception;
}
}
| 3,057
| 37.225
| 98
|
java
|
null |
wildfly-main/clustering/marshalling/protostream/src/main/java/org/wildfly/clustering/marshalling/protostream/SerializationContextInitializerProvider.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.SerializationContext;
import org.infinispan.protostream.SerializationContextInitializer;
/**
* @author Paul Ferraro
*/
public interface SerializationContextInitializerProvider extends SerializationContextInitializer {
SerializationContextInitializer getInitializer();
@Deprecated
@Override
default String getProtoFileName() {
return this.getInitializer().getProtoFileName();
}
@Deprecated
@Override
default String getProtoFile() {
return this.getInitializer().getProtoFile();
}
@Override
default void registerSchema(SerializationContext context) {
this.getInitializer().registerSchema(context);
}
@Override
default void registerMarshallers(SerializationContext context) {
this.getInitializer().registerMarshallers(context);
}
}
| 1,943
| 33.105263
| 98
|
java
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.