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

org.jooq.impl.AbstractNamed Maven / Gradle / Ivy

There is a newer version: 3.19.15
Show newest version
/*
 * 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.
 *
 * Other licenses:
 * -----------------------------------------------------------------------------
 * Commercial licenses for this work are available. These replace the above
 * Apache-2.0 license and offer limited warranties, support, maintenance, and
 * commercial database integrations.
 *
 * For more information, please visit: http://www.jooq.org/licenses
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 *
 */

package org.jooq.impl;

import static org.jooq.impl.AbstractName.NO_NAME;

import java.util.ArrayList;
import java.util.List;

import org.jooq.Comment;
import org.jooq.Name;
import org.jooq.Named;
import org.jooq.tools.StringUtils;

/**
 * @author Lukas Eder
 */
abstract class AbstractNamed extends AbstractQueryPart implements Named {

    private final Name    name;
    private final Comment comment;

    AbstractNamed(Name name, Comment comment) {
        this.name = name == null ? AbstractName.NO_NAME : name;
        this.comment = comment == null ? CommentImpl.NO_COMMENT : comment;
    }

    // -------------------------------------------------------------------------
    // The Named API
    // -------------------------------------------------------------------------

    @Override
    public final String getName() {
        return StringUtils.defaultIfNull(getQualifiedName().last(), "");
    }

    /* [#7172] For lazy initialisation reasons, some subtypes need to override this method */
    @Override
    public /* non-final */ Name getQualifiedName() {
        return name;
    }

    @Override
    public final Name getUnqualifiedName() {
        return name.unqualifiedName();
    }

    @Override
    public final String getComment() {
        return comment.getComment();
    }

    @Override
    public final Comment getCommentPart() {
        return comment;
    }

    // -------------------------------------------------------------------------
    // The Object API
    // -------------------------------------------------------------------------

    @Override
    public int hashCode() {

        // [#1938] This is a much more efficient hashCode() implementation
        // compared to that of standard QueryParts
        return getQualifiedName().hashCode();
    }

    @Override
    public boolean equals(Object that) {
        if (this == that)
            return true;
        if (that == null)
            return false;

        // [#2144] Non-equality can be decided early, without executing the
        // rather expensive implementation of AbstractQueryPart.equals()
        if (that instanceof AbstractNamed n)
            if (!getQualifiedName().equals(n.getQualifiedName()))
                return false;

        return super.equals(that);
    }

    // -------------------------------------------------------------------------
    // Utilities
    // -------------------------------------------------------------------------

    static final Name nameOrDefault(Named named) {
        return named == null ? NO_NAME : named.getUnqualifiedName();
    }

    static final Name qualify(Named qualifier, Name name) {
        // [#9820] [#11292] name == null || name.empty() are special cases that
        //                  may appear when using unnamed constraint declarations.
        //                  Their unnamedness must not be changed, nor qualified!
        return qualifier == null || name == null || name.empty() || name.qualified() ? name : qualifier.getQualifiedName().append(name);
    }

    static final  List findAll(String name, Iterable in) {
        List result = new ArrayList<>();

        for (N n : in)
            if (n.getName().equals(name))
                result.add(n);

        return result;
    }

    static final  List findAll(Name name, Iterable in) {
        List result = new ArrayList<>();

        for (N n : in)
            if (n.getQualifiedName().equals(name) || n.getUnqualifiedName().equals(name))
                result.add(n);

        return result;
    }

    static final  N find(String name, Iterable in) {
        return Tools.findAny(in, n -> n.getName().equals(name));
    }

    static final  N find(Name name, Iterable in) {
        N unqualified = null;

        for (N n : in)
            if (n.getQualifiedName().equals(name))
                return n;
            else if (unqualified == null && n.getUnqualifiedName().equals(name.unqualifiedName()))
                unqualified = n;

        return unqualified;
    }

    static final  N findIgnoreCase(String name, Iterable in) {
        return Tools.findAny(in, n -> n.getName().equalsIgnoreCase(name));
    }

    static final  N findIgnoreCase(Name name, Iterable in) {
        N unqualified = null;

        for (N n : in)
            if (n.getQualifiedName().equalsIgnoreCase(name))
                return n;
            else if (unqualified == null && n.getUnqualifiedName().equalsIgnoreCase(name.unqualifiedName()))
                unqualified = n;

        return unqualified;
    }

    // -------------------------------------------------------------------------
    // XXX: Query Object Model
    // -------------------------------------------------------------------------

    @Override
    public final Name $name() {
        return getQualifiedName();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy