I added couple of methods, getBy and removeBy that I think will be helpful for working with collection of objects. Each takes a property and a value of that property as arguments, then we try to match those to one of the objects in the collection.
Now I need an Iterator for this.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfcomponent output="false"> | |
<cffunction name="init" access="public" returntype="Collection" output="false"> | |
<cfscript> | |
variables.container = arrayNew(1); | |
variables.currentIndex = 0; | |
return this; | |
</cfscript> | |
</cffunction> | |
<cffunction name="add" access="public" output="false" returntype="void"> | |
<cfargument name="Object" type="Any" required="true" /> | |
<cfscript> | |
arrayAppend(variables.container, arguments.object); | |
variables.currentIndex = this.count(); | |
</cfscript> | |
</cffunction> | |
<cffunction name="clear" access="public" returntype="Void" output="false"> | |
<cfscript> | |
variables.container = arrayNew(1); | |
variables.currentIndex = this.count(); | |
</cfscript> | |
</cffunction> | |
<cffunction name="count" access="public" output="false" returntype="Numeric"> | |
<cfreturn arrayLen(variables.container) /> | |
</cffunction> | |
<cffunction name="getAt" access="public" output="false" returntype="Any"> | |
<cfargument name="index" type="Numeric" required="true" /> | |
<cfreturn variables.container[arguments.index] /> | |
</cffunction> | |
<cffunction name="getBy" access="public" output="false" returntype="Any"> | |
<cfargument name="property" type="String" required="true" /> | |
<cfargument name="value" type="Any" required="true" /> | |
<cfscript> | |
var i = 0; | |
var o = ''; | |
for (i=1; i LTE count() ; i=i+1) { | |
o = variables.container[i]; | |
if(isObject(o) and o.hasProperty(arguments.property) and o.get(arguments.property) is arguments.value) { | |
return variables.container[i]; | |
} | |
} | |
return null; | |
</cfscript> | |
</cffunction> | |
<cffunction name="removeAt" access="public" output="false" returntype="boolean"> | |
<cfargument name="index" type="Numeric" required="true" /> | |
<cfscript> | |
if(arguments.index LTE this.count()) { | |
arrayDeleteAt(variables.container,arguments.index); | |
return true; | |
} else { | |
return false; | |
} | |
</cfscript> | |
</cffunction> | |
<cffunction name="removeBy" access="public" output="false" returntype="boolean"> | |
<cfargument name="property" type="String" required="true" /> | |
<cfargument name="value" type="Any" required="true" /> | |
<cfscript> | |
var i = 0; | |
var o = ''; | |
for (i=1; i LTE count() ; i=i+1) { | |
o = variables.container[i]; | |
if(isObject(o) and o.hasProperty(arguments.property) and o.get(arguments.property) is arguments.value) { | |
arrayDeleteAt(variables.container,i); | |
return true; | |
} | |
} | |
return false; | |
</cfscript> | |
</cffunction> | |
</cfcomponent> |
No comments:
Post a Comment