package uk.ac.ebi.quickgo.geneproduct.model;

import com.google.common.base.Preconditions;
import java.util.regex.Pattern;

/**
 * The information for a single Database cross reference.
 * This information is used, for example, to validate gene product ids based on source database and the id type.
 *
 * @author Tony Wardell
 *         Date: 18/04/2016
 *         Time: 13:52
 *         Created with IntelliJ IDEA.
 */
public class GeneProductDbXRefIDFormat {

    //E.g. UniProtKB
    private String database;

    //E.g. PR:000000001
    private String entityType;

    //E.g. protein
    private String entityTypeName;

    //E.g. regex for validation  ([OPQ][0-9][A-Z0-9]{3}[0-9]|[A-NR-Z]([0-9][A-Z][A-Z0-9]{2}){1,2}[0-9])...
    private Pattern idValidationPattern;

    //E.g. http://www.uniprot.org/uniprot/[example_id]/
    private String databaseURL;

    public GeneProductDbXRefIDFormat(String database, String entityType, String entityTypeName, String idValidationPattern,
            String databaseURL) {
        Preconditions.checkNotNull(idValidationPattern,
                "The regex for the validation of ids from " + database + " is null and therefore invalid");
        this.database = database;
        this.entityType = entityType;
        this.entityTypeName = entityTypeName;
        this.idValidationPattern = Pattern.compile(idValidationPattern);
        this.databaseURL = databaseURL;
    }

    /**
     * Does the argument match the regular expression that determines validity of the entity?
     * @param id
     * @return
     */
    public boolean matches(String id) {
        return idValidationPattern.matcher(id).matches();
    }

    public String getDatabase() {
        return database;
    }

    public String getEntityType() {
        return entityType;
    }

    public String getEntityTypeName() {
        return entityTypeName;
    }

    public Pattern getIdValidationPattern() {
        return idValidationPattern;
    }

    public String getDatabaseURL() {
        return databaseURL;
    }

    @Override
    public String toString() {
        return "GeneProductXrefEntity{" +
                "database='" + database + '\'' +
                ", entityType='" + entityType + '\'' +
                ", entityTypeName='" + entityTypeName + '\'' +
                ", idValidationPattern=" + idValidationPattern +
                ", databaseURL='" + databaseURL + '\'' +
                '}';
    }
}

@@ -117,16 +117,82 @@
 ttern;%0A%0A
+import static com.google.common.base.Preconditions.checkNotNull;%0A%0A
 /**%0A * T
@@ -998,37 +998,15 @@
 pe, 
-String entityTypeName, String
+Pattern
  idV
@@ -1021,30 +1021,28 @@
 nPattern
-,%0A    
+) %7B%0A
         
 String d
@@ -1033,31 +1033,53 @@
         
-String 
+this.database = checkNotNull(
 database
 URL) %7B%0A 
@@ -1074,14 +1074,10 @@
 base
-URL) %7B
+);
 %0A   
@@ -1081,30 +1081,95 @@
         
-Preconditions.
+this.entityType = checkNotNull(entityType);%0A        this.idValidationPattern = 
 checkNot
@@ -1196,234 +1196,563 @@
 tern
-,%0A  
+);%0A
     
+%7D%0A%0A
     
-      %22The regex for the validation of ids from %22 + database + %22 is null and therefore invalid%22);%0A        this.database = database;%0A        this.entityType = entityType;%0A        this.entityTypeName = entityTypeName
+public GeneProductDbXRefIDFormat(String database, String entityType, String entityTypeName, String idValidationPattern,%0A            String databaseURL) %7B%0A%0A        this.database =  checkNotNull(database, %22The database ID should not be null%22);%0A        this.entityType =  checkNotNull(entityType, %22The entity type should not be null%22);%0A        this.entityTypeName = entityTypeName;%0A        Preconditions.checkNotNull(idValidationPattern,%0A                %22The regex for the validation of ids from %22 + database + %22 is null and therefore invalid%22)
 ;%0A  

Add preconditions defending against nulls in constructor for parameters as required.