public void testAllEventClassesHaveTheAppropriateConstructor() throws Exception { for (EventType eventType : EventType.values()) { if (eventType == EventType.UNKNOWN) { continue; } try { Class<? extends Event> clazz = eventType.getClazz(); clazz.getDeclaredConstructor(); } catch (NoSuchMethodException e) { fail(eventType.getClazz().getName() + " should implement a no-arg constructor that will " + "be used for deserialization."); } } }
return false; } else if (!eventType.equals(other.eventType)) { return false;
EventType type = EventType.valueOfIgnoreCase(eventTypeString); if (type == EventType.UNKNOWN) { throw new EventSerializationException("Trying to deserialize event JSON with unknown " + Class<? extends Event> clazz = type.getClazz(); Constructor<? extends Event> ctor; try {
/** * Converts a string into an {@link EventType} ignoring case in the process. * * @param name the name of the event type. * @return the converted event type. */ public static EventType valueOfIgnoreCase(String name) { try { return valueOf(name.toUpperCase()); } catch (IllegalArgumentException e) { return UNKNOWN; } }
result.addProperty(TYPE, event.getType().name()); result.addProperty(MODIFIED_BY, event.getModifiedBy()); result.addProperty(TIMESTAMP, event.getTimestamp());
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((contexts == null) ? 0 : contexts.hashCode()); result = prime * result + ((eventType == null) ? 0 : eventType.hashCode()); result = prime * result + ((filter == null) ? 0 : filter.pattern().hashCode()); return result; }
private void parseCapabilityTag(Element capability) { // Get the event type. EventType eventType = EventType.valueOfIgnoreCase( capability.getAttributeValue(CAPABILITY_NAME_ATTRIBUTE)); if (eventType == EventType.UNKNOWN) { return; } // Parse comma separated "context" attribute. List<Context> contexts; String contextsString = capability.getAttributeValue(CAPABILITY_CONTEXT_ATTRIBUTE); if (contextsString != null && !contextsString.isEmpty()) { try { contexts = Lists.newArrayList(); for (String context : contextsString.split(",")) { contexts.add(Context.valueOfIgnoreCase(context)); } } catch (IllegalArgumentException e) { contexts = Capability.DEFAULT_CONTEXT; } } else { contexts = Capability.DEFAULT_CONTEXT; } // Parse optional "filter" attribute. String filter = capability.getAttributeValue(CAPABILITY_FILTER_ATTRIBUTE); if (filter == null || filter.isEmpty()) { filter = ""; } this.capabilities.put(eventType, new Capability(eventType, contexts, filter)); }
private static Capability deserialize(ProtoRobotCapability data) { List<Context> contexts = Lists.newArrayList(); for (String str: data.getContextList()) { contexts.add(Context.valueOf(str)); } return new Capability(EventType.valueOf(data.getEventType()), contexts, data.getFilter()); } }
private DBObject capabilitiesToObject(RobotCapabilities capabilities) { if (capabilities == null) { return null; } BasicDBObject capabilitiesObj = new BasicDBObject(); for (Capability capability : capabilities.getCapabilitiesMap().values()) { BasicBSONList contexts = new BasicBSONList(); for (Context c : capability.getContexts()) { contexts.add(c.name()); } capabilitiesObj.put(capability.getEventType().name(), new BasicDBObject() .append(CAPABILITY_CONTEXTS_FIELD, contexts) .append(CAPABILITY_FILTER_FIELD, capability.getFilter())); } BasicDBObject object = new BasicDBObject() .append(CAPABILITIES_CAPABILITIES_FIELD, capabilitiesObj) .append(CAPABILITIES_HASH_FIELD, capabilities.getCapabilitiesHash()) .append(CAPABILITIES_VERSION_FIELD, capabilities.getProtocolVersion().name()); return object; }
public void testAllEventClassesHaveTheAppropriateConstructor() throws Exception { for (EventType eventType : EventType.values()) { if (eventType == EventType.UNKNOWN) { continue; } try { Class<? extends Event> clazz = eventType.getClazz(); clazz.getDeclaredConstructor(); } catch (NoSuchMethodException e) { fail(eventType.getClazz().getName() + " should implement a no-arg constructor that will " + "be used for deserialization."); } } }
@SuppressWarnings("unchecked") private RobotCapabilities objectToCapabilities(DBObject object) { if (object == null) { return null; } Map<String, Object> capabilitiesObj = (Map<String, Object>) object.get(CAPABILITIES_CAPABILITIES_FIELD); Map<EventType, Capability> capabilities = CollectionUtils.newHashMap(); for (Entry<String, Object> capability : capabilitiesObj.entrySet()) { EventType eventType = EventType.valueOf(capability.getKey()); List<Context> contexts = CollectionUtils.newArrayList(); DBObject capabilityObj = (DBObject) capability.getValue(); DBObject contextsObj = (DBObject) capabilityObj.get(CAPABILITY_CONTEXTS_FIELD); for (String contextId : contextsObj.keySet()) { contexts.add(Context.valueOf((String) contextsObj.get(contextId))); } String filter = (String) capabilityObj.get(CAPABILITY_FILTER_FIELD); capabilities.put(eventType, new Capability(eventType, contexts, filter)); } String capabilitiesHash = (String) object.get(CAPABILITIES_HASH_FIELD); ProtocolVersion version = ProtocolVersion.valueOf((String) object.get(CAPABILITIES_VERSION_FIELD)); return new RobotCapabilities(capabilities, capabilitiesHash, version); } }
private static ProtoRobotCapability serialize(Capability capability) { ProtoRobotCapability.Builder builder = ProtoRobotCapability.newBuilder(); builder.setEventType(capability.getEventType().name()); builder.setFilter(capability.getFilter()); for (Context context: capability.getContexts()) { builder.addContext(context.name()); } return builder.build(); }
/** * Checks whether the event should be filtered out. It can happen * if the robot received several deltas where in some delta it is added to * the wavelet but it didn't receive the WAVELET_SELF_ADDED event yet. * Or if robot already received WAVELET_SELF_REMOVED * event - then it should not receive events after that. * * @param event the event to filter. * @return true if the event should be filtered out */ protected boolean isEventFilteredOut(Event event) { boolean isEventSuspensionOveriden = false; if (event.getType().equals(EventType.WAVELET_SELF_REMOVED)) { // Stop processing events. isEventProcessingSuspended = true; // Allow robot receive WAVELET_SELF_REMOVED event, but suspend after that. isEventSuspensionOveriden = true; } if (event.getType().equals(EventType.WAVELET_SELF_ADDED)) { // Start processing events. isEventProcessingSuspended = false; } if ((isEventProcessingSuspended && !isEventSuspensionOveriden) || event.getModifiedBy().equals(robotName.toParticipantAddress())) { // Robot was removed from wave or this is self generated event. return true; } return false; }
public void testAllEventClassesHaveConversionStaticMethod() throws Exception { for (EventType eventType : EventType.values()) { if (eventType == EventType.UNKNOWN) { continue; } try { Method method = eventType.getClazz().getDeclaredMethod("as", Event.class); assertEquals(eventType.getClazz(), method.getReturnType()); } catch (NoSuchMethodException e) { fail(eventType.getClazz().getName() + " should implement a no-arg constructor that will " + "be used for deserialization."); } } }
public void testAllEventClassesHaveConversionStaticMethod() throws Exception { for (EventType eventType : EventType.values()) { if (eventType == EventType.UNKNOWN) { continue; } try { Method method = eventType.getClazz().getDeclaredMethod("as", Event.class); assertEquals(eventType.getClazz(), method.getReturnType()); } catch (NoSuchMethodException e) { fail(eventType.getClazz().getName() + " should implement a no-arg constructor that will " + "be used for deserialization."); } } }