001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Florent Guillaume
011 */
012package org.nuxeo.ecm.core.storage.sql;
013
014import java.io.Serializable;
015
016/**
017 * The identification of a {@link Row} (table name and id) without the row content itself.
018 * <p>
019 * This class is sometimes used as a marker for an "absent" row in the database, when mixed with actual {@link Row}s.
020 */
021public class RowId implements Serializable {
022
023    private static final long serialVersionUID = 1L;
024
025    public final String tableName;
026
027    public Serializable id;
028
029    public RowId(RowId rowId) {
030        tableName = rowId.tableName;
031        id = rowId.id;
032    }
033
034    public RowId(String tableName, Serializable id) {
035        this.tableName = tableName == null ? null : tableName.intern();
036        this.id = id;
037    }
038
039    @Override
040    public int hashCode() {
041        int result = 31 + (id == null ? 0 : id.hashCode());
042        return 31 * result + tableName.hashCode();
043    }
044
045    @Override
046    public boolean equals(Object other) {
047        if (other instanceof RowId) {
048            return equals((RowId) other);
049        }
050        return false;
051    }
052
053    private boolean equals(RowId other) {
054        if (other == this) {
055            return true;
056        }
057        if (id == null) {
058            if (other.id != null) {
059                return false;
060            }
061        } else if (!id.equals(other.id)) {
062            return false;
063        }
064        return tableName.equals(other.tableName);
065    }
066
067    @Override
068    public String toString() {
069        return getClass().getSimpleName() + '(' + tableName + ", " + id + ')';
070    }
071
072}