Skip to content

Commit

Permalink
feat: Issuer Optimizations
Browse files Browse the repository at this point in the history
* Added Guava dependency to use in String checks etc.
* Added empty-null checks to Issuer (Person, Organization) properties

Refs: #4
  • Loading branch information
Muratcan Şentürk committed Mar 21, 2023
1 parent 0af5df9 commit b213b0f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
15 changes: 13 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,29 @@
<dependency>
<groupId>org.immutables</groupId>
<artifactId>value</artifactId>
<version>2.8.8</version>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.immutables</groupId>
<artifactId>builder</artifactId>
<version>2.8.8</version>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.diffplug.guava</groupId>
<artifactId>guava-core</artifactId>
<version>19.0.0</version>
</dependency>
</dependencies>

<properties>
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/org/snturk/petition/Petition.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,41 @@

@Value.Immutable
@Value.Style(strictBuilder = true)
public abstract class Petition {
public interface Petition {

/**
* By default id is generated by idGenerator function, but it can be overridden by setting id explicitly.
* @return
*/
@Value.Default
public @Nullable String id() {
return idGenerator().apply(this);
default @Nullable String getId() {
return getIdGenerator().apply(this);
}

/**
* Name of the petition
* @return
*/
public abstract String name();
String getName();

/**
* Title of the petition
* @return
*/
public abstract String title();
String getTitle();

/**
* Issuer or signer of the petition
* @return
*/
public abstract Issuer issuer();
Issuer getIssuer();

/**
* Date of the petition
* @return
*/
@Value.Default
public LocalDate issuedDate() {
default LocalDate getIssuedDate() {
return LocalDate.now();
}

Expand All @@ -55,17 +55,17 @@ public LocalDate issuedDate() {
* @see PetitionType
* @return PetitionType
*/
public abstract @Nullable PetitionType type();
@Nullable PetitionType getType();

/**
* Related attachments of the petition, key is the name of the attachment, value is the java.io.File object
* @return
*/
public abstract @Nullable Map<String, File> attachments();
@Nullable Map<String, File> getAttachments();

/**
* Function to generate id of the petition
* @return
*/
public abstract Function<Petition, String> idGenerator();
Function<Petition, String> getIdGenerator();
}
15 changes: 14 additions & 1 deletion src/main/java/org/snturk/petition/model/Organization.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
package org.snturk.petition.model;

import com.google.common.base.Strings;
import org.snturk.petition.exceptions.InvalidIssuerException;

public class Organization implements Issuer {

private String corporateName;
private String corporationType;

public Organization(String corporateName) {
if (Strings.isNullOrEmpty(corporateName)) {
throw new InvalidIssuerException("Corporate name cannot be null or empty");
}
this.corporateName = corporateName;
}

public Organization(String corporateName, String corporationType) {
if (Strings.isNullOrEmpty(corporateName)) {
throw new InvalidIssuerException("Corporate name cannot be null or empty");
}
if (Strings.isNullOrEmpty(corporationType)) {
throw new InvalidIssuerException("Corporation type cannot be null or empty, if there is no corporation type, use the other constructor");
}
this.corporateName = corporateName;
this.corporationType = corporationType;
}

@Override
public String getCompleteName() {
return getCorporateName() + (getCorporationType().isEmpty() ? "" : " (" + getCorporationType() + ")");
String type = Strings.isNullOrEmpty(getCorporationType()) ? "" : " (" + getCorporationType() + ")";
return getCorporateName() + type;
}

public String getCorporateName() {
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/org/snturk/petition/model/Person.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.snturk.petition.model;

import com.google.common.base.Strings;
import org.snturk.petition.exceptions.InvalidIssuerException;

public class Person implements Issuer {
Expand All @@ -11,25 +12,35 @@ public class Person implements Issuer {
// TODO: Signature of the petition should be added here

public Person(String firstName, String middleName, String lastName) {
if (firstName == null || firstName.isEmpty()) {
if (Strings.isNullOrEmpty(firstName)) {
throw new InvalidIssuerException("First name cannot be null or empty");
}
if (lastName == null || lastName.isEmpty()) {
if (Strings.isNullOrEmpty(lastName)) {
throw new InvalidIssuerException("Last name cannot be null or empty");
}
if (Strings.isNullOrEmpty(middleName)) {
throw new InvalidIssuerException("Middle name cannot be null or empty, if there is no middle name, use the other constructor");
}
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
}

public Person(String firstName, String lastName) {
if (Strings.isNullOrEmpty(firstName)) {
throw new InvalidIssuerException("First name cannot be null or empty");
}
if (Strings.isNullOrEmpty(lastName)) {
throw new InvalidIssuerException("Last name cannot be null or empty");
}
this.firstName = firstName;
this.lastName = lastName;
}

@Override
public String getCompleteName() {
return getFirstName() + " " + (getMiddleName().isEmpty() ? "" : getMiddleName()) + " " + getLastName();
String middle = Strings.isNullOrEmpty(getMiddleName()) ? " " : " " + getMiddleName() + " ";
return getFirstName() + middle + getLastName();
}

public String getFirstName() {
Expand Down

0 comments on commit b213b0f

Please sign in to comment.