org.jsimpledb.change.SetFieldAdd Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jsimpledb-main Show documentation
Show all versions of jsimpledb-main Show documentation
JSimpleDB classes that map Java model classes onto the core API.
The newest version!
/*
* Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
*/
package org.jsimpledb.change;
import java.util.Objects;
import java.util.Set;
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 added to a set field.
*
* @param the type of the object containing the changed field
* @param the type of the changed set's elements
*/
public class SetFieldAdd 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 added
* @throws IllegalArgumentException if {@code jobj} or {@code fieldName} is null
*/
public SetFieldAdd(T jobj, int storageId, String fieldName, E element) {
super(jobj, storageId, fieldName);
this.element = element;
}
@Override
public R visit(ChangeSwitch target) {
return target.caseSetFieldAdd(this);
}
@Override
@SuppressWarnings("unchecked")
public void apply(JTransaction jtx, JObject jobj) {
((Set)jtx.readSetField(jobj.getObjId(), this.getStorageId(), false)).add(this.element);
}
/**
* Get the element that was added to the set.
*
* @return the newly added value
*/
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 SetFieldAdd, ?> that = (SetFieldAdd, ?>)obj;
return Objects.equals(this.element, that.element);
}
@Override
public int hashCode() {
return super.hashCode() ^ Objects.hashCode(this.element);
}
@Override
public String toString() {
return "SetFieldAdd[object=" + this.getObject() + ",field=\"" + this.getFieldName() + "\",element=" + this.element + "]";
}
}