use of com.google.protobuf.Any in project core-java by SpineEventEngine.
the class TypeConverter method toAny.
/**
* Performs the {@link Object} to {@link Any} mapping.
*
* @param value the {@link Object} value to convert
* @param <T> the converted object type
* @return the packed value
*/
public static <T> Any toAny(T value) {
checkNotNull(value);
// Must be checked at runtime
@SuppressWarnings("unchecked") final Class<T> srcClass = (Class<T>) value.getClass();
final AnyCaster<T> caster = AnyCaster.forType(srcClass);
final Message message = caster.reverse().convert(value);
checkNotNull(message);
final Any result = AnyPacker.pack(message);
return result;
}
use of com.google.protobuf.Any in project core-java by SpineEventEngine.
the class Wrapper method pack.
public Any pack(T value) {
checkNotNull(value);
final W wrapper = doForward(value);
final Any result = AnyPacker.pack(wrapper);
return result;
}
use of com.google.protobuf.Any in project core-java by SpineEventEngine.
the class AnyPacker method pack.
/**
* Wraps {@link Message} object inside of {@link Any} instance.
*
* <p>If an instance of {@code Any} passed, this instance is returned.
*
* @param message the message to pack
* @return the wrapping instance of {@link Any} or the message itself, if it is {@code Any}
*/
public static Any pack(Message message) {
if (message instanceof Any) {
return (Any) message;
}
final TypeUrl typeUrl = TypeUrl.from(message.getDescriptorForType());
final String typeUrlPrefix = typeUrl.getPrefix();
final Any result = Any.pack(message, typeUrlPrefix);
return result;
}
use of com.google.protobuf.Any in project core-java by SpineEventEngine.
the class Attribute method setValue.
/**
* Sets the value of the attribute in the passed builder.
*/
public final void setValue(B builder, T value) {
final Map<String, Any> map = getMutableMap(builder);
final Any packed = this.pack(value);
map.put(getName(), packed);
}
use of com.google.protobuf.Any in project core-java by SpineEventEngine.
the class Aggregate method toSnapshot.
/**
* Transforms the current state of the aggregate into the {@link Snapshot} instance.
*
* @return new snapshot
*/
@CheckReturnValue
Snapshot toSnapshot() {
final Any state = AnyPacker.pack(getState());
final Snapshot.Builder builder = Snapshot.newBuilder().setState(state).setVersion(getVersion()).setTimestamp(getCurrentTime());
return builder.build();
}
Aggregations