MtasCQLParserBasicSentencePartCondition.java

  1. package mtas.parser.cql.util;

  2. import mtas.parser.cql.ParseException;
  3. import mtas.search.spans.util.MtasSpanQuery;

  4. /**
  5.  * The Class MtasCQLParserBasicSentencePartCondition.
  6.  */
  7. public abstract class MtasCQLParserBasicSentencePartCondition {

  8.   /** The minimum occurence. */
  9.   protected int minimumOccurence;

  10.   /** The maximum occurence. */
  11.   protected int maximumOccurence;

  12.   /** The optional. */
  13.   protected boolean optional;

  14.   /** The not. */
  15.   protected boolean not;

  16.   /**
  17.    * Gets the query.
  18.    *
  19.    * @return the query
  20.    * @throws ParseException the parse exception
  21.    */
  22.   public abstract MtasSpanQuery getQuery() throws ParseException;

  23.   /**
  24.    * Gets the minimum occurence.
  25.    *
  26.    * @return the minimum occurence
  27.    */
  28.   public int getMinimumOccurence() {
  29.     return minimumOccurence;
  30.   }

  31.   /**
  32.    * Gets the maximum occurence.
  33.    *
  34.    * @return the maximum occurence
  35.    */
  36.   public int getMaximumOccurence() {
  37.     return maximumOccurence;
  38.   }

  39.   /**
  40.    * Sets the occurence.
  41.    *
  42.    * @param min the min
  43.    * @param max the max
  44.    * @throws ParseException the parse exception
  45.    */
  46.   public void setOccurence(int min, int max) throws ParseException {
  47.     if ((min < 0) || (min > max) || (max < 1)) {
  48.       throw new ParseException("Illegal number {" + min + "," + max + "}");
  49.     }
  50.     if (min == 0) {
  51.       optional = true;
  52.     }
  53.     minimumOccurence = Math.max(1, min);
  54.     maximumOccurence = max;
  55.   }

  56.   /**
  57.    * Checks if is optional.
  58.    *
  59.    * @return true, if is optional
  60.    */
  61.   public boolean isOptional() {
  62.     return optional;
  63.   }

  64.   /**
  65.    * Sets the optional.
  66.    *
  67.    * @param status the new optional
  68.    */
  69.   public void setOptional(boolean status) {
  70.     optional = status;
  71.   }

  72.   /*
  73.    * (non-Javadoc)
  74.    *
  75.    * @see java.lang.Object#toString()
  76.    */
  77.   @Override
  78.   public String toString() {
  79.     return toString("", "");
  80.   }

  81.   /**
  82.    * To string.
  83.    *
  84.    * @param firstIndent the first indent
  85.    * @param indent the indent
  86.    * @return the string
  87.    */
  88.   public String toString(String firstIndent, String indent) {
  89.     String text = "";
  90.     text += firstIndent + "PART";
  91.     if (optional) {
  92.       text += " OPTIONAL";
  93.     }
  94.     if ((minimumOccurence > 1) || (minimumOccurence != maximumOccurence)) {
  95.       if (minimumOccurence != maximumOccurence) {
  96.         text += " {" + minimumOccurence + "," + maximumOccurence + "}";
  97.       } else {
  98.         text += " {" + minimumOccurence + "}";
  99.       }
  100.     }
  101.     try {
  102.       text += "\n" + indent + "- Query: "
  103.           + getQuery().toString(getQuery().getField());
  104.     } catch (ParseException e) {
  105.       text += "\n" + indent + "- Query: " + e.getMessage();
  106.     }
  107.     text += "\n";
  108.     return text;
  109.   }

  110. }