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

org.rx.bean.DataRange Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package org.rx.bean;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import org.rx.core.Reflects;
import org.rx.core.StringBuilder;
import org.rx.core.Strings;

import java.io.Serializable;

import static org.rx.core.Extends.require;

@Data
@NoArgsConstructor
public class DataRange> implements Serializable {
    private static final long serialVersionUID = 2698228026798507997L;
    static final String DELIMITER = " - ";

    public static > DataRange of(String expr, Class type) {
        String[] vals = Strings.split(expr, DELIMITER, 2);
        return new DataRange<>(Reflects.changeType(vals[0], type), Reflects.changeType(vals[1], type));
    }

    public T start;
    public T end;

    public DataRange(@NonNull T start, @NonNull T end) {
        require(start, start.compareTo(end) <= 0);

        this.start = start;
        this.end = end;
    }

    public boolean has(T data) {
        return start.compareTo(data) <= 0 && end.compareTo(data) > 0;
    }

    @Override
    public String toString() {
        StringBuilder buf = new StringBuilder();
        if (start != null) {
            buf.append(start.toString());
        }
        buf.append(DELIMITER);
        if (end != null) {
            buf.append(end.toString());
        }
        return buf.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy