All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dk.eobjects.metamodel.schema.Column Maven / Gradle / Ivy

Go to download

The eobjects.dk MetaModel is a common domain model, query-engine and optimizer for different kinds of datastores.

The newest version!
/*
 * Copyright 2008 eobjects.dk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package dk.eobjects.metamodel.schema;

import java.io.Serializable;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * Represents a column and it's metadata description. Columns reside within a
 * Table and can be used as keys for relationships between tables.
 * 
 * @see Table
 * @see Relationship
 */
public class Column implements Serializable, Comparable {

	private static final long serialVersionUID = -353183696233890927L;
	private int _columnNumber;
	private String _name;
	private ColumnType _type;
	private Table _table;
	private Boolean _nullable = null;
	private String _remarks;
	private boolean _indexed = false;
	private Integer _columnSize = null;
	private String _nativeType = null;
	private String _quoteString = null;

	public Column() {
	}

	public Column(String name) {
		this();
		setName(name);
	}

	public Column(String name, ColumnType type) {
		this(name);
		setType(type);
	}

	public Column(String name, ColumnType type, Table table, int columnNumber,
			Boolean nullable) {
		this(name, type);
		setColumnNumber(columnNumber);
		setTable(table);
		setNullable(nullable);
	}

	/**
	 * Returns the column number or index. Note: This column number is 0-based
	 * whereas the JDBC is 1-based.
	 */
	public int getColumnNumber() {
		return _columnNumber;
	}

	public Column setColumnNumber(int columnNumber) {
		_columnNumber = columnNumber;
		return this;
	}

	/**
	 * Returns the Column Name
	 */
	public String getName() {
		return _name;
	}

	public Column setName(String name) {
		_name = name;
		return this;
	}

	/**
	 * Gets the type of the column
	 */
	public ColumnType getType() {
		return _type;
	}

	public Column setType(ColumnType type) {
		_type = type;
		return this;
	}

	/**
	 * Gets the table for which this column belong
	 */
	public Table getTable() {
		return _table;
	}

	public Column setTable(Table table) {
		_table = table;
		return this;
	}

	public Boolean isNullable() {
		return _nullable;
	}

	public Column setNullable(Boolean nullable) {
		_nullable = nullable;
		return this;
	}

	public String getRemarks() {
		return _remarks;
	}

	public void setRemarks(String remarks) {
		_remarks = remarks;
	}

	public Integer getColumnSize() {
		return _columnSize;
	}

	public Column setColumnSize(Integer columnSize) {
		_columnSize = columnSize;
		return this;
	}

	public String getNativeType() {
		return _nativeType;
	}

	public Column setNativeType(String nativeType) {
		_nativeType = nativeType;
		return this;
	}

	public boolean isIndexed() {
		return _indexed;
	}

	public Column setIndexed(boolean indexed) {
		_indexed = indexed;
		return this;
	}

	public String getQuote() {
		return _quoteString;
	}

	public Column setQuote(String quoteString) {
		_quoteString = quoteString;
		return this;
	}

	public String getQuotedName() {
		if (_quoteString != null) {
			return _quoteString + getName() + _quoteString;
		}
		return getName();
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
				.append("name", _name).append("columnNumber", _columnNumber)
				.append("type", _type).append("nullable", _nullable).append(
						"indexed", isIndexed()).append("nativeType",
						_nativeType).append("columnSize", _columnSize)
				.toString();
	}

	@Override
	public int hashCode() {
		HashCodeBuilder hcb = new HashCodeBuilder();
		hcb.append(_nullable).append(_columnNumber).append(_name).append(_type)
				.append(isIndexed()).append(_nativeType).append(_columnSize);
		return hcb.toHashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (obj instanceof Column) {
			Column that = (Column) obj;
			EqualsBuilder eb = new EqualsBuilder().append(this.getName(),
					that.getName()).append(this.getColumnNumber(),
					that.getColumnNumber()).append(this.getType(),
					that.getType()).append(this.isIndexed(), that.isIndexed())
					.append(this.getNativeType(), that.getNativeType()).append(
							this.getColumnSize(), that.getColumnSize());
			if (this.getTable() != null && that.getTable() != null) {
				eb.append(this.getTable().getName(), that.getTable().getName());
			}
			return eb.isEquals();
		}
		return false;
	}

	public int compareTo(Column that) {
		CompareToBuilder ctb = new CompareToBuilder();
		ctb.append(this.getColumnNumber(), that.getColumnNumber());
		ctb.append(this.getTable(), that.getTable());
		return ctb.toComparison();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy