Skip to content

Commit

Permalink
#6 predefining PROPERTY if empty
Browse files Browse the repository at this point in the history
+ unit tests
  • Loading branch information
shamseen committed Jul 26, 2017
1 parent b7eb74f commit 42163ea
Show file tree
Hide file tree
Showing 2 changed files with 608 additions and 602 deletions.
254 changes: 132 additions & 122 deletions src/main/java/org/cidarlab/eugene/dom/ComponentType.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,150 +26,160 @@
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/

package org.cidarlab.eugene.dom;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.cidarlab.eugene.constants.EugeneConstants;
import org.cidarlab.eugene.exception.EugeneException;

/**
* A ComponentType represents an abstract genetic component, such as part type.
* In Eugene, every Component has a ComponentType. A ComponentType has a list of
* A ComponentType represents an abstract genetic component, such as part type.
* In Eugene, every Component has a ComponentType. A ComponentType has a list of
* properties that the Component instantiates (so-called Property Values).
*
*
* @author Ernst Oberortner
*
*/
public class ComponentType
extends NamedElement {
public class ComponentType
extends NamedElement {

private static final long serialVersionUID = 1242448556255896751L;
private static final long serialVersionUID = 1242448556255896751L;

/*
/*
* LIST OF PROPERTIES
*
* Every ComponentType has two pre-defined properties: PIGEON and SEQUENCE
* both pre-defined properties are appended at the
* end of the user-defined list of properties.
*
*/
protected List<Property> properties;
/**
* Constructor w/ name
*
* @param name ... the name of the ComponentType
*/
protected ComponentType(String name) {
super(name);
this.properties = new ArrayList<Property>();

this.addPredefinedProperties();
}
private void addPredefinedProperties() {
/*
*/
protected List<Property> properties;

/**
* Constructor w/ name
*
* @param name ... the name of the ComponentType
*/
protected ComponentType(String name) {
super(name);
this.properties = new ArrayList<Property>();

this.addPredefinedProperties();
}

private void addPredefinedProperties() {
/*
* check if the list of properties contains
* the PIGEON and SEQUENCE Property
*/
boolean bPigeon = false;
boolean bSequence = false;
for(Property prop : this.getProperties()) {
if(EugeneConstants.SEQUENCE_PROPERTY.equals(prop.getName().toUpperCase())) {
bSequence = true;
} else if(EugeneConstants.PIGEON_PROPERTY.equals(prop.getName().toUpperCase())) {
bPigeon = true;
}
}

if(!bPigeon) {
this.properties.add(
new Property(EugeneConstants.PIGEON_PROPERTY, EugeneConstants.TXT));
}

if(!bSequence) {
this.properties.add(
new Property(EugeneConstants.SEQUENCE_PROPERTY, EugeneConstants.TXT));
}
}

/**
* Constructor w/ name and properties
*
* @param name ... the name of the ComponentType
* @param properties ... a list of properties of the type
*/
public ComponentType(String name, List<Property> properties) {
super(name);
this.properties = properties;

this.addPredefinedProperties();
}


/**
* The getProperties() method returns the list of properties
* of the ComponentType
*
* @return the of the ComponentType's properties
*/
public List<Property> getProperties() {
return this.properties;
}

/**
* The getProperty(String) method returns a property of
* a given name. If the property does not exist, then
* it returns NULL.
*
* @param name ... the name of the desired Property
* @return ... the Property if it exists, NULL otherwise
*/
public Property getProperty(String name) {

for (Property p : this.getProperties()) {
if (p.getName().equals(name)) {
return p;
}
}

return (Property) null;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Type ").append(this.getName()).append("(");
for(int i=0; i<this.getProperties().size(); i++) {
sb.append(".").append(this.getProperties().get(i).getName()).append("(")
.append(this.getProperties().get(i).getType()).append(")");

if(i < this.getProperties().size() -1 ) {
sb.append(", ");
}
}
sb.append(");");
return sb.toString();
}

@Override
public NamedElement getElement(String name) {
/*
* the PIGEON, SEQUENCE, SEQUENCE_ONTOLOGY Properties
*/

List<String> requiredProps = Arrays.asList(
EugeneConstants.SEQUENCE_ONTOLOGY_PROPERTY,
EugeneConstants.PIGEON_PROPERTY,
EugeneConstants.SEQUENCE_PROPERTY);

List<String> props = this.getPropertyNames();

for (String rProp : requiredProps) {
if (!props.contains(rProp)) {
this.properties.add(new Property(rProp, EugeneConstants.TXT));
}
}
}

/**
* Constructor w/ name and properties
*
* @param name ... the name of the ComponentType
* @param properties ... a list of properties of the type
*/
public ComponentType(String name, List<Property> properties) {
super(name);
this.properties = properties;

this.addPredefinedProperties();
}

/**
* The getProperties() method returns the list of properties of the
* ComponentType
*
* @return the of the ComponentType's properties
*/
public List<Property> getProperties() {
return this.properties;
}

/**
* The getProperty(String) method returns a property of a given name. If the
* property does not exist, then it returns NULL.
*
* @param name ... the name of the desired Property
* @return ... the Property if it exists, NULL otherwise
*/
public Property getProperty(String name) {

for (Property p : this.getProperties()) {
if (p.getName().equals(name)) {
return p;
}
}

return (Property) null;
}

/**
* The getProperties() method returns the list of properties of the
* ComponentType
*
* @return the of the ComponentType's properties
*/
public List<String> getPropertyNames() {

List<String> names = new ArrayList<>();

for (Property prop : this.getProperties()) {
names.add(prop.getName());
}

return names;

}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Type ").append(this.getName()).append("(");
for (int i = 0; i < this.getProperties().size(); i++) {
sb.append(".").append(this.getProperties().get(i).getName()).append("(")
.append(this.getProperties().get(i).getType()).append(")");

if (i < this.getProperties().size() - 1) {
sb.append(", ");
}
}
sb.append(");");
return sb.toString();
}

@Override
public NamedElement getElement(String name) {
/*
* here, we iterate over the properties
*/
return this.getProperty(name);
}

@Override
public NamedElement getElement(int idx)
throws EugeneException {
if(idx < 0 || idx >= this.getProperties().size()) {
throw new EugeneException(idx + " is out of bounds in the " + this.getName() +" type!");
}
return this.getProperties().get(idx);
}
*/
return this.getProperty(name);
}

@Override
public NamedElement getElement(int idx)
throws EugeneException {
if (idx < 0 || idx >= this.getProperties().size()) {
throw new EugeneException(idx + " is out of bounds in the " + this.getName() + " type!");
}
return this.getProperties().get(idx);
}

}
Loading

0 comments on commit 42163ea

Please sign in to comment.