Esempio n. 1
0
  /**
   * 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;
  }