Added ClearFree() to DiddyPool and fixed FreeAll() to actually call Reset() on IPoolable instances.

--HG--
branch : containers

#Rem
Copyright (c) 2011 Steve Revill and Shane Woolcock
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#End

Strict
Private
Import diddy.containers
Import diddy.exception

Public
#Rem monkeydoc
	The DiddyPool class extends DiddyStack to provide functionality similar to the official Pool class.
	As with the other Diddy container classes, it simplifies mixing and matching of container types by sharing
	common method names.
#End
Class DiddyPool<T> Extends DiddyStack<T>
Private
	Global NIL:T
	Field freeObjects:DiddyStack<T> = New DiddyStack<T>

	Method CheckRange:Void(index:Int, low:Int=0, high:Int=-1)
		If high < 0 Then high = Self.Count()
		If index < low Or index >= high Then
			Throw New IndexOutOfBoundsException("DiddyPool.CheckRange: index " + index + " not in range " + low + " <= index < " + high)
		End
	End

Public
	Method New(initialCapacity:Int=0)
		Super.New()
		For Local i:Int = 0 Until initialCapacity
			freeObjects.Push(New T)
		Next
	End

	Method New(data:T[])
		Super.New(data)
	End

	Method New(src:Stack<T>)
		Super.New(src)
	End

	Method New(src:List<T>)
		Super.New(src)
	End

	Method New(src:Set<T>)
		Super.New(src)
	End

	Method Allocate:T(count:Int=1)
		Local obj:T = Null
		For Local i:Int = 0 Until count
			If freeObjects.IsEmpty() Then
				obj = New T
			Else
				obj = freeObjects.Pop()
			End
			Self.Push(obj)
		Next
		Return obj
	End

	Method Free:Void(val:T)
		If val Then
			Self.RemoveItem(val)
			freeObjects.Push(val)
			If IPoolable(val) Then IPoolable(val).Reset()
		End
	End

	Method FreeIndex:Void(index:Int)
#If CONFIG="debug" Then
		CheckRange(index)
#End
		Local val:T = Self.DeleteItem(index)
		If val Then
			freeObjects.Push(val)
			If IPoolable(val) Then IPoolable(val).Reset()
		End
	End

	Method FreeAll:Void()
		For Local obj:T = EachIn Self
			freeObjects.Push(obj)
			If IPoolable(obj) Then IPoolable(obj).Reset()
		Next
		Self.Clear()
	End

	Method FreeItems:IEnumerable<T>() Property
		Return New WrappedStackEnumerable<T>(freeObjects)
	End

	Method FreeCount:Int()
		Return freeObjects.Count()
	End

	Method ClearFree:Void()
		freeObjects.Clear()
	End
End

#Rem
Copyright (c) 2011 Steve Revill and Shane Woolcock
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#End

Strict
Private
Import diddy.containers
Import diddy.exception

Public
#Rem monkeydoc
	The DiddyPool class extends DiddyStack to provide functionality similar to the official Pool class.
	As with the other Diddy container classes, it simplifies mixing and matching of container types by sharing
	common method names.
#End
Class DiddyPool<T> Extends DiddyStack<T>
Private
	Global NIL:T
	Field freeObjects:DiddyStack<T> = New DiddyStack<T>

	Method CheckRange:Void(index:Int, low:Int=0, high:Int=-1)
		If high < 0 Then high = Self.Count()
		If index < low Or index >= high Then
			Throw New IndexOutOfBoundsException("DiddyPool.CheckRange: index " + index + " not in range " + low + " <= index < " + high)
		End
	End

Public
	Method New(initialCapacity:Int=0)
		Super.New()
		For Local i:Int = 0 Until initialCapacity
			freeObjects.Push(New T)
		Next
	End

	Method New(data:T[])
		Super.New(data)
	End

	Method New(src:Stack<T>)
		Super.New(src)
	End

	Method New(src:List<T>)
		Super.New(src)
	End

	Method New(src:Set<T>)
		Super.New(src)
	End

	Method Allocate:T(count:Int=1)
		Local obj:T = Null
		For Local i:Int = 0 Until count
			If freeObjects.IsEmpty() Then
				obj = New T
			Else
				obj = freeObjects.Pop()
			End
			Self.Push(obj)
		Next
		Return obj
	End

	Method Free:Void(val:T)
		If val Then
			Self.RemoveItem(val)
			freeObjects.Push(val)
			If IPoolable(val) Then IPoolable(val).Reset()
		End
	End

	Method FreeIndex:Void(index:Int)
#If CONFIG="debug" Then
		CheckRange(index)
#End
		Local val:T = Self.DeleteItem(index)
		If val Then
			freeObjects.Push(val)
			If IPoolable(val) Then IPoolable(val).Reset()
		End
	End

	Method FreeAll:Void()
		freeObjects.AddAll(Self)
		Self.Clear()
	End

	Method FreeItems:IEnumerable<T>() Property
		Return New WrappedStackEnumerable<T>(freeObjects)
	End

	Method FreeCount:Int()
		Return freeObjects.Count()
	End
End


Fixed null pointer when instantiating a user exception.

Strict

#REFLECTION_FILTER="diddy.exception"
Import reflection

Class DiddyException Extends Throwable
Private
	Field message:String
	Field cause:Throwable
	Field type:String
	Field fullType:String

Public
	Method Message:String() Property Final
		Return message
	End

	Method Message:Void(message:String) Property Final
		Self.message = message
	End

	Method Cause:Throwable() Property Final
		Return cause
	End

	Method Cause:Void(cause:Throwable) Property Final
		If cause = Self Then cause = Null
		Self.cause = cause
	End

	Method Type:String() Property Final
		Return type
	End

	Method FullType:String() Property Final
		Return fullType
	End

	Method New(message:String="", cause:Throwable=Null)
		Self.message = message
		Self.cause = cause
		Local ci:ClassInfo = GetClass(Self)
		If ci Then
			Self.fullType = ci.Name
		Else
			Self.fullType = "diddy.exception.DiddyException"
		End
		If Self.fullType.Contains(".") Then
			Self.type = Self.fullType[Self.fullType.FindLast(".")+1..]
		Else
			Self.type = Self.fullType
		End
	End

	Method ToString:String(recurse:Bool=False)
		Local rv:String = type+": "+message
		If recurse Then
			Local depth:Int = 10
			Local current:Throwable = cause
			While current And depth > 0
				If DiddyException(current) Then
					rv += "~nCaused by "+type+": "+DiddyException(current).message
					current = DiddyException(current).cause
					depth -= 1
				Else
					rv += "~nCaused by a non-Diddy exception."
					current = Null
				End
			End
		End
		Return rv
	End
End

''''''''''''''''''' EXCEPTIONS '''''''''''''''''''

Class AssertException Extends DiddyException
	Method New(message:String="", cause:Throwable=Null)
		Super.New(message, cause)
	End
End

Class ConcurrentModificationException Extends DiddyException
	Method New(message:String="", cause:Throwable=Null)
		Super.New(message, cause)
	End
End

Class IndexOutOfBoundsException Extends DiddyException
	Method New(message:String="", cause:Throwable=Null)
		Super.New(message, cause)
	End
End

Class IllegalArgumentException Extends DiddyException
	Method New(message:String="", cause:Throwable=Null)
		Super.New(message, cause)
	End
End

Class XMLParseException Extends DiddyException
	Method New(message:String="", cause:Throwable=Null)
		Super.New(message, cause)
	End
End

Strict

#REFLECTION_FILTER="diddy.exception"
Import reflection

Class DiddyException Extends Throwable
Private
	Field message:String
	Field cause:Throwable
	Field type:String
	Field fullType:String

Public
	Method Message:String() Property Final
		Return message
	End

	Method Message:Void(message:String) Property Final
		Self.message = message
	End

	Method Cause:Throwable() Property Final
		Return cause
	End

	Method Cause:Void(cause:Throwable) Property Final
		If cause = Self Then cause = Null
		Self.cause = cause
	End

	Method Type:String() Property Final
		Return type
	End

	Method FullType:String() Property Final
		Return fullType
	End

	Method New(message:String="", cause:Throwable=Null)
		Self.message = message
		Self.cause = cause
		Self.fullType = GetClass(Self).Name
		If Self.fullType.Contains(".") Then
			Self.type = Self.fullType[Self.fullType.FindLast(".")+1..]
		Else
			Self.type = Self.fullType
		End
	End

	Method ToString:String(recurse:Bool=False)
		Local rv:String = type+": "+message
		If recurse Then
			Local depth:Int = 10
			Local current:Throwable = cause
			While current And depth > 0
				If DiddyException(current) Then
					rv += "~nCaused by "+type+": "+DiddyException(current).message
					current = DiddyException(current).cause
					depth -= 1
				Else
					rv += "~nCaused by a non-Diddy exception."
					current = Null
				End
			End
		End
		Return rv
	End
End

''''''''''''''''''' EXCEPTIONS '''''''''''''''''''

Class AssertException Extends DiddyException
	Method New(message:String="", cause:Throwable=Null)
		Super.New(message, cause)
	End
End

Class ConcurrentModificationException Extends DiddyException
	Method New(message:String="", cause:Throwable=Null)
		Super.New(message, cause)
	End
End

Class IndexOutOfBoundsException Extends DiddyException
	Method New(message:String="", cause:Throwable=Null)
		Super.New(message, cause)
	End
End

Class IllegalArgumentException Extends DiddyException
	Method New(message:String="", cause:Throwable=Null)
		Super.New(message, cause)
	End
End

Class XMLParseException Extends DiddyException
	Method New(message:String="", cause:Throwable=Null)
		Super.New(message, cause)
	End
End