📄 abstractsupergene.java
字号:
* Retrieves the allele value represented by this Supergene.
* @return array of objects, each matching the subgene in this Supergene
*/
public Object getAllele() {
Object[] o = new Object[m_genes.length];
for (int i = 0; i < m_genes.length; i++) {
o[i] = m_genes[i].getAllele();
}
return o;
}
/**
* @return a string representation of the value of this Supergene
* instance, using calls to the Supergene components. Supports other
* (nested) supergenes in this supergene
*/
public String getPersistentRepresentation() {
StringBuffer b = new StringBuffer();
// Write validator:
String validator = null;
String v_representation = "";
SupergeneValidator v = getValidator();
if (v == null) {
validator = "null";
}
else
if (v == this) {
validator = "this";
}
else {
validator = v.getClass().getName();
v_representation = v.getPersistent();
}
b.append(GENE_DELIMITER_HEADING);
b.append(encode(validator + GENE_DELIMITER + v_representation));
b.append(GENE_DELIMITER_CLOSING);
// Write genes:
Gene gene;
for (int i = 0; i < m_genes.length; i++) {
gene = m_genes[i];
b.append(GENE_DELIMITER_HEADING);
b.append(encode(gene.getClass().getName() + GENE_DELIMITER
+ gene.getPersistentRepresentation()));
b.append(GENE_DELIMITER_CLOSING);
}
return b.toString();
}
/**
* Sets the value and internal state of this Gene from the string
* representation returned by a previous invocation of the
* getPersistentRepresentation() method.
*
* If the validator is not THIS and not null, a new validator is
* created using Class.forName(..).newInstance.
*
* @param a_representation the string representation retrieved from a
* prior call to the getPersistentRepresentation() method
*
* @throws UnsupportedRepresentationException
*
* @author Audrius Meskauskas
* @since 2.0
*/
public void setValueFromPersistentRepresentation(String a_representation)
throws UnsupportedRepresentationException {
if (a_representation != null) {
try {
/// Remove the old content.
// ------------------------
List r = split(a_representation);
Iterator iter = r.iterator();
m_genes = new Gene[r.size() - 1];
// The first member in array is a validator representation.
// --------------------------------------------------------
StringTokenizer st;
String clas;
String representation;
String g;
Gene gene;
String validator = (String) iter.next();
setValidator(createValidator(decode(validator)));
for (int i = 0; i < m_genes.length; i++) {
g = decode( (String) iter.next());
st = new StringTokenizer(g, GENE_DELIMITER);
if (st.countTokens() != 2)
throw new UnsupportedRepresentationException("In " + g + ", " +
"expecting two tokens, separated by " + GENE_DELIMITER);
clas = st.nextToken();
representation = st.nextToken();
gene = createGene(clas, representation);
m_genes[i] = gene;
}
}
catch (Exception ex) {
ex.printStackTrace();
throw new UnsupportedRepresentationException(ex.getCause().
getMessage());
}
}
else {
throw new UnsupportedRepresentationException("null value not allowed");
}
}
/** Create validator from the string representation. */
protected SupergeneValidator createValidator(String a_rep) {
try {
StringTokenizer vo = new StringTokenizer
(a_rep, GENE_DELIMITER, true);
if (vo.countTokens() != 2)throw new Error
("In " + a_rep + ", expecting two tokens, separated by " +
GENE_DELIMITER);
String clas = vo.nextToken();
SupergeneValidator sv;
if (clas.equals("this")) {
sv = this;
}
else if (clas.equals("null")) {
sv = null;
}
else {
// sv = (SupergeneValidator) Class.forName(clas).newInstance();
Class svClass = Class.forName(clas);
Constructor constr = svClass.getConstructor(new Class[] {Configuration.class});
sv = (SupergeneValidator) constr.newInstance(new Object[] {
getConfiguration()});
}
if (sv != null) {
sv.setFromPersistent(decode(vo.nextToken()));
}
return sv;
}
catch (Exception ex) {
throw new Error
("Unable to create validator from '" + a_rep + "' for " +
getClass().getName(), ex);
}
}
/** Creates a new instance of gene. */
protected Gene createGene(String a_geneClassName,
String a_persistentRepresentation)
throws Exception {
Class geneClass = Class.forName(a_geneClassName);
Constructor constr = geneClass.getConstructor(new Class[] {Configuration.class});
Gene gene = (Gene) constr.newInstance(new Object[] {getConfiguration()});
gene.setValueFromPersistentRepresentation(a_persistentRepresentation);
return gene;
}
/** Calls cleanup() for each subgene. */
public void cleanup() {
for (int i = 0; i < m_genes.length; i++) {
m_genes[i].cleanup();
}
}
/**
* @return a string representation of the supergene, providing
* class name and calling toString() for all subgenes.
*/
public String toString() {
StringBuffer b = new StringBuffer();
b.append("Supergene " + getClass().getName() + " {");
for (int i = 0; i < m_genes.length; i++) {
b.append("|");
b.append(m_genes[i].toString());
b.append("|");
}
if (m_validator == null) {
b.append(" non validating");
}
else {
b.append(" validator: "+m_validator.getClass().getName());
}
b.append("}");
return b.toString();
}
/** Returns the number of the genes-components of this supergene. */
public int size() {
return m_genes.length;
}
/** Calls compareTo() for all subgenes. The passed parameter must be
* an instance of AbstractSupergene. */
public int compareTo(Object o) {
AbstractSupergene q = (AbstractSupergene) o;
int c = m_genes.length - q.m_genes.length;
if (c != 0) {
return c;
}
for (int i = 0; i < m_genes.length; i++) {
c = m_genes[i].compareTo(q.m_genes[i]);
if (c != 0) {
return c;
}
}
if (getClass().equals(o.getClass())) {
return 0;
}
return getClass().getName().compareTo(o.getClass().getName());
}
/**
* Calls equals() for each pair of genes. If the supplied object is
* an instance of the different class, returns false. Also, the
* genes are assumed to be different if they have different validator
* classes (or only one of the validators is set to null).
*/
public boolean equals(Object a_gene) {
if (a_gene == null || ! (a_gene.getClass().equals(getClass()))) {
return false;
}
AbstractSupergene age = (AbstractSupergene) a_gene;
if (m_validator != age.m_validator)
if (m_validator != null && age.m_immutable != null)
if (!m_validator.getClass().equals(age.m_validator.getClass()))
return false;
return Arrays.equals(m_genes, age.m_genes);
}
/** Returns sum of hashCode() of the genes-components. */
public int hashCode() {
int s = 0;
for (int i = m_genes.length - 1; i >= 0; i--) {
s += m_genes[i].hashCode();
}
return s;
}
/* Encode string, doubling the separators. */
protected static final String encode(String a_x) {
try {
return URLEncoder.encode(a_x, "UTF-8");
}
catch (UnsupportedEncodingException ex) {
throw new Error("This should never happen!");
}
}
/** Decode string, undoubling the separators. */
protected static final String decode(String a_x) {
try {
return URLDecoder.decode(a_x, "UTF-8");
}
catch (UnsupportedEncodingException ex) {
throw new Error("This should never happen!");
}
}
/**
* Splits the string a_x into individual gene representations
* @param a_string the string to split
* @return the elements of the returned array are the
* persistent representation strings of the genes - components
*
* @author Audrius Meskauskas
*/
protected static final List split(String a_string)
throws UnsupportedRepresentationException {
List a = Collections.synchronizedList(new ArrayList());
StringTokenizer st = new StringTokenizer
(a_string, GENE_DELIMITER_HEADING + GENE_DELIMITER_CLOSING, true);
while (st.hasMoreTokens()) {
if (!st.nextToken().equals(GENE_DELIMITER_HEADING)) {
throw new UnsupportedRepresentationException
(a_string + " no open tag");
}
String n = st.nextToken();
if (n.equals(GENE_DELIMITER_CLOSING)) a.add(""); // Empty token
else {
a.add(n);
if (!st.nextToken().equals(GENE_DELIMITER_CLOSING)) {
throw new UnsupportedRepresentationException
(a_string + " no close tag");
}
}
}
return a;
}
/** Append a new gene to the gene array. */
public void addGene(Gene a_gene) {
Gene[] genes = new Gene[m_genes.length + 1];
System.arraycopy(m_genes, 0, genes, 0, m_genes.length);
genes[m_genes.length] = a_gene;
m_genes = genes;
}
/**
* Sets an object, responsible for deciding if the Supergene allele
* combination is valid. If it is set to null, no validation is performed
* (all combinations are assumed to be valid). If no validator is
* set, the method <code>isValid (Gene [] ) </code>is called.
*/
public void setValidator(SupergeneValidator a_validator) {
m_validator = a_validator;
}
/**
* Gets an object, responsible for deciding if the Supergene allele
* combination is valid. If no external validator was set and the
* class uses its own internal validation method, it returns <i>this</i>
*/
public SupergeneValidator getValidator() {
return m_validator;
}
/** A validator (initially set to <i>this</i> */
protected SupergeneValidator m_validator = this;
/** {@inheritDoc}
* The default implementation returns an empty string. */
public String getPersistent() {
return "";
}
/** {@inheritDoc}
* The default implementation does nothing. */
public void setFromPersistent(String a_from) {
}
/**
* @return not needed for abstract supergene
*/
public Object getInternalValue() {
if (true) {
throw new RuntimeException("getInternalValue() called unexpectedly!");
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -