@Override public String toString() { Iterable<String> list; StringBuilder retString = new StringBuilder(); final String cr = System.getProperty("line.separator"); // $NON-NLS-1$ retString.append("Event type: " + fDeclaration.getName() + cr); // $NON-NLS-1$ retString.append("Timestamp: " + Long.toString(fTimestamp) + cr); // $NON-NLS-1$ if (fEventContext != null) { list = fEventContext.getDeclaration().getFieldsList(); for (String field : list) { retString.append( field + " : " + fEventContext.getDefinition(field).toString() + cr); // $NON-NLS-1$ } } if (fFields != null) { list = fFields.getDeclaration().getFieldsList(); for (String field : list) { retString.append( field + " : " + fFields.getDefinition(field).toString() + cr); // $NON-NLS-1$ } } return retString.toString(); }
/** * Gets the context of this event within a stream * * @return the context in struct form */ public StructDefinition getContext() { /* Most common case so far */ if (fStreamContext == null) { return fEventContext; } /* streamContext is not null, but the context of the event is null */ if (fEventContext == null) { return fStreamContext; } // TODO: cache if this is a performance issue /* The stream context and event context are assigned. */ StructDeclaration mergedDeclaration = new StructDeclaration(1); Builder<String> builder = ImmutableList.<String>builder(); List<Definition> fieldValues = new ArrayList<>(); /* Add fields from the stream */ for (String fieldName : fStreamContext.getFieldNames()) { Definition definition = fStreamContext.getDefinition(fieldName); mergedDeclaration.addField(fieldName, definition.getDeclaration()); builder.add(fieldName); fieldValues.add(definition); } ImmutableList<String> fieldNames = builder.build(); /* * Add fields from the event context, overwrite the stream ones if * needed. */ for (String fieldName : fEventContext.getFieldNames()) { Definition definition = fEventContext.getDefinition(fieldName); mergedDeclaration.addField(fieldName, definition.getDeclaration()); if (fieldNames.contains(fieldName)) { fieldValues.set((fieldNames.indexOf(fieldName)), definition); } else { builder.add(fieldName); fieldValues.add(definition); } } fieldNames = builder.build(); StructDefinition mergedContext = new StructDefinition( mergedDeclaration, this, "context", //$NON-NLS-1$ fieldNames, fieldValues.toArray(new Definition[fieldValues.size()])); return mergedContext; }