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 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 MatchFilter method checkFields.
private static boolean checkFields(Message object, FieldFilter filter) {
final Optional<Field> fieldOptional = Field.forFilter(object.getClass(), filter);
if (!fieldOptional.isPresent()) {
return false;
}
final Field field = fieldOptional.get();
final Optional<Message> value;
try {
value = field.getValue(object);
} catch (IllegalStateException ignored) {
// Wrong Message class -> does not satisfy the criteria.
return false;
}
final Collection<Any> expectedAnys = filter.getValueList();
final Collection<Message> expectedValues = Collections2.transform(expectedAnys, unpackFunc());
if (!value.isPresent()) {
/* If there is no value in the field return `true`
if the list of required values is also empty. */
final boolean nothingIsExpected = expectedValues.isEmpty();
return nothingIsExpected;
}
final boolean result = expectedValues.contains(value.get());
return result;
}
use of com.google.protobuf.Message in project core-java by SpineEventEngine.
the class Sample method messageValueFor.
private static Message messageValueFor(FieldDescriptor field) {
final TypeUrl messageType = TypeUrl.from(field.getMessageType());
final Class<? extends Message> javaClass = classFor(messageType);
final Message fieldValue = messageOfType(javaClass);
return fieldValue;
}
Aggregations