Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added groovy metaprogramming for collection classes
- Loading branch information
1 parent
031aafb
commit 5d7402f
Showing
12 changed files
with
169 additions
and
8 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
...in/groovy/groovy/runtime/metaclass/net/fortuna/ical4j/model/ComponentListMetaClass.groovy
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package groovy.runtime.metaclass.net.fortuna.ical4j.model | ||
|
||
class ComponentListMetaClass extends DelegatingMetaClass { | ||
|
||
ComponentListMetaClass(MetaClass delegate) { | ||
super(delegate) | ||
} | ||
|
||
ComponentListMetaClass(Class theClass) { | ||
super(theClass) | ||
} | ||
|
||
@Override | ||
Object getProperty(Object object, String property) { | ||
if (hasProperty(object, property)) { | ||
return super.getProperty(object, property) | ||
} | ||
return object.getComponents(property) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...in/groovy/groovy/runtime/metaclass/net/fortuna/ical4j/model/ParameterListMetaClass.groovy
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package groovy.runtime.metaclass.net.fortuna.ical4j.model | ||
|
||
class ParameterListMetaClass extends DelegatingMetaClass { | ||
|
||
ParameterListMetaClass(MetaClass delegate) { | ||
super(delegate) | ||
} | ||
|
||
ParameterListMetaClass(Class theClass) { | ||
super(theClass) | ||
} | ||
|
||
@Override | ||
Object getProperty(Object object, String property) { | ||
if (hasProperty(object, property)) { | ||
return super.getProperty(object, property) | ||
} | ||
return object.getParameters(property) | ||
} | ||
|
||
@Override | ||
void setProperty(Object object, String property, Object newValue) { | ||
if (hasProperty(object, property)) { | ||
super.setProperty(object, property, newValue) | ||
} | ||
object.replace(newValue) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ain/groovy/groovy/runtime/metaclass/net/fortuna/ical4j/model/PropertyListMetaClass.groovy
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package groovy.runtime.metaclass.net.fortuna.ical4j.model | ||
|
||
class PropertyListMetaClass extends DelegatingMetaClass { | ||
|
||
PropertyListMetaClass(MetaClass delegate) { | ||
super(delegate) | ||
} | ||
|
||
PropertyListMetaClass(Class theClass) { | ||
super(theClass) | ||
} | ||
|
||
@Override | ||
Object getProperty(Object object, String property) { | ||
if (hasProperty(object, property)) { | ||
return super.getProperty(object, property) | ||
} | ||
return object.getProperties(property) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/groovy/net/fortuna/ical4j/groovy/ParameterListExtension.groovy
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package net.fortuna.ical4j.groovy | ||
|
||
import net.fortuna.ical4j.model.Parameter | ||
import net.fortuna.ical4j.model.ParameterList | ||
|
||
class ParameterListExtension { | ||
|
||
static ParameterList leftShift(ParameterList self, Parameter parameter) { | ||
self.add(parameter) | ||
self | ||
} | ||
|
||
static ParameterList leftShift(ParameterList self, ParameterList list) { | ||
list.each {self.add(it)} | ||
self | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/groovy/net/fortuna/ical4j/groovy/PropertyListExtension.groovy
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package net.fortuna.ical4j.groovy | ||
|
||
import net.fortuna.ical4j.model.Property | ||
import net.fortuna.ical4j.model.PropertyList | ||
|
||
class PropertyListExtension { | ||
|
||
static PropertyList leftShift(PropertyList self, Property property) { | ||
self.add(property) | ||
self | ||
} | ||
|
||
static PropertyList leftShift(PropertyList self, PropertyList list) { | ||
list.each {self.add(it)} | ||
self | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/main/resources/META-INF/groovy/org.codehaus.groovy.runtime.ExtensionModule
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
moduleName = ical4j-module | ||
moduleVersion = ${project.version} | ||
extensionClasses = net.fortuna.ical4j.groovy.DurExtension,net.fortuna.ical4j.groovy.PeriodExtension | ||
extensionClasses = net.fortuna.ical4j.groovy.DurExtension,net.fortuna.ical4j.groovy.PeriodExtension,net.fortuna.ical4j.groovy.PropertyListExtension,net.fortuna.ical4j.groovy.ParameterListExtension |
29 changes: 29 additions & 0 deletions
29
src/test/groovy/net/fortuna/ical4j/model/ParameterListTest.groovy
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package net.fortuna.ical4j.model | ||
|
||
import net.fortuna.ical4j.model.parameter.Value | ||
import spock.lang.Specification | ||
|
||
class ParameterListTest extends Specification { | ||
|
||
def 'test list append with groovy left shift operator'() { | ||
given: 'a parameter list' | ||
ParameterList list = [] | ||
|
||
when: 'a parameter is appended with the left shift operator' | ||
list << Value.BINARY | ||
|
||
then: 'it is added to the list' | ||
list.value[0] == Value.BINARY | ||
} | ||
|
||
def 'test list concat with groovy left shift operator'() { | ||
given: 'a parameter list' | ||
ParameterList list = [] | ||
|
||
when: 'another parameter list is appended with the left shift operator' | ||
list << new ParameterList() << Value.BINARY | ||
|
||
then: 'it is added to the list' | ||
list.value[0] == Value.BINARY | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/test/groovy/net/fortuna/ical4j/model/PropertyListTest.groovy
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package net.fortuna.ical4j.model | ||
|
||
|
||
import net.fortuna.ical4j.model.property.CalScale | ||
import spock.lang.Specification | ||
|
||
class PropertyListTest extends Specification { | ||
|
||
def 'test list append with groovy left shift operator'() { | ||
given: 'a property list' | ||
PropertyList list = [] | ||
|
||
when: 'a property is appended with the left shift operator' | ||
list << CalScale.GREGORIAN | ||
|
||
then: 'it is added to the list' | ||
list.calscale[0] == CalScale.GREGORIAN | ||
} | ||
|
||
def 'test list concat with groovy left shift operator'() { | ||
given: 'a parameter list' | ||
PropertyList list = [] | ||
|
||
when: 'another property list is appended with the left shift operator' | ||
list << new PropertyList() << CalScale.GREGORIAN | ||
|
||
then: 'it is added to the list' | ||
list.calscale[0] == CalScale.GREGORIAN | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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