Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2011 Michael Ruflin, Andr´┐Ż Locher, Claudia von Bastian. This file is part of Tatool. Tatool is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Tatool 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with Tatool. If not, see <http://www.gnu.org/licenses/>. /
 
 package ch.tatool.core.data;
 
 
 
Generic property data object. You can use this class to define your own property objects without having to write too much.

Parameters:
<T> the type of the property value
Author(s):
Michael Ruflin
 
 public abstract class GenericProperty<T> implements Property<T> {
 
 	private static Logger logger = LoggerFactory.getLogger(ObjectProperty.class);
 	
 	private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
 	
 	private String propertyName;
 	private Class<T> propertyType;

Creates a new property object.
 
 	public GenericProperty(String propertyNameClass<T> propertyType) {
 		this. = propertyName;
 		this. = propertyType;
 
 	}
 	
 	public String getPropertyName() {
 		return ;
 	}
 	
 	public Class<?> getPropertyType() {
 		return ;
 	}
 	
 	// Checking whether set
 	
 	public boolean isSet(PropertyHolder holder) {
 		return holder.getProperty() != null;
 	}
 	
 	public boolean isSet(DataContainer dataContainerPropertyHolder propertyHolder) {
 		return isSet(dataContainerpropertyHolder.getId());
 	}
 	
 	public boolean isSet(DataContainer dataContainerString nodeId) {
 		return dataContainer.getValue(nodeId) != null;
 	}
 	
 	// Getting data
 	
 	public T getValue(PropertyHolder holder) {
 		return getValue(holdernull);
 	}
 	
 	@SuppressWarnings("unchecked")
 	public T getValue(PropertyHolder holder, T defaultValue) {
 		Object value = holder.getProperty();
 		if (value != null) {
 			if (.isInstance(value)) {
 				return (T) value;
 			} else {
 				.warn("Invalid property value for property " +  + ". Type " + .getName() +
 						" expected, but passed value of type " + value.getClass());
 			}
 		}
 		return defaultValue;
 	}
 	
 	public String getStringValue(DataContainer dataContainerPropertyHolder holder) {
 		return getStringValue(dataContainerholder.getId(), null);
	}
	public String getStringValue(DataContainer dataContainerString nodeId) {
		return getStringValue(dataContainernodeIdnull);
	}
	public String getStringValue(DataContainer dataContainerPropertyHolder holderString defaultValue) {
		return getStringValue(dataContainerholder.getId(), defaultValue);
	}
	public String getStringValue(DataContainer dataContainerString nodeIdString defaultValue) {
		String value = dataContainer.getValue(nodeId);
		return (value != null) ? value : defaultValue;
	}
	// Setting data
	public void setValue(PropertyHolder holder, T value) {
		holder.setProperty(value);
	}
	public void setValue(DataContainer dataContainerPropertyHolder holder) {
    	T value = getValue(holder);
    	setValue(dataContainerholdervalue);
    }
	public void setValue(DataContainer dataContainerPropertyHolder holder, T value) {
		setValue(dataContainerholder.getId(), value);
	}
	public void setValue(DataContainer dataContainerString nodeId, T value) {
		if (value != null) {
			if (value instanceof java.util.Date) {
				setStringValue(dataContainernodeId.format(value));
else {
				setStringValue(dataContainernodeIdvalue.toString());
			}
else {
			setStringValue(dataContainernodeId, (Stringnull);
		}
	}
	public void setStringValue(DataContainer dataContainerPropertyHolder holderString value) {
		setStringValue(dataContainerholder.getId(), value);
	}
	public void setStringValue(DataContainer dataContainerString nodeIdString value) {
		dataContainer.putValue(nodeIdvalue);
	}
	// Clearing data
	public void clearValue(PropertyHolder holder) {
		holder.setProperty(null);
	}
	public void clearValue(DataContainer dataContainerPropertyHolder holder) {
		clearValue(dataContainerholder.getId());
	}
	public void clearValue(DataContainer dataContainerString nodeId) {
		dataContainer.putValue(nodeIdnull);
	}
	// restoring
Default implementation throws an Exception.
	public T getValue(DataContainer dataContainerPropertyHolder holder) {
		return getValue(dataContainerholder.getId(), null);
	}

Default implementation throws an Exception.
	public T getValue(DataContainer dataContainerString nodeId) {
		return getValue(dataContainernodeIdnull);
	}
	public T getValue(DataContainer dataContainerPropertyHolder holder, T defaultValue) {
		return getValue(dataContainerholder.getId(), defaultValue);
	}
	@SuppressWarnings("unchecked")
	public T getValue(DataContainer dataContainerString nodeId, T defaultValue) {
		// check whether we have a converter
		if (! StringConverter.containsConverter()) {
			throw new UnsupportedOperationException("No converter for class " + .getName() + " available.");
		}
		String value = getStringValue(dataContainernodeId);
		if (value != null) {
			return (T) StringConverter.convert(value);
else {
			return defaultValue;
		}
	}
	public void restoreValue(DataContainer dataContainerPropertyHolder holder) {
		restoreValue(dataContainerholdernull);
	}

Restore a value from the dataContainer to the element.
	public void restoreValue(DataContainer dataContainerPropertyHolder holder, T defaultValue) {
value = getValue(dataContainerholderdefaultValue);
		setValue(holdervalue);
	}


Ensure a value, using whatever default should be used for this property.
	public T ensureValue(DataContainer dataContainerPropertyHolder holder) {
		return ensureValue(dataContainerholder.getId());
	}
Ensures a value, using the default value defined by this property. Note: by default this method throws an UnsupportedOperationException

	public T ensureValue(DataContainer dataContainerString nodeId) {
	}
	public T ensureValue(DataContainer dataContainerPropertyHolder holder, T defaultValue) {
		return ensureValue(dataContainerholder.getId(), defaultValue);
	}

Ensure a value is set. Sets the default value if not already available.

Returns:
the value currently set, will be the passed in defaultValue if it wasn't set previously
	public T ensureValue(DataContainer dataContainerString nodeId, T defaultValue) {
value = getValue(dataContainernodeId);
		if (value == null) {
			setValue(dataContainernodeIddefaultValue);
			return defaultValue;
else {
			return value;
		}
	}

Copy a property from one to another nodeId.

Parameters:
defaultValue the value to use if fromNode does not contain a value for given property. If defaultValue is null and fromNode does not contain a value then nothing will be copied.
	public boolean copyValue(DataContainer dataContainerNode fromNodeNode toNode, T defaultValue) {
		return copyValue(dataContainerfromNode.getId(), toNode.getId(), defaultValue);
	}
	public boolean copyValue(DataContainer dataContainerString fromIdString toId, T defaultValue) {
		String value = getStringValue(dataContainerfromIdnull);
    	if (value != null) {
    		setStringValue(dataContainertoIdvalue);
    		return true;
    	}
    	else if (defaultValue != null) {
    		setValue(dataContainertoIddefaultValue);
    		return true;
    	}
    	else {
    		return false;
    	}
	}
New to GrepCode? Check out our FAQ X