Thread:
 Avoiding iteration 
 mxg75   22 Nov 2005, 16:48 
 Re: Avoiding iteration 
 crgardner   15 Aug 2006, 15:47 

Comment
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
From: mxg75 (22 Nov 2005, 16:48) Replies: 1, Views: 27199
Subject: Avoiding iteration
Rather then iterating through theEnumValues each time nullSafeGet, would
it make more sence to sort the array one time durring the constructor,
so theEnumValues[i].ordinal()==i?  My idea:

<code>
	protected IntEnumUserType(Class<E> c, E[] e) { 
		this.clazz = c; 
		this.theEnumValues = (E[]) new Enum[e.length];
		for(E value:e) {
			e[value.ordinal()]=value;
		}
	} 

	public Object nullSafeGet(ResultSet resultSet, String[] names, Object
owner) 
		throws HibernateException, SQLException { 
		final int val = resultSet.getShort(names[0]);
		if (!resultSet.wasNull() && 0<=val && val<theEnumValues.length)
			return theEnumValues[val];
		else
			return null; 
	} 
</code>

Or am I missing some gotcha?  Seems like this could really speed up the
load, especially if you have a large number of enum constants.
Prev. thread 
 Next thread
 
Prev. posting 
 Next posting
© Copyright 2006, Red Hat Middleware, LLC. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc. [Privacy Policy]