package com.dbxml.db.core.indexer;

/*
 * dbXML - Native XML Database
 * Copyright (C) 1999-2004  The dbXML Group, L.L.C.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: IndexMatch.java,v 1.2 2004/02/12 00:17:57 bradford Exp $
 */

import com.dbxml.db.core.data.Key;

/**
 * IndexMatch is used for representing match information.  IndexMatches
 * are the basic unit of information between Indexers and the IndexManager
 * for performing queries and query optimization.
 */

public final class IndexMatch implements Comparable {
   private Key key;    // The Document Key
   private int pos;    // Offset into the stream
   private int elem;   // Element ID (for wildcards)
   private int attr;   // Attribute ID (if any)

   public IndexMatch() {
      key = null;
      pos = -1;
      elem = -1;
      attr = -1;
   }

   public IndexMatch(Key key, int pos, int elem, int attr) {
      this.key = key;
      this.pos = pos;
      this.elem = elem;
      this.attr = attr;
   }

   public IndexMatch(Key key, int pos) {
      this.key = key;
      this.pos = pos;
      elem = -1;
      attr = -1;
   }

   public IndexMatch(Key key, IndexPattern pattern) {
      this.key = key;
      pos = -1;
      elem = pattern.getElementID();
      attr = pattern.getAttributeID();
   }

   /**
    * getKey returns the Document Key for the IndexMatch.
    *
    * @return The Key
    */
   public final Key getKey() {
      return key;
   }

   /**
    * getPosition returns the Document position for the Match.
    *
    * @return The Document position
    */
   public final int getPosition() {
      return pos;
   }

   /**
    * getElement returns the Element symbol ID for the Match.
    *
    * @return The Element Symbol ID
    */
   public final int getElement() {
      return elem;
   }

   /**
    * getAttribute returns the Attribute symbol ID for the Match.
    *
    * @return The Attribute Symbol ID
    */
   public final int getAttribute() {
      return attr;
   }

   public int compareTo(Object obj) {
      return key.compareTo(obj);
   }

   public String toString() {
      return key.toString();
   }
}

