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

org.skife.jdbi.v2.sqlobject.CGLIBDispatchBuilder Maven / Gradle / Ivy

Go to download

jDBI is designed to provide convenient tabular data access in Java(tm). It uses the Java collections framework for query results, provides a convenient means of externalizing sql statements, and provides named parameter support for any database being used.

There is a newer version: 2.78
Show newest version
/*
 * Copyright (C) 2004 - 2014 Brian McCallister
 *
 * 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 org.skife.jdbi.v2.sqlobject;

import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.CallbackFilter;
import net.sf.cglib.proxy.NoOp;

import java.lang.reflect.Method;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

class CGLIBDispatchBuilder
{
    private final SortedMap callmap;
    private final Callback defalt;

    private CGLIBDispatchBuilder(Callback defalt, SortedMap callmap)
    {
        this.callmap = callmap;
        this.defalt = defalt;
    }

    public static CGLIBDispatchBuilder create()
    {
        return new CGLIBDispatchBuilder(NoOp.INSTANCE, new TreeMap(MC));
    }

    public CGLIBDispatchBuilder withDefault(Callback defalt)
    {
        return new CGLIBDispatchBuilder(defalt, this.callmap);
    }

    public CGLIBDispatchBuilder addCallbacks(Map additions)
    {
        TreeMap newmap = new TreeMap(MC);
        newmap.putAll(this.callmap);
        newmap.putAll(additions);
        return new CGLIBDispatchBuilder(this.defalt, newmap);
    }

    public CGLIBDispatchBuilder addCallback(Method method, Callback callback)
    {
        TreeMap newmap = new TreeMap(MC);
        newmap.putAll(this.callmap);
        newmap.put(method, callback);
        return new CGLIBDispatchBuilder(this.defalt, newmap);
    }

    public Callback[] getCallbacks()
    {
        Callback[] callbacks = new Callback[callmap.size() + 1];
        callbacks[callmap.size()] = defalt;
        int idx = 0;
        for (Callback callback : callmap.values()) {
            callbacks[idx++] = callback;
        }
        return callbacks;
    }

    public CallbackFilter getFilter()
    {
        final Map indices = new LinkedHashMap();
        int idx = 0;
        for (Method method : callmap.keySet()) {
            indices.put(method, idx++);
        }
        final int defalt_idx = idx;
        return new MyCallbackFilter(indices, defalt_idx);
    }

    private static final Comparator MC = new Comparator()
    {
        @Override
        public int compare(Method left, Method right)
        {
            return left.toString().compareTo(right.toString());
        }
    };

    private static class MyCallbackFilter implements CallbackFilter
    {
        private final Map indices;
        private final int defalt_idx;

        public MyCallbackFilter(Map indices, int defalt_idx)
        {
            this.indices = indices;
            this.defalt_idx = defalt_idx;
        }

        @Override
        public int accept(Method method)
        {
            if (indices.containsKey(method)) {
                return indices.get(method);
            }
            else {
                return defalt_idx;
            }
        }

        @Override
        public int hashCode()
        {
            return indices.hashCode() + defalt_idx;
        }

        @Override
        public boolean equals(Object o)
        {
            if (!(o instanceof MyCallbackFilter)) {
                return false;
            }
            MyCallbackFilter other = (MyCallbackFilter) o;
            return this.indices.equals(other.indices) && this.defalt_idx == other.defalt_idx;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy