/*
 *                      Nividic development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the microarray platform
 * of the cole Normale Suprieure and the individual authors.
 * These should be listed in @author doc comments.
 *
 * For more information on the Nividic project and its aims,
 * or to join the Nividic mailing list, visit the home page
 * at:
 *
 *      http://www.transcriptome.ens.fr/nividic
 *
 */

package fr.ens.transcriptome.nividic.om.translators;

/**
 * This abstract class implements basic methods to get several field or several
 * annotations.
 * @author Laurent Jourdren
 */
public abstract class BasicTranslator implements Translator {

  private String defaultField;

  /**
   * Get the default field.
   * @return default field
   */
  public String getDefaultField() {

    return this.defaultField;
  }

  /**
   * Set the default field.
   * @param field The field to set
   */
  public void setDefaultField(final String field) {

    if (!isField(field))
      throw new RuntimeException("The field doesn't exists");

    this.defaultField = field;
  }

  /**
   * Get a translation for a feature
   * @param id Identifier of the feature
   * @param field the field to get
   * @return An array with the annotation of the Feature
   */
  public String[] translate(final String id) {

    String[] fields = getFields();

    if (fields == null)
      return null;

    String[] result = new String[fields.length];

    for (int i = 0; i < fields.length; i++)
      result[i] = translateField(id, fields[i]);

    return result;
  }

  /**
   * Get all the annotations for features
   * @param ids Identifiers of the features
   * @return An array with the annotation of the Feature
   */
  public String[][] translate(final String[] ids) {

    if (ids == null)
      return null;

    String[][] result = new String[ids.length][];

    for (int i = 0; i < ids.length; i++) {
      result[i] = translate(ids[i]);
    }

    return result;
  }

  /**
   * Get translations for features
   * @param ids Identifiers of the features
   * @param field the field to get
   * @return An array with the annotation of the Feature
   */
  public String[] translateField(final String[] ids) {

    return translateField(ids, getDefaultField());
  }

  /**
   * Get translations for features
   * @param ids Identifiers of the features
   * @param field the field to get
   * @return An array with the annotation of the Feature
   */
  public String[] translateField(final String[] ids, final String field) {

    if (ids == null || !isField(field))
      return null;

    String[] result = new String[ids.length];

    for (int i = 0; i < ids.length; i++) {
      result[i] = translateField(ids[i], field);
    }

    return result;
  }

  /**
   * Get a translation for a feature. The field is the default field.
   * @param id Identifier of the feature
   * @return An array with the annotation of the Feature
   */
  public String translateField(final String id) {
    
    return translateField(id, getDefaultField());
  }
  
  /**
   * Test if the field exists.
   * @param field Field to test
   * @return true if the field exists
   */
  public boolean isField(final String field) {

    if (field == null)
      return false;

    String[] fields = getFields();

    if (fields == null)
      return false;

    for (int i = 0; i < fields.length; i++)
      if (field.equals(fields[i]))
        return true;

    return false;
  }

}

@@ -2794,24 +2794,182 @@
   if (ids ==
+ null)%0A      return null;%0A%0A    final String lField;%0A    if (field == null)%0A      lField = getDefaultField();%0A    else%0A      lField = field;%0A%0A    if (lField ==
  null %7C%7C !is
@@ -2966,33 +2966,34 @@
 ull %7C%7C !isField(
-f
+lF
 ield))%0A      ret
@@ -3134,17 +3134,18 @@
 ids%5Bi%5D, 
-f
+lF
 ield);%0A 
@@ -3408,13 +3408,9 @@
 ) %7B%0A
-    
 %0A
+
     
@@ -3459,18 +3459,16 @@
 ));%0A  %7D%0A
-  
 %0A  /**%0A 

Fix bug: When field of translateField(ids[],field) is null, now use the default field.