org.conqat.lib.commons.region.SimpleRegion Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of teamscale-lib-commons Show documentation
Show all versions of teamscale-lib-commons Show documentation
Provides common utility functions
The newest version!
/*
* Copyright (c) CQSE GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.conqat.lib.commons.region;
import java.io.Serializable;
import java.util.Comparator;
import java.util.Objects;
import org.conqat.lib.commons.test.IndexValueClass;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* A simple region with only start and end. If you also need a description string (origin), use
* {@link Region}. Both start and end positions are inclusive.
*/
@IndexValueClass(containedInBackup = true)
public class SimpleRegion implements Comparable, Serializable {
/** Version for serialization. */
private static final long serialVersionUID = 1;
/** The name of the JSON property name for {@link #start}. */
public static final String START_PROPERTY = "start";
/** The name of the JSON property name for {@link #end}. */
public static final String END_PROPERTY = "end";
/**
* Compares {@link SimpleRegion}s first by their start and then by their end.
*/
public static final Comparator START_AND_END_COMPARATOR = Comparator.comparing(SimpleRegion::getStart)
.thenComparing(SimpleRegion::getEnd);
/** Region start position (inclusive). */
@JsonProperty(START_PROPERTY)
private final int start;
/** Region end position (inclusive). */
@JsonProperty(END_PROPERTY)
private final int end;
/** Constructor. */
public SimpleRegion(int start, int end) {
this.start = start;
this.end = end;
}
/** Checks if the region contains a position */
public boolean containsPosition(int position) {
return (start <= position && end >= position);
}
/** Checks if two regions are overlapping */
public boolean overlaps(SimpleRegion r) {
// Region with smaller start value performs overlap check
if (r.start < start) {
return r.overlaps(this);
}
return (start <= r.start && end >= r.start);
}
/** Checks if two regions are adjacent */
public boolean adjacent(SimpleRegion r) {
// Region with smaller start value performs adjacency check
if (r.start < start) {
return r.adjacent(this);
}
return (end + 1 == r.start);
}
/**
* Gets the end position of the region. This may be less than start for an empty region (see also
* {@link #isEmpty()}).
*/
public int getEnd() {
return end;
}
/** Gets the start position of the region */
public int getStart() {
return start;
}
/**
* Gets the length of the region. Empty regions have a length of 0.
*/
public int getLength() {
if (isEmpty()) {
return 0;
}
return end - start + 1;
}
/** Returns whether this region is empty. */
public boolean isEmpty() {
return end < start;
}
/** {@inheritDoc} */
@Override
public String toString() {
return "[" + start + "-" + end + "]";
}
/** Compares regions by their start position */
@Override
public int compareTo(SimpleRegion other) {
return Integer.compare(start, other.start);
}
/**
* Returns whether start and end of the region is the same as for this region.
*/
public boolean equalsStartEnd(SimpleRegion other) {
return start == other.start && end == other.end;
}
/** {@inheritDoc} */
@Override
public int hashCode() {
return Objects.hash(start, end);
}
/** {@inheritDoc} */
@Override
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (other == null) {
return false;
}
if (getClass() != other.getClass()) {
return false;
}
SimpleRegion otherRegion = (SimpleRegion) other;
return equalsStartEnd(otherRegion);
}
}