Skip to content

Commit

Permalink
Merge pull request #8 from spdx/issue7
Browse files Browse the repository at this point in the history
Add support for double property values
  • Loading branch information
goneall authored Jan 9, 2025
2 parents 64da23a + fff458c commit c98180f
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 20 deletions.
27 changes: 27 additions & 0 deletions resources/javaTemplates/ExternalJavaClassTemplate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,33 @@ public class External{{{className}}} extends {{{className}}} implements Individ
throw new InvalidSPDXAnalysisException("External elements can not set properties");
}
{{/integerProperties}}
{{#doubleProperties}}
{{#nonOptional}}
/**
* @return the {{{propertyName}}}
*/
public @Nullable Double {{{getter}}}() throws InvalidSPDXAnalysisException {
return null;
}
{{/nonOptional}}
{{^nonOptional}}
/**
* @return the {{{propertyName}}}
*/
public Optional<{{{type}}}> {{{getter}}}() throws InvalidSPDXAnalysisException {
return Optional.empty();
}
{{/nonOptional}}

/**
* @param {{{propertyName}}} the {{{propertyName}}} to set
* @return this to chain setters
* @throws InvalidSPDXAnalysisException
*/
public {{{className}}} {{{setter}}}(@Nullable {{{type}}} {{{propertyName}}}) throws InvalidSPDXAnalysisException {
throw new InvalidSPDXAnalysisException("External elements can not set properties");
}
{{/doubleProperties}}
{{#stringProperties}}
{{#nonOptional}}
/**
Expand Down
27 changes: 27 additions & 0 deletions resources/javaTemplates/IndividualClassTemplate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,33 @@ public class {{{className}}} extends {{{superClass}}} implements IndividualUriVa
throw new InvalidSPDXAnalysisException("Can not set or modify a property of an Individual type");
}
{{/integerProperties}}
{{#doubleProperties}}
{{#nonOptional}}
/**
* @return the {{{propertyName}}}
*/
public @Nullable Double {{{getter}}}() throws InvalidSPDXAnalysisException {
return null;
}
{{/nonOptional}}
{{^nonOptional}}
/**
* @return the {{{propertyName}}}
*/
public Optional<{{{type}}}> {{{getter}}}() throws InvalidSPDXAnalysisException {
return Optional.empty();
}
{{/nonOptional}}

/**
* @param {{{propertyName}}} the {{{propertyName}}} to set
* @return this to chain setters
* @throws InvalidSPDXAnalysisException
*/
public {{{className}}} {{{setter}}}(@Nullable {{{type}}} {{{propertyName}}}) throws InvalidSPDXAnalysisException {
throw new InvalidSPDXAnalysisException("Can not set or modify a property of an Individual type");
}
{{/doubleProperties}}
{{#stringProperties}}
{{#nonOptional}}
/**
Expand Down
142 changes: 142 additions & 0 deletions resources/javaTemplates/JavaClassTemplate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,18 @@ public {{#abstract}}abstract{{/abstract}} class {{{className}}} extends {{{super
{{/nonOptional}}
{{/superSetter}}
{{/integerProperties}}
{{#doubleProperties}}
{{^superSetter}}
{{#nonOptional}}
if (Objects.nonNull(builder.{{{propertyName}}})) {
{{{setter}}}(builder.{{{propertyName}}});
}
{{/nonOptional}}
{{^nonOptional}}
{{{setter}}}(builder.{{{propertyName}}});
{{/nonOptional}}
{{/superSetter}}
{{/doubleProperties}}
{{#stringProperties}}
{{^superSetter}}
{{#nonOptional}}
Expand Down Expand Up @@ -753,6 +765,67 @@ public {{#abstract}}abstract{{/abstract}} class {{{className}}} extends {{{super
}
{{/superSetter}}
{{/integerProperties}}
{{#doubleProperties}}
{{^superSetter}}
{{#nonOptional}}
/**
* @return the {{{propertyName}}}
*/
public @Nullable Double {{{getter}}}() throws InvalidSPDXAnalysisException {
Optional<Double> retval = getDoublePropertyValue(SpdxConstantsV3.{{{propertyConstant}}});
return retval.isPresent() ? retval.get() : null;
}
{{/nonOptional}}
{{^nonOptional}}
/**
* @return the {{{propertyName}}}
*/
public Optional<{{{type}}}> {{{getter}}}() throws InvalidSPDXAnalysisException {
return getDoublePropertyValue(SpdxConstantsV3.{{{propertyConstant}}});
}
{{/nonOptional}}

/**
* @param {{{propertyName}}} the {{{propertyName}}} to set
* @return this to chain setters
* @throws InvalidSPDXAnalysisException
*/
public {{{className}}} {{{setter}}}(@Nullable {{{type}}} {{{propertyName}}}) throws InvalidSPDXAnalysisException {
{{#required}}
if (isStrict() && Objects.isNull({{{propertyName}}})) {
throw new InvalidSPDXAnalysisException("{{{propertyName}}} is a required property");
}
{{/required}}
{{#min}}
if (isStrict() && Objects.nonNull({{{propertyName}}}) && {{{propertyName}}} < {{{min}}}) {
throw new InvalidSPDXAnalysisException("{{{propertyName}}} value " + {{{propertyName}}} + " is less than the minimum {{{min}}} in {{{className}}}");
}
{{/min}}
{{#max}}
if (isStrict() && Objects.nonNull({{{propertyName}}}) && {{{propertyName}}} > {{{max}}}) {
throw new InvalidSPDXAnalysisException("{{{propertyName}}} value " + {{{propertyName}}} + " is greater than the maximum {{{max}}} in {{{className}}}");
}
{{/max}}
{{#superSetter}}
super.{{{setter}}}({{{propertyName}}});
{{/superSetter}}
setPropertyValue(SpdxConstantsV3.{{{propertyConstant}}}, {{{propertyName}}});
return this;
}
{{/superSetter}}
{{#superSetter}}
/**
* @param {{{propertyName}}} the {{{propertyName}}} to set
* @return this to chain setters
* @throws InvalidSPDXAnalysisException
*/
@Override
public {{{className}}} {{{setter}}}(@Nullable {{{type}}} {{{propertyName}}}) throws InvalidSPDXAnalysisException {
super.{{{setter}}}({{{propertyName}}});
return this;
}
{{/superSetter}}
{{/doubleProperties}}
{{#stringProperties}}
{{^superSetter}}
{{#nonOptional}}
Expand Down Expand Up @@ -1111,6 +1184,58 @@ public {{#abstract}}abstract{{/abstract}} class {{{className}}} extends {{{super
}
{{/superSetter}}
{{/integerProperties}}
{{#doubleProperties}}
{{^superSetter}}
try {
{{#nonOptional}}
{{^hasConstraint}}
@SuppressWarnings("unused")
{{/hasConstraint}}
{{{type}}} {{{propertyName}}} = {{{getter}}}();
{{#required}}
if (Objects.isNull({{{propertyName}}}) &&
Collections.disjoint(profiles, Arrays.asList(new ProfileIdentifierType[] { {{{requiredProfiles}}} }))) {
retval.add("Missing {{{propertyName}}} in {{{className}}}");
}
{{/required}}
{{#min}}
if (Objects.nonNull({{{propertyName}}}) && {{{propertyName}}} < {{{min}}}) {
retval.add("{{{propertyName}}} value " + {{{propertyName}}} + " is less than the minimum {{{min}}} in {{{className}}}");
}
{{/min}}
{{#max}}
if (Objects.nonNull({{{propertyName}}}) && {{{propertyName}}} > {{{max}}}) {
retval.add("{{{propertyName}}} value " + {{{propertyName}}} + " is greater than the maximum {{{max}}} in {{{className}}}");
}
{{/max}}
{{/nonOptional}}
{{^nonOptional}}
{{^hasConstraint}}
@SuppressWarnings("unused")
{{/hasConstraint}}
Optional<{{{type}}}> {{{propertyName}}} = {{{getter}}}();
{{#required}}
if (!{{{propertyName}}}.isPresent() &&
Collections.disjoint(profiles, Arrays.asList(new ProfileIdentifierType[] { {{{requiredProfiles}}} }))) {
retval.add("Missing {{{propertyName}}} in {{{className}}}");
}
{{/required}}
{{#min}}
if ({{{propertyName}}}.isPresent() && {{{propertyName}}}.get() < {{{min}}}) {
retval.add("{{{propertyName}}} value " + {{{propertyName}}}.get() + " is less than the minimum {{{min}}} in {{{className}}}");
}
{{/min}}
{{#max}}
if ({{{propertyName}}}.isPresent() && {{{propertyName}}}.get() > {{{max}}}) {
retval.add("{{{propertyName}}} value " + {{{propertyName}}}.get() + " is greater than the maximum {{{max}}} in {{{className}}}");
}
{{/max}}
{{/nonOptional}}
} catch (InvalidSPDXAnalysisException e) {
retval.add("Error getting {{{propertyName}}} for {{{className}}}: "+e.getMessage());
}
{{/superSetter}}
{{/doubleProperties}}
{{#stringProperties}}
{{^superSetter}}
try {
Expand Down Expand Up @@ -1330,6 +1455,11 @@ public {{#abstract}}abstract{{/abstract}} class {{{className}}} extends {{{super
protected Integer {{{propertyName}}} = null;
{{/superSetter}}
{{/integerProperties}}
{{#doubleProperties}}
{{^superSetter}}
protected Double {{{propertyName}}} = null;
{{/superSetter}}
{{/doubleProperties}}
{{#stringProperties}}
{{^superSetter}}
protected String {{{propertyName}}} = null;
Expand Down Expand Up @@ -1536,6 +1666,18 @@ public {{#abstract}}abstract{{/abstract}} class {{{className}}} extends {{{super
return this;
}
{{/integerProperties}}
{{#doubleProperties}}

/**
* Sets the initial value of {{{propertyName}}}
* @parameter {{{propertyName}}} value to set
* @return this for chaining
**/
public {{{className}}}Builder {{{setter}}}({{{type}}} {{{propertyName}}}) {
{{#superSetter}}super.{{/superSetter}}{{^superSetter}}this.{{/superSetter}}{{{propertyName}}} = {{{propertyName}}};
return this;
}
{{/doubleProperties}}
{{#stringProperties}}

/**
Expand Down
4 changes: 2 additions & 2 deletions resources/javaTemplates/PomTemplate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.spdx</groupId>
<artifactId>spdx-java-model-3_0</artifactId>
<version>1.0.0-RC1</version>
<version>1.0.0-RC2</version>
<name>spdx-java-model-3</name>
<description>Generated java model source code</description>
<url>https://github.com/spdx/spdx-java-model-3_0</url>
Expand Down Expand Up @@ -116,7 +116,7 @@
<dependency>
<groupId>org.spdx</groupId>
<artifactId>spdx-java-core</artifactId>
<version>0.1.0-Alpha</version>
<version>1.0.0-RC2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down
18 changes: 18 additions & 0 deletions resources/javaTemplates/TestValuesGeneratorTemplate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,18 @@ public class TestValuesGenerator {
return this;
}
{{/integerProperties}}
{{#doubleProperties}}

/**
* Sets the initial value of {{{propertyName}}}
* @parameter {{{propertyName}}} value to set
* @return this for chaining
**/
public Mock{{{className}}}Builder {{{setter}}}({{{type}}} {{{propertyName}}}) {
{{#superSetter}}super.{{/superSetter}}{{^superSetter}}this.{{/superSetter}}{{{propertyName}}} = {{{propertyName}}};
return this;
}
{{/doubleProperties}}
{{#stringProperties}}

/**
Expand All @@ -299,6 +311,9 @@ public class TestValuesGenerator {
{{#integerProperties}}
public static final Integer {{{propertyNameUpper}}}_TEST_VALUE = 55;
{{/integerProperties}}
{{#doubleProperties}}
public static final Double {{{propertyNameUpper}}}_TEST_VALUE = 55.1;
{{/doubleProperties}}
{{#stringProperties}}
{{^pattern}}
public static final String {{{propertyNameUpper}}}_TEST_VALUE = "test {{{propertyName}}}";
Expand Down Expand Up @@ -366,6 +381,9 @@ public class TestValuesGenerator {
{{#integerProperties}}
.{{{setter}}}({{{propertyNameUpper}}}_TEST_VALUE)
{{/integerProperties}}
{{#doubleProperties}}
.{{{setter}}}({{{propertyNameUpper}}}_TEST_VALUE)
{{/doubleProperties}}
{{#stringProperties}}
.{{{setter}}}({{{propertyNameUpper}}}_TEST_VALUE)
{{/stringProperties}}
Expand Down
24 changes: 24 additions & 0 deletions resources/javaTemplates/UnitTestTemplate.txt
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,28 @@ public class {{{className}}}Test extends TestCase {
{{/nonOptional}}
}
{{/integerProperties}}
{{#doubleProperties}}

/**
* Test method for {@link {{{pkgName}}}.{{{className}}}#{{{setter}}}}.
*/
public void test{{{className}}}{{{setter}}}() throws InvalidSPDXAnalysisException {
{{{className}}} test{{{className}}} = generator.builderFor{{{className}}}Tests(TEST_OBJECT_URI).build();
{{#nonOptional}}
assertEquals(TestValuesGenerator.{{{propertyNameUpper}}}_TEST_VALUE, test{{{className}}}.{{{getter}}}());
{{/nonOptional}}
{{^nonOptional}}
assertEquals(Optional.of(TestValuesGenerator.{{{propertyNameUpper}}}_TEST_VALUE), test{{{className}}}.{{{getter}}}());
{{/nonOptional}}
test{{{className}}}.{{{setter}}}(new Double(653.6));
{{#nonOptional}}
assertEquals(new Double(653.6), test{{{className}}}.{{{getter}}}());
{{/nonOptional}}
{{^nonOptional}}
assertEquals(Optional.of(new Double(653.6)), test{{{className}}}.{{{getter}}}());
{{/nonOptional}}
}
{{/doubleProperties}}
{{#stringProperties}}

/**
Expand Down Expand Up @@ -353,6 +375,8 @@ public class {{{className}}}Test extends TestCase {
{{/booleanProperties}}
{{#integerProperties}}
{{/integerProperties}}
{{#doubleProperties}}
{{/doubleProperties}}
{{#stringProperties}}
{{/stringProperties}}
{{#objectPropertyValueCollection}}
Expand Down
Loading

0 comments on commit c98180f

Please sign in to comment.