📄 abstractsupergene.java
字号:
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 */
ArrayList 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());
}
}
}
/** 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();
if (sv!=null) sv.setFromPersistent(decode(vo.nextToken()));
return sv;
}
catch (Exception ex) {
ex.printStackTrace();
throw new Error
("Unable to crate validator from '"+a_rep+"' for "+
getClass().getName());
}
}
/** Creates a new instance of gene. */
protected Gene createGene(String a_geneClassName,
String a_persistentRepresentation) throws Exception
{
Class geneClass = Class.forName (a_geneClassName);
Gene gene = (Gene) geneClass.newInstance ();
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());
}
if (m_validator==null) b.append(" non validating ");
else
if (m_validator!=this) b.append(m_validator.toString());
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
* @author Audrius Meskauskas
* @param a_x The string to split.
* @return The elements of the returned array are the
* persistent representation strings of the genes - components.
*/
protected static final ArrayList split(String a_x)
throws UnsupportedRepresentationException
{
ArrayList a = new ArrayList();
StringTokenizer st = new StringTokenizer
(a_x,GENE_DELIMITER_HEADING+ GENE_DELIMITER_CLOSING, true);
while (st.hasMoreTokens())
{
if (!st.nextToken().equals(GENE_DELIMITER_HEADING))
throw new UnsupportedRepresentationException
(a_x+" 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_x+" no close tag");
}
}
return a;
}
/** Append a new gene to the gene array. */
public void addGene(Gene g)
{
if (m_genes == null)
m_genes = new Gene [] { g };
else
{
Gene [] genes = new Gene [m_genes.length+1];
System.arraycopy(m_genes, 0, genes, 0, m_genes.length);
genes [m_genes.length] = g;
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) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -