/*
 * Copyright 2012-2015 JetBrains s.r.o
 *
 * 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 jetbrains.jetpad.model.collections;

import jetbrains.jetpad.model.event.ListenerEvent;

import java.util.Objects;

public class CollectionItemEvent<ItemT> implements ListenerEvent<CollectionListener<ItemT>> {
  public enum EventType {
    ADD, SET, REMOVE
  }
  private ItemT myOldItem;
  private ItemT myNewItem;
  private EventType myType;
  private int myIndex;

  public CollectionItemEvent(ItemT oldItem, ItemT newItem, int index, EventType type) {
    if (EventType.ADD.equals(type) && oldItem != null || EventType.REMOVE.equals(type) && newItem != null) {
      throw new IllegalStateException();
    }
    myOldItem = oldItem;
    myNewItem = newItem;
    myIndex = index;
    myType = type;
  }

  @Deprecated
  public CollectionItemEvent(ItemT item, int index, boolean isAdd) {
    if (isAdd) {
      myNewItem = item;
      myType = EventType.ADD;
    } else {
      myOldItem = item;
      myType = EventType.REMOVE;
    }
    myIndex = index;
  }

  /**
   * @deprecated  Use {@link #getOldItem()} or {@link #getNewItem()} instead.
   */
  @Deprecated
  public ItemT getItem() {
    if (EventType.ADD.equals(myType)) {
      return myNewItem;
    } else if (EventType.REMOVE.equals(myType)) {
      return myOldItem;
    } else {
      throw new UnsupportedOperationException("Events of this type do not have a designated item");
    }
  }

  public ItemT getOldItem() {
    return myOldItem;
  }

  public ItemT getNewItem() {
    return myNewItem;
  }

  public int getIndex() {
    return myIndex;
  }

  /**
   * @deprecated  Use {@link #getType()} instead.
   */
  @Deprecated
  public boolean isAdded() {
    return EventType.ADD.equals(myType);
  }

  public EventType getType() {
    return myType;
  }

  @Override
  public void dispatch(CollectionListener<ItemT> l) {
    if (EventType.ADD.equals(myType)) {
      l.onItemAdded(this);
    } else if (EventType.SET.equals(myType)) {
      l.onItemSet(this);
    } else {
      l.onItemRemoved(this);
    }
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    CollectionItemEvent that = (CollectionItemEvent) o;

    return Objects.equals(myOldItem, that.myOldItem) && Objects.equals(myNewItem, that.myNewItem) &&
           Objects.equals(myType, that.myType) && myIndex == that.myIndex;
  }

  @Override
  public int hashCode() {
    return Objects.hash(myOldItem, myNewItem, myType, myIndex);
  }

  @Override
  public String toString() {
    if (EventType.ADD.equals(myType)) {
      return myNewItem + " added at " + myIndex;
    } else if (EventType.SET.equals(myType)) {
      return myOldItem + " replaced with " + myNewItem + " at " + myIndex;
    } else {
      return myOldItem + " removed at " + myIndex;
    }
  }
}

@@ -1313,657 +1313,8 @@
  %7D%0A%0A
-  @Deprecated%0A  public CollectionItemEvent(ItemT item, int index, boolean isAdd) %7B%0A    if (isAdd) %7B%0A      myNewItem = item;%0A      myType = EventType.ADD;%0A    %7D else %7B%0A      myOldItem = item;%0A      myType = EventType.REMOVE;%0A    %7D%0A    myIndex = index;%0A  %7D%0A%0A  /**%0A   * @deprecated  Use %7B@link #getOldItem()%7D or %7B@link #getNewItem()%7D instead.%0A   */%0A  @Deprecated%0A  public ItemT getItem() %7B%0A    if (EventType.ADD.equals(myType)) %7B%0A      return myNewItem;%0A    %7D else if (EventType.REMOVE.equals(myType)) %7B%0A      return myOldItem;%0A    %7D else %7B%0A      throw new UnsupportedOperationException(%22Events of this type do not have a designated item%22);%0A    %7D%0A  %7D%0A%0A
   pu
@@ -1478,159 +1478,8 @@
  %7D%0A%0A
-  /**%0A   * @deprecated  Use %7B@link #getType()%7D instead.%0A   */%0A  @Deprecated%0A  public boolean isAdded() %7B%0A    return EventType.ADD.equals(myType);%0A  %7D%0A%0A
   pu

Remove deprecated methods from CollectionItemEvent