001: /*
002: * Copyright 2006 Le Duc Bao, Ralf Joachim
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package org.castor.ddlgen.schemaobject;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import org.castor.ddlgen.DDLGenConfiguration;
022: import org.castor.ddlgen.DDLWriter;
023:
024: /**
025: * Abstract base class for all indices.
026: *
027: * @author <a href="mailto:leducbao AT gmail DOT com">Le Duc Bao</a>
028: * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
029: * @version $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
030: * @since 1.1
031: */
032: public abstract class Index extends AbstractSchemaObject {
033: //--------------------------------------------------------------------------
034:
035: /** List of index fields. */
036: private List _fields = new ArrayList();
037:
038: /** Table the index is used for. */
039: private Table _table;
040:
041: //--------------------------------------------------------------------------
042:
043: /**
044: * Add given field to list of index fields.
045: *
046: * @param field Field to add to list of index fields.
047: */
048: public final void addField(final Field field) {
049: _fields.add(field);
050: }
051:
052: /**
053: * Get number of index fields.
054: *
055: * @return Number of index fields.
056: */
057: public final int getFieldCount() {
058: return _fields.size();
059: }
060:
061: /**
062: * Get index field at given index.
063: *
064: * @param index Index of index field to return.
065: * @return Index field at given index.
066: */
067: public final Field getField(final int index) {
068: return (Field) _fields.get(index);
069: }
070:
071: /**
072: * Set table the foreign key is used for.
073: *
074: * @param table Table the foreign key is used for.
075: */
076: public final void setTable(final Table table) {
077: _table = table;
078: }
079:
080: /**
081: * Get table the foreign key is used for.
082: *
083: * @return Table the foreign key is used for.
084: */
085: public final Table getTable() {
086: return _table;
087: }
088:
089: //--------------------------------------------------------------------------
090:
091: /**
092: * Concatenate all field names delimited by field delimiter and whitespace.
093: *
094: * @param writer DDLWriter to write schema objects to.
095: */
096: protected final void fieldNames(final DDLWriter writer) {
097: String delimiter = DDLGenConfiguration.DEFAULT_FIELD_DELIMITER;
098:
099: for (int i = 0; i < getFieldCount(); i++) {
100: if (i > 0) {
101: writer.print(delimiter);
102: writer.print(" ");
103: }
104: writer.print(getField(i).getName());
105: }
106: }
107:
108: //--------------------------------------------------------------------------
109:
110: /**
111: * {@inheritDoc}
112: */
113: public final boolean equals(final Object other) {
114: if (other == this ) {
115: return true;
116: }
117: if (other == null) {
118: return false;
119: }
120: if (other.getClass() != this .getClass()) {
121: return false;
122: }
123:
124: Index index = (Index) other;
125: return equals(getName(), index.getName())
126: && equals(_table, index._table)
127: && equals(_fields, index._fields);
128: }
129:
130: /**
131: * {@inheritDoc}
132: */
133: public final int hashCode() {
134: int hashCode = 0;
135: if (getName() != null) {
136: hashCode += getName().hashCode();
137: }
138: hashCode *= HASHFACTOR;
139: if (_table != null) {
140: hashCode += _table.hashCode();
141: }
142: hashCode *= HASHFACTOR;
143: hashCode += _fields.hashCode();
144: return hashCode;
145: }
146:
147: //--------------------------------------------------------------------------
148: }
|