Generic Object Interning

Whipped together an nice little object interning facility:

class RefStore[T<:AnyRef]
{
	import scala.collection.mutable.HashSet
	var store = new HashSet[T]()

	def intern(ref: T): T = {
		store.findEntry(ref) match {
			case None => {
				store += ref
				// Added new shared ref, ref
				ref
			}
			case Some(r) => {
				// Returning shared ref, r
				r
			}
		}
	}
}

Dirt simple, generic, type safe.

Footnote

Posted by Stephen Strowes on Wednesday, October 28th, 2009. You can follow me on twitter.

Recent Posts

(full archive)

All content, including images, © Stephen D. Strowes, 2000–2016. Hosted by Digital Ocean.