Code example for Vector

Methods: add, get, set, size

0
   * @param newvalue 
   *          The value that shall be added. 
   */ 
  private void addValue(String newvalue) {
    if (this._values == null) {
      this._values = new Vector<String>();
    } 
    this._values.add(newvalue);
  } 
 
  /** 
   * Returns the data collected by this query. 
   *  
   * @return The data collected by this query (Vector{String}) 
   */ 
  public String[] getResult() {
    if (this._values == null) {
      return new String[0];
    } 
    String[] result = new String[this._values.size()];
    this._values.toArray(result);
    return result;
  } 
 
  /** 
   * Returns the data collected by this query. If no or one result were found, this method returns null or the result. 
   * If more than one result were found, an XQueryException will be thrown. 
   *  
   * @return The data collected by this query (String) 
   */ 
  public String getSingleResult() {
    String[] results = getResult();
    if (results.length > 1) {
      throw new Ant4EclipseException(CoreExceptionCode.X_QUERY_DUCPLICATE_ENTRY_EXCEPTION, this._xquery, this._fileName);
    } 
    if (results.length == 0) {
      return null; 
    } 
    return results[0];
  } 
 
  /** 
   * Returns true if at least one result is available. 
   *  
   * @return true <=> At least one result is available. 
   */ 
  public boolean gotResult() { 
    return this._values != null;
  } 
 
  /** 
   * Returns the counter for the supplied level. 
   *  
   * @param depth 
   *          The depth which counter will be returned. 
   *  
   * @return The depth of the supplied level. 
   */ 
  private int counter(int depth) {
    // -1 because the method 'adjustCounter' will be called before 
    // the use of this function. 
    return this._counter[depth] - 1;
  } 
 
  /** 
   * {@inheritDoc} 
   */ 
  @Override 
  public String toString() {
    return this._xquery;
  } 
 
  /** 
   * Creates an exception indicating that the query is invalid. 
   *  
   * @param query 
   *          The invalid query. 
   * @param msg 
   *          An error message. 
   */ 
  private void invalid(String query, String msg) {
    throw new Ant4EclipseException(CoreExceptionCode.X_QUERY_INVALID_QUERY_EXCEPTION, query, msg);
  } 
 
  /** 
   * A Condition used to test a query expression. 
   */ 
  private interface Condition { 
 
    /** 
     * Tests a query expression using the supplied attributes. 
     *  
     * @param element 
     *          The element name. 
     * @param attrs 
     *          The attributes which has to be used for the test. 
     *  
     * @return true <=> The condition is met. 
     */ 
    boolean check(String element, Attributes attrs);
 
  } /* ENDINTERFACE */ 
 
  /** 
   * Condition implementation which checks for a specific element level. 
   */ 
  private class IndexCompare implements Condition { 
 
    private int level;
 
    private int index;
 
    /** 
     * Initializes this condition using the supplied element level. 
     *  
     * @param lvlindex 
     *          The element count used for the check. 
     * @param cnt 
     *          The index used for the comparison. 
     */ 
    public IndexCompare(int lvlindex, int cnt) {
      this.level = lvlindex;
      this.index = cnt;
    } 
 
    /** 
     * {@inheritDoc} 
     */ 
    public boolean check(String element, Attributes attrs) {
      return counter(this.index) == this.level;
    } 
 
  } /* ENDCLASS */ 
 
  /** 
   * Implements a simple function that generates the values. 
   */ 
  private class CounterFunction implements Condition { 
 
    private int index;
 
    /** 
     * Initializes this counter. 
     *  
     * @param idx 
     *          The index of the counter that shall be used. 
     */ 
    public CounterFunction(int idx) {
      this.index = idx;
    } 
 
    /** 
     * {@inheritDoc} 
     */ 
    public boolean check(String element, Attributes attrs) {
 
      if (counter(this.index) == 0) {
        // create a new entry 
        addValue("0"); 
      } 
 
      // increment the current value 
      Vector<String> values = XQuery.this._values;
      String lastval = values.get(values.size() - 1);
      int value = Integer.parseInt(lastval);
      values.set(values.size() - 1, String.valueOf(value + 1));
 
      // function evaluation, so we don't need to process anything else 
      return false; 
 
    }