/*
 * Copyright 2014 the original author or authors.
 *
 * 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.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.kuujo.vertigo.cluster.data;

import net.kuujo.vertigo.util.serialization.JsonSerializable;

/**
 * Map event.
 *
 * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
 */
public class MapEvent<K, V> implements JsonSerializable {

  /**
   * Map event type.
   *
   * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
   */
  public static enum Type {

    /**
     * Occurs when a key is created.
     */
    CREATE("create"),

    /**
     * Occurs when a key is updated.
     */
    UPDATE("update"),

    /**
     * Occurs when a key is changed (created, updated, or deleted).
     */
    CHANGE("change"),

    /**
     * Occurs when a key is deleted.
     */
    DELETE("delete");

    private final String name;

    private Type(String name) {
      this.name = name;
    }

    @Override
    public String toString() {
      return name;
    }

    /**
     * Parses a string into a cluster event type.
     *
     * @param name The event type name.
     * @return The event type.
     */
    public static Type parse(String name) {
      switch (name) {
        case "create":
          return CREATE;
        case "update":
          return UPDATE;
        case "change":
          return CHANGE;
        case "delete":
          return DELETE;
        default:
          throw new IllegalArgumentException(name + " is not a valid event type.");
      }
    }

  }

  private Type type;
  private K key;
  private V value;

  public MapEvent(Type type, K key, V value) {
    this.type = type;
    this.key = key;
    this.value = value;
  }

  /**
   * Returns the event type.
   *
   * @return The map event type.
   */
  public Type type() {
    return type;
  }

  /**
   * Returns the key on which the event occurred.
   *
   * @return The key on which the event occurred.
   */
  public K key() {
    return (K) key;
  }

  /**
   * Returns the current key value.
   *
   * @return The current key value.
   */
  public V value() {
    return (V) value;
  }

}

@@ -2615,10 +2615,621 @@
 e;%0A  %7D%0A%0A
+  @Override%0A  public int hashCode() %7B%0A    int hashCode = 23;%0A    hashCode = 37 * hashCode + type.hashCode();%0A    hashCode = 37 * hashCode + key.hashCode();%0A    hashCode = 37 * hashCode + value.hashCode();%0A    return hashCode;%0A  %7D%0A%0A  @Override%0A  public String toString() %7B%0A    return String.format(%22MapEvent(%25s)%5B%25s:%25s%5D%22, type, key, value);%0A  %7D%0A%0A  @Override%0A  public boolean equals(Object object) %7B%0A    if (!(object instanceof MapEvent)) %7B%0A      return false;%0A    %7D%0A    MapEvent%3C?, ?%3E event = (MapEvent%3C?, ?%3E) object;%0A    return event.type.equals(type) && event.key.equals(key) && event.value.equals(value);%0A  %7D%0A%0A
 %7D%0A

Add hash support to MapEvent.