001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of 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,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.storage.sql;
020
021import java.io.Serializable;
022
023/**
024 * The identification of a {@link Row} (table name and id) without the row content itself.
025 * <p>
026 * This class is sometimes used as a marker for an "absent" row in the database, when mixed with actual {@link Row}s.
027 */
028public class RowId implements Serializable {
029
030    private static final long serialVersionUID = 1L;
031
032    public final String tableName;
033
034    public Serializable id;
035
036    public RowId(RowId rowId) {
037        tableName = rowId.tableName;
038        id = rowId.id;
039    }
040
041    public RowId(String tableName, Serializable id) {
042        this.tableName = tableName == null ? null : tableName.intern();
043        this.id = id;
044    }
045
046    @Override
047    public int hashCode() {
048        int result = 31 + (id == null ? 0 : id.hashCode());
049        return 31 * result + tableName.hashCode();
050    }
051
052    @Override
053    public boolean equals(Object other) {
054        if (other instanceof RowId) {
055            return equals((RowId) other);
056        }
057        return false;
058    }
059
060    private boolean equals(RowId other) {
061        if (other == this) {
062            return true;
063        }
064        if (id == null) {
065            if (other.id != null) {
066                return false;
067            }
068        } else if (!id.equals(other.id)) {
069            return false;
070        }
071        return tableName.equals(other.tableName);
072    }
073
074    @Override
075    public String toString() {
076        return getClass().getSimpleName() + '(' + tableName + ", " + id + ')';
077    }
078
079}