
com.magictools.core.lang.Segment Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of magic-tools-all Show documentation
Show all versions of magic-tools-all Show documentation
magic-tools 是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。
The newest version!
package com.magictools.core.lang;
import com.magictools.core.convert.Convert;
import com.magictools.core.util.NumberUtil;
import java.lang.reflect.Type;
/**
* 片段表示,用于表示文本、集合等数据结构的一个区间。
* @param 数字类型,用于表示位置index
*
* @author looly
* @since 5.5.3
*/
public interface Segment {
/**
* 获取起始位置
*
* @return 起始位置
*/
T getStartIndex();
/**
* 获取结束位置
*
* @return 结束位置
*/
T getEndIndex();
/**
* 片段长度,默认计算方法为abs({@link #getEndIndex()} - {@link #getEndIndex()})
*
* @return 片段长度
*/
default T length(){
final T start = Assert.notNull(getStartIndex(), "Start index must be not null!");
final T end = Assert.notNull(getEndIndex(), "End index must be not null!");
return Convert.convert((Type) start.getClass(), NumberUtil.sub(end, start).abs());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy