View Javadoc

1   /*
2    * db2sa: DB2 Syntax Assistant
3    * Copyright (C) Andres Gomez Casanova
4    *
5    * This file is part of db2sa.
6    *
7    * db2sa is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as published by
9    * the Free Software Foundation; either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * db2sa is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public License
18   * along with this library; if not, see <http://www.gnu.org/licenses/>.
19   *
20   * Contact:
21   * a n g o c a  at  y a h o o  dot  c o m
22   * Cra. 45 No 61 - 31, Bogota, Colombia.
23   *
24   * Author:   $LastChangedBy: angoca $:
25   * Date:     $LastChangedDate: 2009-06-11 23:36:14 +0200 (Thu, 11 Jun 2009) $:
26   * Revision: $LastChangedRevision: 221 $:
27   * URL:      $HeadURL: https://db2sa.svn.sourceforge.net/svnroot/db2sa/branches/db2sa_beta/source-code/src/main/java/name/angoca/db2sa/core/syntax/GraphAnswer.java $:
28   */
29  package name.angoca.db2sa.core.syntax;
30  
31  import java.util.List;
32  
33  import name.angoca.db2sa.core.lexical.Token;
34  
35  /**
36   * Represents the answer of a given command. An answer is the phrase completed
37   * or the possible options of the current phrase.<br/>
38   * <b>Control Version</b><br />
39   * <ul>
40   * <li>0.0.1 Class creation.</li>
41   * <li>0.1.0</li>
42   * <li>0.2.0</li>
43   * <li>0.3.0 Recommendations from PMD.</li>
44   * <li>0.3.1 Organized.</li>
45   * <li>0.3.2 not null check.</li>
46   * <li>1.0.0 Moved to version 1.</li>
47   * </ul>
48   * 
49   * @author Andres Gomez Casanova <a
50   *         href="mailto:a n g o c a at y a h o o dot c o m">(AngocA)</a>
51   * @version 1.0.0 2009-07-19
52   */
53  public final class GraphAnswer {
54      /**
55       * Set of possible options.
56       */
57      private final transient List<Token> m_options;
58  
59      /**
60       * Phrases that represents the command completed.
61       */
62      private final transient List<Token> m_phrases;
63  
64      /**
65       * Constructor that associates the phrase and the possible options.
66       * 
67       * @param phrases
68       *            phrase completed (could be the same that the user wrote.)
69       * @param options
70       *            Possible options (0..n)
71       */
72      public GraphAnswer(final List<Token>/* <!>! */phrases,
73              final List<Token>/* <!>! */options) {
74          this.m_phrases = phrases;
75          this.m_options = options;
76      }
77  
78      /*
79       * (non-Javadoc)
80       * 
81       * @see java.lang.Object#equals(java.lang.Object)
82       */
83      @Override
84      public boolean equals(final Object/* ? */object) {
85          boolean equals = false;
86          if (object instanceof GraphAnswer) {
87              final GraphAnswer graph = (GraphAnswer) object;
88              equals = true;
89              int size = graph.m_options.size();
90              for (int i = 0; i < size && equals; i += 1) {
91                  equals &= this.m_options.contains(graph.m_options.get(i));
92              }
93              size = graph.m_phrases.size();
94              for (int i = 0; i < size && equals; i += 1) {
95                  equals &= this.m_phrases.contains(graph.m_phrases.get(i));
96              }
97          }
98          return equals;
99      }
100 
101     /**
102      * Returns the set of options of the current position.
103      * 
104      * @return set of options.
105      */
106     public List<Token>/* <!>! */getOptions() {
107         return this.m_options;
108     }
109 
110     /**
111      * Returns the set of phrases of the command.
112      * 
113      * @return set of tokens that represents the command.
114      */
115     public List<Token>/* <!>! */getPhrases() {
116         return this.m_phrases;
117     }
118 
119     /*
120      * (non-Javadoc)
121      * 
122      * @see java.lang.Object#hashCode()
123      */
124     @Override
125     public int hashCode() {
126         int ret = this.m_phrases.size() * this.m_options.size();
127         for (Token token : this.m_phrases) {
128             ret -= token.getToken().hashCode();
129         }
130         for (Token token : this.m_options) {
131             ret += token.getToken().hashCode();
132         }
133         return ret;
134     }
135 
136     /**
137      * Returns a string representation of a graph answer. The representation is:
138      * [phrases],{options}
139      * 
140      * @see java.lang.Object#toString()
141      * @return String representation of a graph answer.
142      */
143     @Override
144     public String toString() {
145         final StringBuffer ret = new StringBuffer();
146         ret.append('[');
147         // Phrases
148         for (Token token : this.m_phrases) {
149             ret.append(token.toString());
150         }
151         ret.append("],{"); //$NON-NLS-1$
152         // Options
153         for (Token token : this.m_options) {
154             ret.append(token.toString());
155         }
156         ret.append('}');
157         return ret.toString();
158     }
159 }