📄 mapgene.java
字号:
if (element.startsWith("(")) {
key = element.substring(1);
lastWasOpening = true;
}
else {
throw new IllegalStateException("Opening bracket missing");
}
}
}
}
}
/**
* Retrieves a string representation of this Gene that includes any
* information required to reconstruct it at a later time, such as its
* value and internal state. This string will be used to represent this
* Gene in XML persistence. This is an optional method but, if not
* implemented, XML persistence and possibly other features will not be
* available. An UnsupportedOperationException should be thrown if no
* implementation is provided.
*
* @return string representation of this Gene's current state
* @throws UnsupportedOperationException to indicate that no implementation
* is provided for this method.
*
* @author Neil Rostan
* @author Klaus Meffert
* @since 2.4
*/
public String getPersistentRepresentation()
throws UnsupportedOperationException {
// The persistent representation includes the value and the allele
// assignment.
// ---------------------------------------------------------------
Iterator it = m_geneMap.keySet().iterator();
StringBuffer strbf = new StringBuffer();
boolean first = true;
while (it.hasNext()) {
if (!first) {
strbf.append(",");
}
Object key = it.next();
Object value = m_geneMap.get(key);
strbf.append("(" + key.toString() + "," + value.toString() + ")");
first = false;
}
return m_value.toString() + MapGene.PERSISTENT_FIELD_DELIMITER +
strbf.toString();
}
/**
* Sets the value (allele) of this Gene to the new given value. This class
* expects the value to be an instance of current type (e.g. Integer).
*
* @param a_newValue the new value of this Gene instance
*
* @author Johnathan Kool
* @since 2.4
*/
public void setAllele(Object a_newValue) {
// ignore null value as it should have no effect here (otherwise problematic
// in conjunction with newGene)
if (a_newValue == null) {
return;
}
if (m_geneMap.keySet().isEmpty()) {
m_value = a_newValue;
}
else if (m_geneMap.keySet().contains(a_newValue)) {
m_value = m_geneMap.get(a_newValue);
}
else {
throw new IllegalArgumentException("Allele value being set ("
+a_newValue
+") is not an element of the set of"
+" permitted values.");
}
}
/**
* Compares this NumberGene with the specified object (which must also
* be a NumberGene) for order, which is determined by the number
* value of this Gene compared to the one provided for comparison.
*
* @param a_other the NumberGene to be compared to this NumberGene
* @return a negative integer, zero, or a positive integer as this object
* is less than, equal to, or greater than the object provided for comparison
*
* @throws ClassCastException if the specified object's type prevents it from
* being compared to this Gene
*
* @author Klaus Meffert
* @author Johnathan Kool
* @since 2.4
*/
public int compareTo(Object a_other) {
MapGene otherGene = (MapGene) a_other;
// First, if the other gene (or its value) is null, then this is
// the greater allele. Otherwise, just use the overridden compareToNative
// method to perform the comparison.
// ---------------------------------------------------------------
if (otherGene == null) {
return 1;
}
else if (otherGene.m_value == null) {
// If our value is not null, then we're the greater gene.
// ------------------------------------------------------
if (m_value != null) {
return 1;
}
}
try {
int size1 = m_geneMap.size();
int size2 = otherGene.m_geneMap.size();
if (size1 != size2) {
if (size1 < size2) {
return -1;
}
else {
return 1;
}
}
else {
//compare geneMap keys and values
Iterator it1 = m_geneMap.keySet().iterator();
Iterator it2 = otherGene.m_geneMap.keySet().iterator();
while (it1.hasNext()) {
Object key1 = it1.next();
if (!otherGene.m_geneMap.keySet().contains(key1)) {
// arbitrarily returning -1
return -1;
}
Object value1 = m_geneMap.get(key1);
Object value2 = otherGene.m_geneMap.get(key1);
if (value1 == null && value2 != null) {
return -1;
}
else if (!value1.equals(value2)) {
// arbitrarily returning -1
return -1;
}
}
}
if (m_value == null) {
if (otherGene.m_value != null) {
return 1;
}
else {
return 0;
}
}
Method method = m_value.getClass().getMethod("compareTo",
new Class[] {otherGene.m_value.getClass()});
Integer i = (Integer) method.invoke(m_value,
new Object[] {otherGene.m_value});
return i.intValue();
}
catch (InvocationTargetException ex) {
ex.printStackTrace();
throw new IllegalArgumentException("CompareTo method of the Gene value" +
" object cannot be invoked.");
}
catch (IllegalArgumentException ex) {
ex.printStackTrace();
throw new IllegalArgumentException("The value object of the Gene does" +
" not have a compareTo method. It" +
" cannot be compared.");
}
catch (IllegalAccessException ex) {
ex.printStackTrace();
throw new IllegalArgumentException("The compareTo method of the Gene" +
" value object cannot be accessed ");
}
catch (SecurityException ex) {
ex.printStackTrace();
throw new IllegalArgumentException("The compareTo method of the Gene" +
" value object cannot be accessed." +
" Insufficient permission levels.");
}
catch (NoSuchMethodException ex) {
ex.printStackTrace();
throw new IllegalArgumentException("The value object of the Gene does" +
" not have a compareTo method. It" +
" cannot be compared.");
}
}
/**
* @return the internal value of the gene
* @since 2.4
*/
protected Object getInternalValue() {
return m_value;
}
/**
* Modified hashCode() function to return different hashcodes for differently
* ordered genes in a chromosome
* @return -1 if no allele set, otherwise value return by BaseGene.hashCode()
*
* @author Klaus Meffert
* @since 2.4
*/
public int hashCode() {
if (getInternalValue() == null) {
return -71;
}
else {
return super.hashCode();
}
}
/**
* Retrieves a string representation of this Gene's value that
* may be useful for display purposes.
*
* @return a string representation of this Gene's value.
*
* @author Klaus Meffert
* @since 2.4
*/
public String toString() {
String result = "[";
if (m_geneMap.size() < 1) {
result += "null";
}
else {
Set keys = m_geneMap.keySet();
Iterator keyIterator = keys.iterator();
boolean firstTime = true;
while (keyIterator.hasNext()) {
if (!firstTime) {
result += ",";
}
else {
firstTime = false;
}
Object key = keyIterator.next();
String keyString;
if (key == null) {
keyString = "null";
}
else {
keyString = key.toString();
}
result += "(" + keyString + ",";
Object value = m_geneMap.get(key);
String valueString;
if (value == null) {
valueString = "null";
}
else {
valueString = value.toString();
}
result += valueString + ")";
}
}
result += "]";
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -