/*
* This file is part of rockframework.
*
* rockframework is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* rockframework is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>;.
*/
package br.net.woodstock.rockframework.domain.persistence;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import br.net.woodstock.rockframework.core.RockFrameworkLogger;
import br.net.woodstock.rockframework.core.RockFrameworkVersion;
import br.net.woodstock.rockframework.core.jdbc.Type;
import br.net.woodstock.rockframework.core.util.Assert;
import br.net.woodstock.rockframework.core.util.EqualsBuilder;
import br.net.woodstock.rockframework.core.util.HashCodeBuilder;
import br.net.woodstock.rockframework.core.util.ToStringBuilder;
import br.net.woodstock.rockframework.core.utils.Conditions;
public class FilterParameter implements Serializable {
private static final long serialVersionUID = RockFrameworkVersion.VERSION;
private int index = -1;
private String name;
private Object value;
private Type type;
public FilterParameter(final String name, final Object value) {
this(name, value, null);
}
public FilterParameter(final String name, final Object value, final Type type) {
super();
Assert.notNull(name, "name");
this.name = name;
this.value = value;
this.type = type;
if ((value == null) && (type == null)) {
RockFrameworkLogger.getLogger().warn("Value and type null may cause unexpected errors");
}
}
public FilterParameter(final int index, final Object value) {
this(index, value, null);
}
public FilterParameter(final int index, final Object value, final Type type) {
super();
Assert.greaterThan(index, 0, "index");
this.index = index;
this.value = value;
this.type = type;
if ((value == null) && (type == null)) {
RockFrameworkLogger.getLogger().warn("Value and type null may cause unexpected errors");
}
}
public int getIndex() {
return this.index;
}
public String getName() {
return this.name;
}
public Object getValue() {
return this.value;
}
public Type getType() {
return this.type;
}
// Aux
public boolean isIndexed() {
if (this.getIndex() >= 0) {
return true;
}
return false;
}
public boolean isNamed() {
if (Conditions.isNotEmpty(this.getName())) {
return true;
}
return false;
}
public static List<FilterParameter> fromStringKeyMap(final Map<String, Object> map) {
if (map == null) {
return null;
}
List<FilterParameter> list = new ArrayList<FilterParameter>();
for (Entry<String, Object> entry : map.entrySet()) {
FilterParameter fp = new FilterParameter(entry.getKey(), entry.getValue());
list.add(fp);
}
return list;
}
public static List<FilterParameter> fromIntegerKeyMap(final Map<Integer, Object> map) {
if (map == null) {
return null;
}
List<FilterParameter> list = new ArrayList<FilterParameter>();
for (Entry<Integer, Object> entry : map.entrySet()) {
FilterParameter fp = new FilterParameter(entry.getKey().intValue(), entry.getValue());
list.add(fp);
}
return list;
}
// Object
@Override
public int hashCode() {
return new HashCodeBuilder().add(this.getIndex()).add(this.getName()).add(this.getValue()).add(this.getType()).build().intValue();
}
@Override
public boolean equals(final Object obj) {
if (!(obj instanceof FilterParameter)) {
return false;
}
FilterParameter other = (FilterParameter) obj;
return new EqualsBuilder().add(this.getIndex(), other.getIndex()).add(this.getName(), other.getName()).add(this.getValue(), other.getValue()).add(this.getType(), other.getType()).build().booleanValue();
}
@Override
public String toString() {
return new ToStringBuilder(this.getClass()).add("index", this.getIndex()).add("name", this.getName()).add("value", this.getValue()).add("type", this.getType()).build();
}
}