cn.guoyukun.spring.jpa.entity.search.SearchOperator.java Source code

Java tutorial

Introduction

Here is the source code for cn.guoyukun.spring.jpa.entity.search.SearchOperator.java

Source

/**
 * Copyright (c) 2005-2012 https://github.com/zhangkaitao
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package cn.guoyukun.spring.jpa.entity.search;

import cn.guoyukun.spring.jpa.entity.search.exception.SearchException;

import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;

/**
 * <p>?</p>
 */
public enum SearchOperator {
    eq("", "="), ne("?", "!="), gt("", ">"), gte("", ">="), lt("?", "<"), lte(
            "?", "<="), prefixLike("??", "like"), prefixNotLike("???",
                    "not like"), suffixLike("??", "like"), suffixNotLike("???",
                            "not like"), like("?", "like"), notLike("??", "not like"), isNull(
                                    "", "is null"), isNotNull("?", "is not null"), in("?",
                                            "in"), notIn("??", "not in"), custom("", null);

    private final String info;
    private final String symbol;

    SearchOperator(final String info, String symbol) {
        this.info = info;
        this.symbol = symbol;
    }

    public String getInfo() {
        return info;
    }

    public String getSymbol() {
        return symbol;
    }

    public static String toStringAllOperator() {
        return Arrays.toString(SearchOperator.values());
    }

    /**
     * ???
     *
     * @param operator
     * @return
     */
    public static boolean isAllowBlankValue(final SearchOperator operator) {
        return operator == SearchOperator.isNotNull || operator == SearchOperator.isNull;
    }

    public static SearchOperator valueBySymbol(String symbol) throws SearchException {
        symbol = formatSymbol(symbol);
        for (SearchOperator operator : values()) {
            if (operator.getSymbol().equals(symbol)) {
                return operator;
            }
        }

        throw new SearchException("SearchOperator not method search operator symbol : " + symbol);
    }

    private static String formatSymbol(String symbol) {
        if (StringUtils.isBlank(symbol)) {
            return symbol;
        }
        return symbol.trim().toLowerCase().replace("  ", " ");
    }
}