001: package org.zilonis.symbol;
002:
003: /**
004: * Copyright (c) 2005 Elie Levy <elie.levy@zilonis.org> All rights reserved
005: *
006: * This License governs use of the accompanying Software, and your use of the
007: * Software constitutes acceptance of this license.
008: *
009: * You may use this Software for any non-commercial purpose, subject to the
010: * restrictions in this license. Some purposes which can be non-commercial are
011: * teaching, academic research, and personal experimentation. You may also
012: * distribute this Software with books or other teaching materials, or publish
013: * the Software on websites, that are intended to teach the use of the Software.
014: *
015: *
016: * You may not use or distribute this Software or any derivative works in any
017: * form for commercial purposes. Examples of commercial purposes would be
018: * running business operations, licensing, leasing, or selling the Software, or
019: * distributing the Software for use with commercial products.
020: *
021: * You may modify this Software and distribute the modified Software for
022: * non-commercial purposes, however, you may not grant rights to the Software or
023: * derivative works that are broader than those provided by this License. For
024: * example, you may not distribute modifications of the Software under terms
025: * that would permit commercial use, or under terms that purport to require the
026: * Software or derivative works to be sublicensed to others.
027: *
028: * You may use any information in intangible form that you remember after
029: * accessing the Software. However, this right does not grant you a license to
030: * any of the copyrights or patents for anything you might create using such
031: * information.
032: *
033: * In return, we simply require that you agree:
034: *
035: * Not to remove any copyright or other notices from the Software.
036: *
037: *
038: * That if you distribute the Software in source or object form, you will
039: * include a verbatim copy of this license.
040: *
041: *
042: * That if you distribute derivative works of the Software in source code form
043: * you do so only under a license that includes all of the provisions of this
044: * License, and if you distribute derivative works of the Software solely in
045: * object form you do so only under a license that complies with this License.
046: *
047: *
048: * That if you have modified the Software or created derivative works, and
049: * distribute such modifications or derivative works, you will cause the
050: * modified files to carry prominent notices so that recipients know that they
051: * are not receiving the original Software. Such notices must state: (i) that
052: * you have changed the Software; and (ii) the date of any changes.
053: *
054: *
055: * THAT THE SOFTWARE COMES "AS IS", WITH NO WARRANTIES. THIS MEANS NO EXPRESS,
056: * IMPLIED OR STATUTORY WARRANTY, INCLUDING WITHOUT LIMITATION, WARRANTIES OF
057: * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE OR ANY WARRANTY OF TITLE
058: * OR NON-INFRINGEMENT. ALSO, YOU MUST PASS THIS DISCLAIMER ON WHENEVER YOU
059: * DISTRIBUTE THE SOFTWARE OR DERIVATIVE WORKS.
060: *
061: *
062: * THAT NEITHER ZILONIS NOR THE AUTHOR WILL BE LIABLE FOR ANY DAMAGES RELATED TO
063: * THE SOFTWARE OR THIS LICENSE, INCLUDING DIRECT, INDIRECT, SPECIAL,
064: * CONSEQUENTIAL OR INCIDENTAL DAMAGES, TO THE MAXIMUM EXTENT THE LAW PERMITS,
065: * NO MATTER WHAT LEGAL THEORY IT IS BASED ON. ALSO, YOU MUST PASS THIS
066: * LIMITATION OF LIABILITY ON WHENEVER YOU DISTRIBUTE THE SOFTWARE OR DERIVATIVE
067: * WORKS.
068: *
069: *
070: * That if you sue anyone over patents that you think may apply to the Software
071: * or anyone's use of the Software, your license to the Software ends
072: * automatically.
073: *
074: *
075: * That your rights under the License end automatically if you breach it in any
076: * way.
077: *
078: *
079: * Elie Levy reserves all rights not expressly granted to you in this license.
080: *
081: */
082:
083: public class Triplet {
084:
085: private static SymbolTable symbolTable = new SymbolTable(100000);
086:
087: private String field[];
088:
089: public static final int IDENTIFIER = 0;
090:
091: public static final int ATTRIBUTE = 1;
092:
093: public static final int VALUE = 2;
094:
095: public Triplet(String identifier, String attribute, String value) {
096: field = new String[] { symbolTable.addSymbol(identifier),
097: symbolTable.addSymbol(attribute),
098: symbolTable.addSymbol(value) };
099: }
100:
101: public String get(int fieldName) {
102: return field[fieldName];
103: }
104:
105: public boolean isVariable(int fieldName) {
106: return field[fieldName].charAt(0) == '?';
107: }
108:
109: public boolean match(Triplet triplet) {
110: return (((triplet.isVariable(IDENTIFIER)) || (triplet
111: .get(IDENTIFIER) == get(IDENTIFIER)))
112: && ((triplet.isVariable(ATTRIBUTE)) || (triplet
113: .get(ATTRIBUTE) == get(ATTRIBUTE))) && ((triplet
114: .isVariable(VALUE)) || (triplet.get(VALUE) == get(VALUE))));
115: }
116:
117: public boolean equals(Object object) {
118: if (!(object instanceof Triplet))
119: return false;
120: Triplet triplet = (Triplet) object;
121: return ((triplet.get(IDENTIFIER) == get(IDENTIFIER))
122: && (triplet.get(ATTRIBUTE) == get(ATTRIBUTE)) && (triplet
123: .get(VALUE) == get(VALUE)));
124: }
125:
126: public int hashCode() {
127: return (get(IDENTIFIER).hashCode() + get(ATTRIBUTE).hashCode() + get(
128: VALUE).hashCode());
129: }
130:
131: public String toString() {
132: return "(" + get(IDENTIFIER) + "," + get(ATTRIBUTE) + ","
133: + get(VALUE) + ")";
134: }
135:
136: }
|