use of com.google.protobuf.Message in project core-java by SpineEventEngine.
the class TypeConverterShould method checkMapping.
private static void checkMapping(Object javaObject, Message protoObject) {
final Any wrapped = AnyPacker.pack(protoObject);
final Object mappedJavaObject = TypeConverter.toObject(wrapped, javaObject.getClass());
assertEquals(javaObject, mappedJavaObject);
final Any restoredWrapped = TypeConverter.toAny(mappedJavaObject);
final Message restored = AnyPacker.unpack(restoredWrapped);
assertEquals(protoObject, restored);
}
use of com.google.protobuf.Message in project core-java by SpineEventEngine.
the class FieldMasks method applyMask.
/**
* Applies the given {@code FieldMask} to given collection of {@link Message}s.
* Does not change the {@link Collection} itself.
*
* <p>In case the {@code FieldMask} instance contains invalid field declarations, they are
* ignored and do not affect the execution result.
*
* @param mask {@code FieldMask} to apply to each item of the input {@link Collection}.
* @param messages {@link Message}s to filter.
* @param type type of the {@link Message}s.
* @return messages with the {@code FieldMask} applied
*/
@Nonnull
public static <M extends Message, B extends Message.Builder> Collection<M> applyMask(FieldMask mask, Collection<M> messages, TypeUrl type) {
checkNotNull(mask);
checkNotNull(messages);
checkNotNull(type);
final List<M> filtered = new LinkedList<>();
final ProtocolStringList filter = mask.getPathsList();
final Class<B> builderClass = getBuilderForType(type);
if (filter.isEmpty() || builderClass == null) {
return Collections.unmodifiableCollection(messages);
}
try {
final Constructor<B> builderConstructor = builderClass.getDeclaredConstructor();
builderConstructor.setAccessible(true);
for (Message wholeMessage : messages) {
final M message = messageForFilter(filter, builderConstructor, wholeMessage);
filtered.add(message);
}
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException | InstantiationException e) {
// If any reflection failure happens, return all the data without any mask applied.
log().warn(format(CONSTRUCTOR_INVOCATION_ERROR_LOGGING_PATTERN, builderClass.getCanonicalName()), e);
return Collections.unmodifiableCollection(messages);
}
return Collections.unmodifiableList(filtered);
}
use of com.google.protobuf.Message in project core-java by SpineEventEngine.
the class IdSetFunctions method put.
/**
* Puts a function into the map.
*
* @param eventClass the class of the event handled by the function
* @param func the function instance
* @param <E> the type of the event message
*/
public <E extends Message> void put(Class<E> eventClass, IdSetEventFunction<I, E> func) {
final EventClass clazz = EventClass.of(eventClass);
@SuppressWarnings("unchecked") final IdSetEventFunction<I, Message> // since we want to store {@code IdSetFunction}s for various event types.
casted = (IdSetEventFunction<I, Message>) func;
map.put(clazz, casted);
}
use of com.google.protobuf.Message in project core-java by SpineEventEngine.
the class IdSetFunctions method get.
/**
* Obtains a function for the passed event class.
*
* @param eventClass the class of the event message
* @param <E> the type of the event message
* @return the function wrapped into {@code Optional} or empty {@code Optional}
* if there is no matching function
*/
public <E extends Message> Optional<IdSetEventFunction<I, E>> get(Class<E> eventClass) {
final EventClass clazz = EventClass.of(eventClass);
final IdSetEventFunction<I, Message> func = map.get(clazz);
// we ensure the type when we put into the map.
@SuppressWarnings("unchecked") final IdSetEventFunction<I, E> result = (IdSetEventFunction<I, E>) func;
return Optional.fromNullable(result);
}
use of com.google.protobuf.Message in project core-java by SpineEventEngine.
the class EventException method getEventMessage.
/**
* Returns a related event message.
*/
public Message getEventMessage() {
if (eventMessage instanceof Any) {
final Any any = (Any) eventMessage;
Message unpacked = AnyPacker.unpack(any);
return unpacked;
}
return eventMessage;
}
Aggregations