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 org.apache.commons.logging.Log;
019: import org.apache.commons.logging.LogFactory;
020:
021: /**
022: * Abstract base class for all key generators.
023: *
024: * @author <a href="mailto:leducbao AT gmail DOT com">Le Duc Bao</a>
025: * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
026: * @version $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
027: * @since 1.1
028: */
029: public abstract class KeyGenerator extends AbstractSchemaObject {
030: //--------------------------------------------------------------------------
031:
032: /** The <a href="http://jakarta.apache.org/commons/logging/">Jakarta Commons
033: * Logging </a> instance used for all logging. */
034: private static final Log LOG = LogFactory
035: .getLog(KeyGenerator.class);
036:
037: //--------------------------------------------------------------------------
038:
039: /** Alias of the key generator. */
040: private String _alias;
041:
042: /** Table the key generator creates keys for. */
043: private Table _table;
044:
045: //--------------------------------------------------------------------------
046:
047: /**
048: * Construct key generator with given name and alias.
049: *
050: * @param name Name of the key generator algorithm.
051: * @param alias Alias of the key generator.
052: */
053: protected KeyGenerator(final String name, final String alias) {
054: super ();
055:
056: setName(name);
057: setAlias((alias != null) ? alias : name);
058: }
059:
060: //--------------------------------------------------------------------------
061:
062: /**
063: * Set alias of the key generator.
064: *
065: * @param alias Alias of the key generator.
066: */
067: public final void setAlias(final String alias) {
068: _alias = alias;
069: }
070:
071: /**
072: * Get alias of the key generator.
073: *
074: * @return Alias of the key generator.
075: */
076: public final String getAlias() {
077: return _alias;
078: }
079:
080: /**
081: * Set table the key generator creates keys for.
082: *
083: * @param table Table the key generator creates keys for.
084: */
085: public final void setTable(final Table table) {
086: _table = table;
087: }
088:
089: /**
090: * Get table the key generator creates keys for.
091: *
092: * @return Table the key generator creates keys for.
093: */
094: public final Table getTable() {
095: return _table;
096: }
097:
098: //--------------------------------------------------------------------------
099:
100: /**
101: * Check wether this key generator is compatible with the given one to allow merge
102: * of table definitions.
103: *
104: * @param keygen Key generator to merge.
105: */
106: public final void merge(final KeyGenerator keygen) {
107: if (keygen == null) {
108: String msg = "Merge table has no key generator";
109: LOG.warn(msg);
110: } else {
111: if ((getAlias() == null)
112: || !getAlias().equalsIgnoreCase(keygen.getAlias())) {
113: String msg = "Merge table has different key generator: "
114: + getAlias() + " / " + keygen.getAlias();
115: LOG.warn(msg);
116: }
117: }
118: }
119:
120: //--------------------------------------------------------------------------
121:
122: /**
123: * {@inheritDoc}
124: */
125: public final boolean equals(final Object other) {
126: if (other == this ) {
127: return true;
128: }
129: if (other == null) {
130: return false;
131: }
132: if (other.getClass() != this .getClass()) {
133: return false;
134: }
135:
136: KeyGenerator kg = (KeyGenerator) other;
137: return equals(getName(), kg.getName())
138: && equals(_alias, kg._alias)
139: && equals(_table, kg._table);
140: }
141:
142: /**
143: * {@inheritDoc}
144: */
145: public final int hashCode() {
146: int hashCode = 0;
147: if (getName() != null) {
148: hashCode += getName().hashCode();
149: }
150: hashCode *= HASHFACTOR;
151: if (_alias != null) {
152: hashCode += _alias.hashCode();
153: }
154: hashCode *= HASHFACTOR;
155: if (_table != null) {
156: hashCode += _table.hashCode();
157: }
158: return hashCode;
159: }
160:
161: //--------------------------------------------------------------------------
162: }
|