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

org.jsimpledb.change.SetFieldRemove Maven / Gradle / Ivy

The newest version!

/*
 * Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
 */

package org.jsimpledb.change;

import java.util.Objects;

import org.jsimpledb.JObject;
import org.jsimpledb.JTransaction;

/**
 * Notification object that gets passed to {@link org.jsimpledb.annotation.OnChange @OnChange}-annotated methods
 * when an element is removed from a set field.
 *
 * @param  the type of the object containing the changed field
 * @param  the type of the changed set's elements
 */
public class SetFieldRemove extends SetFieldChange {

    private final E element;

    /**
     * Constructor.
     *
     * @param jobj Java object containing the set field that changed
     * @param storageId the storage ID of the affected field
     * @param fieldName the name of the field that changed
     * @param element the element that was removed
     * @throws IllegalArgumentException if {@code jobj} or {@code fieldName} is null
     */
    public SetFieldRemove(T jobj, int storageId, String fieldName, E element) {
        super(jobj, storageId, fieldName);
        this.element = element;
    }

    @Override
    public  R visit(ChangeSwitch target) {
        return target.caseSetFieldRemove(this);
    }

    @Override
    public void apply(JTransaction jtx, JObject jobj) {
        jtx.readSetField(jobj.getObjId(), this.getStorageId(), false).remove(this.element);
    }

    /**
     * Get the element that was removed from the set.
     *
     * @return the value removed from the set
     */
    public E getElement() {
        return this.element;
    }

// Object

    @Override
    public boolean equals(Object obj) {
        if (obj == this)
            return true;
        if (!super.equals(obj))
            return false;
        final SetFieldRemove that = (SetFieldRemove)obj;
        return Objects.equals(this.element, that.element);
    }

    @Override
    public int hashCode() {
        return super.hashCode() ^ Objects.hashCode(this.element);
    }

    @Override
    public String toString() {
        return "SetFieldRemove[object=" + this.getObject() + ",field=\"" + this.getFieldName() + "\",element=" + this.element + "]";
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy