001/* 
002 * JKNIV, whinstone one contract to access your database.
003 * 
004 * Copyright (C) 2017, the original author or authors.
005 *
006 * This library is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 2.1 of the License.
010 * 
011 * This library is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 * 
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with this library; if not, write to the Free Software Foundation, Inc., 
018 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
019 */
020package net.sf.jkniv.whinstone;
021
022import java.util.Collection;
023
024/**
025 * Represents the parameter from {@link Queryable}.
026 * 
027 * @author Alisson Gomes
028 * @since 0.6.0
029 */
030public class Param
031{
032    private final static int NO_INDEX = -1;
033    private Object value;
034    private Object valueAs;// converted value
035    private int index;
036    private String name;
037
038    public Param() 
039    {
040        super();
041        this.index = NO_INDEX;
042    }
043
044    public Param(Object value) 
045    {
046        this(value, value, "?", NO_INDEX);
047    }
048
049    public Param(Object value, int index)
050    {
051        this(value, value, "?", index);
052    }
053
054    public Param(Object value, String name)
055    {
056        this(value, value, name, NO_INDEX);
057    }
058
059    public Param(Object value, Object valueAs, String name)
060    {
061        this(value, valueAs, name, NO_INDEX);
062    }
063
064    public Param(Object value, String name, int index)
065    {
066        this(value, value, name, index);
067    }
068
069    public Param(Object value, Object valueAs, String name, int index)
070    {
071        this();
072        this.value = value;
073        this.valueAs = valueAs;
074        this.index = index;
075        this.name = name;
076    }
077
078    public Object getValue()
079    {
080        return value;
081    }
082    
083    /**
084     * get the value converted when there is one, otherwise the original value.
085     * @return a converted value or original value.
086     */
087    public Object getValueAs()
088    {
089        return valueAs;
090    }
091
092    public int getIndex()
093    {
094        return index;
095    }
096
097    public String getName()
098    {
099        return name;
100    }
101    
102    public void setIndex(int index)
103    {
104        if (this.index == NO_INDEX)
105            this.index = index;
106    }
107
108    public Param[] asArray()
109    {
110        Param[] params = null;
111        if (isArray())
112            params = ofArray();
113        else if (isCollection())
114            params = ofCollection();
115        else
116            throw new IllegalStateException("Cannot retrieve the parameter as array because is not a collection or array");
117        return params;
118    }
119    
120    public boolean isArray()
121    {
122        return (value != null && value.getClass().isArray());
123    }
124    
125    public boolean isCollection()
126    {
127        return (value instanceof Collection);
128    }
129    
130    private Param[] ofCollection()
131    {
132        Collection<?> paramsAsCollection = (Collection<?>)this.value;
133        Param[] params = new Param[paramsAsCollection.size()];
134        int i = 0;
135        for(Object o : paramsAsCollection)
136        {
137            params[i] = new Param(o, this.name, i);
138            i++;
139        }
140        return params;
141    }
142
143    private Param[] ofArray()
144    {
145        Object[] paramsAsArray = (Object[])this.value;
146        Param[] params = new Param[paramsAsArray.length];
147        int i = 0;
148        for(Object o : paramsAsArray)
149        {
150            params[i] = new Param(o, this.name, i);
151            i++;
152        }
153        return params;
154    }    
155    
156    @Override
157    public int hashCode()
158    {
159        final int prime = 31;
160        int result = 1;
161        result = prime * result + ((name == null) ? 0 : name.hashCode());
162        result = prime * result + ((value == null) ? 0 : value.hashCode());
163        return result;
164    }
165
166    @Override
167    public boolean equals(Object obj)
168    {
169        if (this == obj)
170            return true;
171        if (obj == null)
172            return false;
173        if (getClass() != obj.getClass())
174            return false;
175        Param other = (Param) obj;
176        if (name == null)
177        {
178            if (other.name != null)
179                return false;
180        }
181        else if (!name.equals(other.name))
182            return false;
183        
184        if (value == null)
185        {
186            if (other.value != null)
187                return false;
188        }
189        else if (!value.equals(other.value))
190            return false;
191        return true;
192    }
193    
194    
195    @Override
196    public String toString()
197    {
198        return "Param [value=" + value + ", index=" + index + ", name=" + name + "]";
199    }
200    
201    
202}