All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vaadin.collaborationengine.ListChangeEvent Maven / Gradle / Ivy

/*
 * Copyright 2020-2022 Vaadin Ltd.
 *
 * This program is available under Commercial Vaadin Runtime License 1.0
 * (CVRLv1).
 *
 * For the full License, see http://vaadin.com/license/cvrl-1
 */
package com.vaadin.collaborationengine;

import java.util.EventObject;
import java.util.Optional;
import java.util.function.Function;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;

/**
 * Event that is fired when the value in a collaboration list changes.
 *
 * @author Vaadin Ltd
 * @since 3.1
 */
public class ListChangeEvent extends EventObject {

    private final ListChange change;

    /**
     * Creates a new event.
     *
     * @param list
     *            the list source of the event
     * @param change
     *            the list change
     */
    ListChangeEvent(CollaborationList list, ListChange change) {
        super(list);
        this.change = change;
    }

    @Override
    public CollaborationList getSource() {
        return (CollaborationList) super.getSource();
    }

    ListChangeType getType() {
        return change.getType();
    }

    /**
     * Gets the key of the list item affected by the change.
     *
     * @return the key of the changed item, not null
     */
    public ListKey getKey() {
        return new ListKey(change.getKey());
    }

    /**
     * Gets the current value of the list item affected by the change as
     * instance of the given class.
     * 

* If the item was removed by the change, this method returns * null and {@link #getOldValue(Class)} return the removed item * value. * * @param * the type of the value from type parameter, e.g. * String * @param type * the expected type of the returned instance * @return the current value of the item affected by the change */ public T getValue(Class type) { return JsonUtil.toInstance(change.getValue(), type); } /** * Gets the current value of the list item affected by the change as * instance corresponding to the given type reference. *

* If the item was removed by the change, this method returns * null and {@link #getOldValue(TypeReference)} return the * removed item value. * * @param * the type reference of the value from type * parameter, e.g. List * @param type * the expected type reference of the returned instance * @return the current value of the item affected by the change */ public T getValue(TypeReference type) { return JsonUtil.toInstance(change.getValue(), type); } /** * Gets the old value of the list item affected by the change as instance of * the given class. * * @param * the type of the value from type parameter, e.g. * String * @param type * the expected type of the returned instance * @return the old value of the item affected by the change */ public T getOldValue(Class type) { return JsonUtil.toInstance(change.getOldValue(), type); } /** * Gets the old value of the list item affected by the change as instance * corresponding to the given type reference. * * @param * the type reference of the value from type * parameter, e.g. List * @param type * the expected type reference of the returned instance * @return the old value of the item affected by the change */ public T getOldValue(TypeReference type) { return JsonUtil.toInstance(change.getOldValue(), type); } /** * Gets the key of the item which is after the current item after the * change. * * @return the key of the item which is after the current item, or * null if there is none */ public ListKey getNext() { return ListKey.of(change.getNext()); } /** * Gets the key of the item which was after the current item before the * change. * * @return the key of the item which was after the current item, or * null if there was none */ public ListKey getOldNext() { return ListKey.of(change.getOldNext()); } /** * Gets the key of the item which is before the current item after the * change. * * @return the key of the item which is before the current item, or * null if there is none */ public ListKey getPrev() { return ListKey.of(change.getPrev()); } /** * Gets the key of the item which was before the current item before the * change. * * @return the key of the item which was before the current item, or * null if there was none */ public ListKey getOldPrev() { return ListKey.of(change.getOldPrev()); } /** * Gets the added item as instance of the given class. * * @param type * the class of the expected type of the returned instance * @param * the type of the class given as the type argument * @return the added item, or an empty optional if no item was added * * @deprecated This method is preserved for backwards compatibility from the * initial version when the only possible list change was to add * a new item to the end of the list. */ @Deprecated public Optional getAddedItem(Class type) { return convertAddedItem(JsonUtil.fromJsonConverter(type)); } /** * Gets the added item as instance of the given type reference. * * @param type * the expected type reference of the returned instance * @param * the type of the reference given as the type * argument * @return the added item, or an empty optional if no item was added * * @deprecated This method is preserved for backwards compatibility from the * initial version when the only possible list change was to add * a new item to the end of the list. */ @Deprecated public Optional getAddedItem(TypeReference type) { return convertAddedItem(JsonUtil.fromJsonConverter(type)); } private Optional convertAddedItem(Function converter) { if (change.getType() != ListChangeType.INSERT) { return Optional.empty(); } return Optional.ofNullable(change.getValue()).map(converter); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy