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

org.jsimpledb.ReferenceConverter Maven / Gradle / Ivy

The newest version!

/*
 * Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
 */

package org.jsimpledb;

import com.google.common.base.Converter;

import org.jsimpledb.core.ObjId;

/**
 * Converts {@link ObjId}s into {@link JObject}s and vice-versa.
 */
class ReferenceConverter extends Converter {

    private final JTransaction jtx;
    private final Class type;

    ReferenceConverter(JTransaction jtx, Class type) {
        assert jtx != null;
        assert type != null;
        this.jtx = jtx;
        this.type = type;
    }

    @Override
    protected ObjId doForward(T jobj) {
        if (jobj == null)
            return null;
        return ((JObject)jobj).getObjId();
    }

    @Override
    protected T doBackward(ObjId id) {
        if (id == null)
            return null;
        final JObject jobj = this.jtx.get(id);
        return this.type.cast(jobj);
    }

// Object

    @Override
    public boolean equals(Object obj) {
        if (obj == this)
            return true;
        if (obj == null || obj.getClass() != this.getClass())
            return false;
        final ReferenceConverter that = (ReferenceConverter)obj;
        return this.jtx == that.jtx;                                    // we don't include type in the comparison
    }

    @Override
    public int hashCode() {
        return this.jtx.hashCode();
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName() + "[type=" + this.type + ",jtx=" + this.jtx + "]";
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy