/*
 * Copyright (c) 2009-2013 Topi Nieminen
 */
package fi.tnie.db;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;

import fi.tnie.db.ent.Attribute;
import fi.tnie.db.ent.DataObject;
import fi.tnie.db.ent.Entity;
import fi.tnie.db.ent.value.PrimitiveKey;
import fi.tnie.db.rpc.PrimitiveHolder;
import fi.tnie.db.types.PrimitiveType;
import fi.tnie.db.types.ReferenceType;

public abstract class AbstractAttributeWriter<
	A extends Attribute,
	T extends ReferenceType<T, ?>,
	E extends Entity<A, ?, T, E, ?, ?, ?>,
	V extends Serializable,
	P extends PrimitiveType<P>,
	H extends PrimitiveHolder<V, P>,
	K extends PrimitiveKey<A, T, E, V, P, H, K>>
	implements AttributeWriter<A, T, E, V, P, H, K> {

	private K key;
	private int index;
	
		
	public AbstractAttributeWriter(K key, int index) {
		super();
		this.key = key;
		this.index = index;
	}

	public K getKey() {
		return key;
	}
	
	public int getIndex() {
		return index;
	}
	
	/**
	 * Returns <code>ph</code> as of type <code>H</code>.
	 *  
	 * @param ph
	 * @return
	 */
	protected abstract H as(PrimitiveHolder<?, ?> ph);
	
	@Override
	public H write(DataObject src, E dest) {
		PrimitiveHolder<?, ?> ph = src.get(getIndex());
		H h = as(ph);
		dest.set(getKey(), h);
		return h;
	}
	
	@Override
	public H write(ResultSet src, E dest) throws SQLException {
		PrimitiveHolder<?, ?> ph = extract(src);
		H h = as(ph);
		dest.set(getKey(), h);
		return h;
	}
	
	protected abstract H extract(ResultSet src) throws SQLException;
}

@@ -1229,32 +1229,147 @@
 %09%09H h = as(ph);%0A
+%09%09%0A%09%09if (h == null) %7B%0A%09%09%09throw new NullPointerException(ph.getClass().getName() + %22.as(ph) returned null%22);%0A%09%09%7D%0A%09%09%0A
 %09%09dest.set(getKe

Add null-pointer check