Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setters for collection attributes and to-many associations #37

Closed
wants to merge 9 commits into from
44 changes: 44 additions & 0 deletions src/main/resources/org/fulib/templates/associations.bean.stg
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,47 @@ withoutItem(role, other) ::= <<
return this;
}
>>

setAllColl(role, other) ::= <<
<if(role.description)>
<setterJavaDoc(role)>
<endif>
public <role.clazz.name> set<role.name; format="cap">(import(java.util.Collection)\<? extends <other.clazz.name>\> value)
{
if (this.<role.name> == null)
{
this.<role.name> = new <collectionImpl(role, other)>();
}

if (value.equals(this.<role.name>))
{
return this;
}

for (final <other.clazz.name> oldItem : this.<role.name>)
{
if (!value.contains(oldItem))
{
<if(other.name)>
oldItem.<withoutThis(other)>;
<endif>
this.firePropertyChange(PROPERTY_<role.name>, oldItem, null);
}
}

for (final <other.clazz.name> newItem : value)
{
if (!this.<role.name>.contains(newItem))
{
<if(other.name)>
newItem.<withThis(other)>;
<endif>
this.firePropertyChange(PROPERTY_<role.name>, null, newItem);
}
}

this.<role.name>.clear();
this.<role.name>.addAll(value);
return this;
}
>>
22 changes: 22 additions & 0 deletions src/main/resources/org/fulib/templates/associations.javafx.stg
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,28 @@ withoutItem(role, other) ::= <<
}
>>

setAllArray(role, other) ::= <<
<if(role.description)>
<setterJavaDoc(role)>
<endif>
public <role.clazz.name> set<role.name; format="cap">(<other.clazz.name>... value)
{
this.<role.name>.setAll(value);
return this;
}
>>

setAllColl(role, other) ::= <<
<if(role.description)>
<setterJavaDoc(role)>
<endif>
public <role.clazz.name> set<role.name; format="cap">(import(java.util.Collection)\<? extends <other.clazz.name>\> value)
{
this.<role.name>.setAll(value);
return this;
}
>>

// --------------- Additional Templates ---------------

propertyMethod(role, other) ::= <<
Expand Down
31 changes: 31 additions & 0 deletions src/main/resources/org/fulib/templates/associations.pojo.stg
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ roleSignatures(role, other) ::= <<
withoutItem: class/<role.clazz.name>/property/without<role.name; format="cap">(<other.clazz.name>)
withoutArray: class/<role.clazz.name>/property/without<role.name; format="cap">(<other.clazz.name>...)
withoutColl: class/<role.clazz.name>/property/without<role.name; format="cap">(Collection\<? extends <other.clazz.name>\>)
setAllArray: class/<role.clazz.name>/property/set<role.name; format="cap">(<other.clazz.name>...)
setAllColl: class/<role.clazz.name>/property/set<role.name; format="cap">(Collection\<? extends <other.clazz.name>\>)
<else>
setMethod: class/<role.clazz.name>/property/set<role.name; format="cap">(<other.clazz.name>)
<endif>
Expand Down Expand Up @@ -196,6 +198,35 @@ withoutColl(role, other) ::= <<
}
>>

setAllArray(role, other) ::= <<
<if(role.description)>
<setterJavaDoc(role)>
<endif>
public <role.clazz.name> set<role.name; format="cap">(<other.clazz.name>... value)
{
return this.set<role.name; format="cap">(import(java.util.Arrays).asList(value));
}
>>

setAllColl(role, other) ::= <<
<if(role.description)>
<setterJavaDoc(role)>
<endif>
public <role.clazz.name> set<role.name; format="cap">(import(java.util.Collection)\<? extends <other.clazz.name>\> value)
{
if (this.<role.name> == null)
{
this.<role.name> = new <collectionImpl(role, other)>(value);
}
else
{
this.<role.name>.clear();
this.<role.name>.addAll(value);
}
return this;
}
>>

// --------------- Helpers ---------------

withoutThis(other) ::= <%
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/org/fulib/templates/attributes.bean.stg
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,19 @@ attrWithoutColl(attr) ::= <<
// --------------- Set All ---------------

attrSetAllArray(attr) ::= <<
<if(attr.description)>
<setterJavaDoc(attr)>
<endif>
public <attr.clazz.name> set<attr.name; format="cap">(<box(attr.type)>... value)
{
return this.set<attr.name; format="cap">(import(java.util.Arrays).asList(value));
}
>>

attrSetAllColl(attr) ::= <<
<if(attr.description)>
<setterJavaDoc(attr)>
<endif>
public <attr.clazz.name> set<attr.name; format="cap">(import(java.util.Collection)\<? extends <box(attr.type)>\> value)
{
if (this.<attr.name> == null)
Expand Down
22 changes: 22 additions & 0 deletions src/main/resources/org/fulib/templates/attributes.javafx.stg
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,25 @@ attrWithoutItem(attr) ::= <<
return this;
}
>>

attrSetAllArray(attr) ::= <<
<if(attr.description)>
<setterJavaDoc(attr)>
<endif>
public <attr.clazz.name> set<attr.name; format="cap">(<box(attr.type)>... value)
{
this.<attr.name>.setAll(value);
return this;
}
>>

attrSetAllColl(attr) ::= <<
<if(attr.description)>
<setterJavaDoc(attr)>
<endif>
public <attr.clazz.name> set<attr.name; format="cap">(import(java.util.Collection)\<? extends <box(attr.type)>\> value)
{
this.<attr.name>.setAll(value);
return this;
}
>>
8 changes: 8 additions & 0 deletions src/main/resources/org/fulib/templates/attributes.pojo.stg
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ attrSignatures(attr) ::= <<
attrWithoutItem: class/<attr.clazz.name>/property/without<attr.name; format="cap">(<box(attr.typeSignature)>)
attrWithoutArray: class/<attr.clazz.name>/property/without<attr.name; format="cap">(<box(attr.typeSignature)>...)
attrWithoutColl: class/<attr.clazz.name>/property/without<attr.name; format="cap">(Collection\<? extends <box(attr.typeSignature)>\>)
attrSetAllArray: class/<attr.clazz.name>/property/set<attr.name; format="cap">(<box(attr.typeSignature)>...)
attrSetAllColl: class/<attr.clazz.name>/property/set<attr.name; format="cap">(Collection\<? extends <box(attr.typeSignature)>\>)
<else>
attrSet: class/<attr.clazz.name>/property/set<attr.name; format="cap">(<attr.typeSignature>)
<endif>
Expand Down Expand Up @@ -165,13 +167,19 @@ attrWithoutColl(attr) ::= <<
// --------------- Set All ---------------

attrSetAllArray(attr) ::= <<
<if(attr.description)>
<setterJavaDoc(attr)>
<endif>
public <attr.clazz.name> set<attr.name; format="cap">(<box(attr.type)>... value)
{
return this.set<attr.name; format="cap">(import(java.util.Arrays).asList(value));
}
>>

attrSetAllColl(attr) ::= <<
<if(attr.description)>
<setterJavaDoc(attr)>
<endif>
public <attr.clazz.name> set<attr.name; format="cap">(import(java.util.Collection)\<? extends <box(attr.type)>\> value)
{
if (this.<attr.name> == null)
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/org/fulib/generator/MultiAttributeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;

public class MultiAttributeTest
{
Expand Down Expand Up @@ -69,6 +73,7 @@ public void testAttrList() throws Exception
final Method getMethod = rootClass.getMethod("getResultList");
final Method withItemMethod = rootClass.getMethod("withResultList", Integer.class);
final Method withoutItemMethod = rootClass.getMethod("withoutResultList", Integer.class);
final Method setItemsMethod = rootClass.getMethod("setResultList", Collection.class);

final Object theRoot = rootClass.newInstance();

Expand All @@ -85,6 +90,12 @@ public void testAttrList() throws Exception
withoutItemMethod.invoke(theRoot, 23);
assertThat("without removes all occurrences", theList.size(), equalTo(1));

setItemsMethod.invoke(theRoot, Arrays.asList(1, 2, 3, 4, 5));
assertThat("setter replaces old items", theList, equalTo(Arrays.asList(1, 2, 3, 4, 5)));

setItemsMethod.invoke(theRoot, Collections.emptyList());
assertThat("setter clears items", theList, empty());

// TODO test other "with" and "without" overloads
}

Expand Down