package me.chyxion.tigon.mybatis;

import lombok.Getter;
import java.util.Arrays;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;

/**
 * @author Shaun Chyxion <br>
 * chyxion@163.com <br>
 * 2017/1/24 10:23
 */
@Getter
class Criterion implements Serializable {
    private static final long serialVersionUID = 1L;

    private final CriterionType type;
    private final String col;
    private final Collection<?> values;
    private final Search search;

    Criterion(CriterionType type, Search search) {
        this.type = type;
        this.col = null;
        this.values = Collections.emptyList();
        this.search = search;
    }

    Criterion(CriterionType type, String col, Object value) {
        this(type, col, Arrays.asList(value));
    }

    Criterion(CriterionType type, String col, Object[] values) {
        this(type, col, Arrays.asList(values));
    }

    Criterion(CriterionType type, String col, Collection<?> values) {
        this.type = type;
        this.col = col;
        this.values = values;
        this.search = null;
    }

    @SuppressWarnings("unchecked")
    <T> T getValue() {
        return values.isEmpty() ? null : (T) values.iterator().next();
    }
}
