org.rx.bean.DataRange Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxlib Show documentation
Show all versions of rxlib Show documentation
A set of utilities for Java
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();
}
}