Newer
Older
/*
* _____________________________________________________________________________
*
* INAF - OATS National Institute for Astrophysics - Astronomical Observatory of
* Trieste INAF - IA2 Italian Center for Astronomical Archives
* _____________________________________________________________________________
*
* Copyright (C) 2016 Istituto Nazionale di Astrofisica
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License Version 3 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package it.inaf.ia2.tsm;
import it.inaf.ia2.tsm.model.PropertyModel;
import java.io.Serializable;
/**
* Store the value of an entity property (that corresponds to a column of the
* mapped table).
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
public class EntityProperty<T> implements Serializable {
private static final long serialVersionUID = 6735553751078589085L;
private PropertyModel propertyModel;
private Class<T> type;
private T originalValue;
private T value;
private boolean changed;
private EntityProperty() {
}
public EntityProperty(PropertyModel propertyModel, T defaultValue) {
this.propertyModel = propertyModel;
this.type = propertyModel.getType();
this.init(defaultValue);
}
/**
* Retrieve the current value.
*/
public T getValue() {
return value;
}
/**
* Retrieve the current value.
*/
public <X> X getValue(Class<X> type) {
return (X) value;
}
public T getOriginalValue() {
return originalValue;
}
public <X> X getOriginalValue(Class<X> type) {
return (X) originalValue;
}
public <X> void setValue(X value) {
this.setValue(value, true);
}
private <X> void setValue(X value, boolean checkUpdatable) {
if (checkUpdatable && !propertyModel.isUpdatable()) {
throw new UnsupportedOperationException("This EntityProperty instance (" + propertyModel.getName() + ") is not updatable");
}
if (value == null && !propertyModel.isNullable()) {
throw new IllegalArgumentException("This EntityProperty instance (" + propertyModel.getName() + ") doesn't allow null values");
}
this.value = (T) value;
}
public boolean isChanged() {
if (changed) {
return true;
}
if (originalValue == null) {
return value != null;
}
return !originalValue.equals(value);
}
public void setChanged(boolean changed) {
this.changed = changed;
}
/**
* Initialize the value.
*/
public final <X> void init(X initialValue) {
setValue(initialValue, false);
this.originalValue = (T) initialValue;
}
public void save() {
this.originalValue = value;
}
public Class getType() {
return type;
}