Skip to content

Commit

Permalink
Added groovy metaprogramming for collection classes
Browse files Browse the repository at this point in the history
  • Loading branch information
benfortuna committed Jul 28, 2021
1 parent 031aafb commit 5d7402f
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 8 deletions.
@@ -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)
}
}
@@ -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)
}
}
@@ -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)
}
}
@@ -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
}
}
@@ -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
}
}
@@ -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 src/test/groovy/net/fortuna/ical4j/model/ParameterListTest.groovy
@@ -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 src/test/groovy/net/fortuna/ical4j/model/PropertyListTest.groovy
@@ -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
}
}
Expand Up @@ -21,10 +21,10 @@ class ParticipantFactoryTest extends Specification {
}

then: 'result is as expected'
participant.getProperty('UID').value == '1'
participant.properties.uid[0].value == '1'
participant.locations.size() == 1
participant.locations[0].getProperty('UID').value == '11'
participant.locations[0].properties.uid[0].value == '11'
participant.resources.size() == 1
participant.resources[0].getProperty('UID').value == '12'
participant.resources[0].properties.uid[0].value == '12'
}
}
Expand Up @@ -18,8 +18,8 @@ class VAvailabilityFactoryTest extends Specification {
}

then: 'result is as expected'
availability.getProperty('UID').value == '1'
availability.properties.uid[0].value == '1'
availability.available.size() == 1
availability.available[0].getProperty('UID').value == '11'
availability.available[0].properties.uid[0].value == '11'
}
}
Expand Up @@ -15,6 +15,6 @@ class VLocationFactoryTest extends Specification {
}

then: 'result is as expected'
location.getProperty('UID').value == '1'
location.properties.uid[0].value == '1'
}
}
Expand Up @@ -15,6 +15,6 @@ class VResourceFactoryTest extends Specification {
}

then: 'result is as expected'
resource.getProperty('UID').value == '1'
resource.properties.uid[0].value == '1'
}
}

0 comments on commit 5d7402f

Please sign in to comment.