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

com.blazebit.persistence.impl.ParameterManager Maven / Gradle / Ivy

There is a newer version: 1.2.0-Alpha1
Show newest version
/*
 * Copyright 2014 Blazebit.
 *
 * 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 com.blazebit.persistence.impl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.persistence.Parameter;
import javax.persistence.TemporalType;

import com.blazebit.persistence.impl.expression.ParameterExpression;

/**
 *
 * @author Moritz Becker
 * @since 1.0
 */
public class ParameterManager {

    private static final String prefix = "param_";
    private int counter;
    private final Map parameters = new HashMap();
    private static final Object REGISTERED_PLACEHOLDER = new Object();

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public Parameter getParameter(String parameterName) {
        if (parameterName == null) {
            throw new NullPointerException("parameterName");
        }
        if (!containsParameter(parameterName)) {
            return null;
        }

        Object value = getParameterValue(parameterName);

        return new ParameterImpl(value == null ? null : value.getClass(), parameterName);
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    public Set> getParameters() {
        Set> result = new HashSet>();

        for (Map.Entry paramEntry : parameters.entrySet()) {
            Class paramClass = paramEntry.getValue() == null || paramEntry.getValue() == REGISTERED_PLACEHOLDER ? null : paramEntry.getValue()
                .getClass();
            result.add(new ParameterImpl(paramClass, paramEntry.getKey()));
        }
        return result;
    }

    public boolean containsParameter(String parameterName) {
        if (parameterName == null) {
            throw new NullPointerException("parameterName");
        }
        return parameters.containsKey(parameterName);
    }

    public boolean isParameterSet(String parameterName) {
        if (parameterName == null) {
            throw new NullPointerException("parameterName");
        }
        return parameters.containsKey(parameterName) && parameters.get(parameterName) != REGISTERED_PLACEHOLDER;
    }

    public Object getParameterValue(String parameterName) {
        if (parameterName == null) {
            throw new NullPointerException("parameterName");
        }
        Object o = parameters.get(parameterName);
        return o == REGISTERED_PLACEHOLDER ? null : o;
    }

    public ParameterExpression addParameterExpression(Object o) {
        String name = addParameter(o);
        return new ParameterExpression(name, o);
    }

    public String addParameter(Object o) {
        if (o == null) {
            throw new NullPointerException();
        }
        String name = prefix + counter++;
        parameters.put(name, o);
        return name;
    }

    public void addParameterMapping(String parameterName, Object o) {
        if (parameterName == null) {
            throw new NullPointerException("parameterName");
        }
        parameters.put(parameterName, o);
    }

    public void registerParameterName(String parameterName) {
        if (parameterName == null) {
            throw new NullPointerException("parameterName");
        }
        if (!parameters.containsKey(parameterName)) {
            parameters.put(parameterName, REGISTERED_PLACEHOLDER);
        }
    }

    public void satisfyParameter(String parameterName, Object parameterValue) {
        if (parameterName == null) {
            throw new NullPointerException("parameterName");
        }
        if (!parameters.containsKey(parameterName)) {
            throw new IllegalArgumentException(String.format("Parameter name \"%s\" does not exist", parameterName));
        }
        parameters.put(parameterName, parameterValue);
    }

    // TODO: needs equals-hashCode implementation

    private class ParameterImpl implements Parameter {

        private final Class paramClass;
        private final String paramName;

        public ParameterImpl(Class paramClass, String paramName) {
            this.paramClass = paramClass;
            this.paramName = paramName;
        }

        @Override
        public String getName() {
            return paramName;
        }

        @Override
        public Integer getPosition() {
            return null;
        }

        @Override
        public Class getParameterType() {
            return paramClass;
        }

    }

    static class TemporalCalendarParameterWrapper {

        private final Calendar value;
        private final TemporalType type;

        public TemporalCalendarParameterWrapper(Calendar value, TemporalType type) {
            this.value = value;
            this.type = type;
        }

        public Calendar getValue() {
            return value;
        }

        public TemporalType getType() {
            return type;
        }
    }

    static class TemporalDateParameterWrapper {

        private final Date value;
        private final TemporalType type;

        public TemporalDateParameterWrapper(Date value, TemporalType type) {
            this.value = value;
            this.type = type;
        }

        public Date getValue() {
            return value;
        }

        public TemporalType getType() {
            return type;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy