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

org.unlaxer.CursorRange Maven / Gradle / Ivy

package org.unlaxer;

import java.util.stream.IntStream;

import org.unlaxer.Cursor.EndExclusiveCursor;
import org.unlaxer.Cursor.StartInclusiveCursor;

public class CursorRange implements Comparable{
	
	public final StartInclusiveCursor startIndexInclusive;
	public final EndExclusiveCursor endIndexExclusive;
	
	public CursorRange(StartInclusiveCursor startIndexInclusive, EndExclusiveCursor endIndexExclusive) {
		super();
		this.startIndexInclusive = startIndexInclusive;
		this.endIndexExclusive = endIndexExclusive;
	}
	public CursorRange(StartInclusiveCursor startIndexInclusive) {
		super();
		this.startIndexInclusive = startIndexInclusive;
		this.endIndexExclusive = new EndExclusiveCursorImpl(startIndexInclusive);
	}
	public CursorRange() {
		super();
		this.startIndexInclusive = new StartInclusiveCursorImpl();
		this.endIndexExclusive = new EndExclusiveCursorImpl();
	}
	
	public static CursorRange of(
	    CodePointIndex startIndexInclusive,
	    LineNumber startLineNumber,
      CodePointIndex endIndexExclusive,
      LineNumber endLineNumber
	    ) {
	  return new CursorRange(
        new StartInclusiveCursorImpl().setLineNumber(startLineNumber).setPosition(startIndexInclusive),
        new EndExclusiveCursorImpl().setLineNumber(endLineNumber).setPosition(endIndexExclusive)
	  );
	  
	}
	
	public final StartInclusiveCursor startIndexInclusive() {
		return startIndexInclusive;
	}
	
	public final EndExclusiveCursor endIndexExclusive() {
		return endIndexExclusive;
	}
	
	public boolean isSingle() {
		return startIndexInclusive.getPosition() == endIndexExclusive.getPosition();
	}

	public boolean match(CodePointIndex position){
		return position.ge(startIndexInclusive.getPosition()) 
		    && position.lt(endIndexExclusive.getPosition());
	}

	
  public boolean lt(CodePointIndex position){
	  return position.ge(endIndexExclusive.getPosition());
	}

	public boolean lessThan(CodePointIndex position){
		return position.ge(endIndexExclusive.getPosition());
	}
	
	public boolean graterThan(CodePointIndex position){
		return position.lt(startIndexInclusive.getPosition());
	}
	
  public boolean gt(CodePointIndex position){
    return position.lt(startIndexInclusive.getPosition());
	}
	
	public boolean lessThan(CursorRange other){
		return other.startIndexInclusive.getPosition().ge(endIndexExclusive.getPosition());
	}
	
  public boolean lt(CursorRange other){
	  return other.startIndexInclusive.getPosition().ge(endIndexExclusive.getPosition());
	}
	
  public boolean gt(CursorRange other){
    return other.endIndexExclusive.getPosition().le(startIndexInclusive.getPosition());
  }
  
	public boolean graterThan(CursorRange other){
		return other.endIndexExclusive.getPosition().le(startIndexInclusive.getPosition());
	}
	
	public RangesRelation relation(CursorRange other){
		CodePointIndex otherStart = other.startIndexInclusive.getPosition();
		CodePointIndex otherEnd= other.endIndexExclusive.getPosition();
		if(startIndexInclusive.getPosition().eq(otherStart) && 
		    endIndexExclusive.getPosition().eq(otherEnd)){
			
			return RangesRelation.equal;
			
		}else if(startIndexInclusive.getPosition().ge(otherStart) && 
		    endIndexExclusive.getPosition().le(otherEnd)){
			
			return RangesRelation.outer;
			
		}else if(startIndexInclusive.getPosition().le(otherStart) && 
		    endIndexExclusive.getPosition().ge(otherEnd)){
			
			return RangesRelation.inner;
			
		}else if(startIndexInclusive.getPosition().ge(otherEnd) || 
		    endIndexExclusive.getPosition().le(otherStart)){
			
			return RangesRelation.notCrossed;
		}
		return RangesRelation.crossed;
	}
	
	public IntStream asIntStream() {
		return IntStream.range(startIndexInclusive.getPosition().value(), 
		    endIndexExclusive.getPosition().value());
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + endIndexExclusive.getPosition().value();
		result = prime * result + startIndexInclusive.getPosition().value();
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CursorRange other = (CursorRange) obj;
		if (endIndexExclusive.getPosition().value() != other.endIndexExclusive.getPosition().value())
			return false;
		if (startIndexInclusive.getPosition().value() != other.startIndexInclusive.getPosition().value())
			return false;
		return true;
	}
	
	
	@Override
	public int compareTo(CursorRange other) {
		int value = startIndexInclusive.getPosition().value() - other.startIndexInclusive.getPosition().value();
		if(value == 0) {
			return endIndexExclusive.getPosition().value() - other.endIndexExclusive.getPosition().value();
		}
		return value;
	}
	@Override
	public String toString() {
		return isSingle() ?
			"["+startIndexInclusive.toString()+"]":
			"["+startIndexInclusive.toString()+","+endIndexExclusive.toString()+"]";
				
	}
	public static CursorRange invalidRange() {
		return invalidRange;
	}
	
	static final CursorRange invalidRange = new CursorRange();
	
	public Range toRange() {
	   return new Range(startIndexInclusive.getPosition() , endIndexExclusive.getPosition());
	}
	
//	public CursorRange plus(CodePointOffset codePointOffset) {
//	  lineNumerの更新も行う
//	  position in lineはそもそもsubシーケンスでの扱いをどうするか?subシーケンスの中で全体的に新たなものになるか。
//	  rootは良いとしてもparentの中での扱いはどうするか?
//	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy