/*
 * Copyright (C) 2016 Swisscom All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
 * this file except in compliance with the License. You may obtain a copy of the
 * License at  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied.
 */

package gobblin.metrics.event;

import static gobblin.metrics.event.JobEvent.JOB_STATE;
import static gobblin.metrics.event.JobEvent.METADATA_JOB_COMPLETED_TASKS;
import static gobblin.metrics.event.JobEvent.METADATA_JOB_END_TIME;
import static gobblin.metrics.event.JobEvent.METADATA_JOB_LAUNCHED_TASKS;
import static gobblin.metrics.event.JobEvent.METADATA_JOB_START_TIME;
import static gobblin.metrics.event.JobEvent.METADATA_JOB_STATE;
import static gobblin.metrics.event.TaskEvent.METADATA_TASK_END_TIME;
import static gobblin.metrics.event.TaskEvent.METADATA_TASK_START_TIME;
import static gobblin.metrics.event.TaskEvent.METADATA_TASK_WORKING_STATE;
import static gobblin.metrics.event.TaskEvent.TASK_STATE;
import static gobblin.metrics.event.TimingEvent.METADATA_DURATION;
import static gobblin.metrics.event.TimingEvent.METADATA_END_TIME;
import static gobblin.metrics.event.TimingEvent.METADATA_START_TIME;
import static gobblin.metrics.event.TimingEvent.METADATA_TIMING_EVENT;

import java.util.Map;

import com.google.common.collect.Maps;

/**
 * Each of the metadata fields of a MultiPartEvent is reported separately
 * 
 * @author Lorand Bendig
 *
 */
public enum MultiPartEvent {
  
  TIMING_EVENT(
      METADATA_TIMING_EVENT,
      METADATA_START_TIME,
      METADATA_END_TIME,
      METADATA_DURATION),
  JOBSTATE_EVENT(
      JOB_STATE,
      METADATA_JOB_START_TIME,
      METADATA_JOB_END_TIME,
      METADATA_JOB_LAUNCHED_TASKS,
      METADATA_JOB_COMPLETED_TASKS,
      METADATA_JOB_STATE),
  TASKSTATE_EVENT(
      TASK_STATE,
      METADATA_TASK_START_TIME,
      METADATA_TASK_END_TIME,
      METADATA_TASK_WORKING_STATE);

  private String eventName;
  private String[] metadataFields;
  
  private static final Map<String, MultiPartEvent> lookup = Maps.newHashMap();
  static {
    for (MultiPartEvent event : MultiPartEvent.values()) {
      lookup.put(event.eventName, event);
    }
  }
     
  private MultiPartEvent(String eventName, String... metadataFields) {
    this.eventName = eventName;
    this.metadataFields = metadataFields;
  }
  
  public static MultiPartEvent getEvent(String eventName) {
    return lookup.get(eventName);
  }
  
  public String getEventName() {
    return eventName;
  }
  
  public String[] getMetadataFields() {
    return metadataFields;
  }
  
}

@@ -1543,20 +1543,28 @@
 collect.
+Immutable
 Map
-s
 ;%0A%0A/**%0A 
@@ -2283,40 +2283,132 @@
 okup
- = Maps.newHashMap();%0A  static %7B
+;%0A  static %7B%0A    ImmutableMap.Builder%3CString, MultiPartEvent%3E builder = new  ImmutableMap.Builder%3CString, MultiPartEvent%3E();
 %0A   
@@ -2469,22 +2469,23 @@
 %7B%0A      
-lookup
+builder
 .put(eve
@@ -2512,16 +2512,46 @@
 ;%0A    %7D%0A
+    lookup = builder.build();%0A
   %7D%0A    

Address comment in PR#863