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

com.hfg.math.IntRange Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.math;


import com.hfg.exception.ProgrammingException;
import com.hfg.util.CompareUtil;

//------------------------------------------------------------------------------
/**
 Integer range object.
 

@deprecated Use Range<Integer> @author J. Alex Taylor, hairyfatguy.com */ //------------------------------------------------------------------------------ // com.hfg Library // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com // [email protected] //------------------------------------------------------------------------------ @Deprecated public class IntRange implements Cloneable, Comparable { private Integer mStart; private Integer mEnd; //########################################################################### // CONSTRUCTORS //########################################################################### //-------------------------------------------------------------------------- public IntRange() { } //-------------------------------------------------------------------------- public IntRange(Integer inStart, Integer inEnd) { mStart = inStart; mEnd = inEnd; } //########################################################################### // PUBLIC METHODS //########################################################################### //--------------------------------------------------------------------------- @Override public IntRange clone() { IntRange cloneObj; try { cloneObj = (IntRange) super.clone(); } catch (CloneNotSupportedException e) { throw new ProgrammingException(e); } return cloneObj; } //-------------------------------------------------------------------------- @Override public int hashCode() { int hashCode = (getStart() != null ? getStart().hashCode() : 0); hashCode += 31 * (getEnd() != null ? getEnd().hashCode() : 0); return hashCode; } //-------------------------------------------------------------------------- @Override public boolean equals(Object inObj2) { boolean result = false; if (inObj2 != null && inObj2 instanceof IntRange) { result = (0 == compareTo((IntRange)inObj2)); } return result; } //-------------------------------------------------------------------------- @Override public int compareTo(IntRange inObj2) { int result = 0; if (null == inObj2) { result = 1; } else { result = CompareUtil.compare(getStart(), inObj2.getStart()); if (0 == result) { result = CompareUtil.compare(getEnd(), inObj2.getEnd()); } } return result; } //-------------------------------------------------------------------------- public IntRange setStart(Integer inValue) { mStart = inValue; return this; } //-------------------------------------------------------------------------- public Integer getStart() { return mStart; } //-------------------------------------------------------------------------- public IntRange setEnd(Integer inValue) { mEnd = inValue; return this; } //-------------------------------------------------------------------------- public Integer getEnd() { return mEnd; } //-------------------------------------------------------------------------- @Override public String toString() { return String.format("[%d, %d]", getStart(), getEnd()); } //-------------------------------------------------------------------------- public Integer length() { Integer length = null; if (getStart() != null && getEnd() != null) { length = getEnd() - getStart() + 1; } return length; } //-------------------------------------------------------------------------- public boolean contains(int inValue) { return ((getStart() != null && getEnd() != null && inValue >= getStart() && inValue <= getEnd()) || (null == getStart() && getEnd() != null && inValue <= getEnd()) || (null == getEnd() && getStart() != null && inValue >= getStart()) || (null == getStart() && null == getEnd())); } //-------------------------------------------------------------------------- public boolean contains(IntRange inRange2) { return (contains(inRange2.getStart()) && contains(inRange2.getEnd())); } //-------------------------------------------------------------------------- public IntRange intersection(IntRange inRange2) { IntRange intersection = null; if (inRange2 != null) { if (getStart() != null) { if (inRange2.getStart() != null) { if (getEnd() != null) { if (inRange2.getEnd() != null) { if (getStart() <= inRange2.getEnd() && getEnd() >= inRange2.getStart()) { // ----- // ----- intersection = new IntRange() .setStart(getStart() < inRange2.getStart() ? inRange2.getStart() : getStart()) .setEnd(getEnd() > inRange2.getEnd() ? inRange2.getEnd() : getEnd()); } } else // Range 2 extends infinitely to the right { // ----- // -----> if (inRange2.contains(getStart())) { intersection = new IntRange() .setStart(getStart() < inRange2.getStart() ? inRange2.getStart() : getStart()) .setEnd(getEnd()); } } } else if (inRange2.getEnd() != null) // Range 1 extends infinitely to the right { // -----> // ----- if (getStart() <= inRange2.getEnd()) { intersection = new IntRange() .setStart(getStart() < inRange2.getStart() ? inRange2.getStart() : getStart()) .setEnd(inRange2.getEnd()); } } else { // Both ranges extend infinitely to the right // -----> // -------> intersection = new IntRange() .setStart(getStart() > inRange2.getStart() ? inRange2.getStart() : getStart()) .setEnd(getStart() > inRange2.getStart() ? getStart() : inRange2.getStart()); } } else // Range 2 extends infinitely to the left { if (getEnd() != null) { if (inRange2.getEnd() != null) { // ----- // <------- if (getStart() <= inRange2.getEnd()) { intersection = new IntRange() .setStart(getStart()) .setEnd(getEnd() > inRange2.getEnd() ? inRange2.getEnd() : getEnd()); } } else // Range 2 extends infinitely to the left & right { // ----- // <-------> intersection = clone(); } } else if (inRange2.getEnd() != null) // Range 1 extends infinitely to the right { // -----> // <--- if (inRange2.contains(getStart())) { intersection = new IntRange() .setStart(getStart()) .setEnd(inRange2.getEnd()); } } else { // Both ranges extend infinitely to the right // -----> // <-------> intersection = new IntRange() .setStart(getStart()); } } } else if (inRange2.getStart() != null) { if (getEnd() != null) { if (inRange2.getEnd() != null) { // <----- // ------- if (inRange2.getStart() <= getEnd()) { intersection = new IntRange() .setStart(inRange2.getStart()) .setEnd(getEnd() <= inRange2.getEnd() ? getEnd() : inRange2.getEnd()); } } else { // <----- // -------> if (inRange2.getStart() <= getEnd()) { intersection = new IntRange() .setStart(inRange2.getStart()) .setEnd(getEnd()); } } } else { if (inRange2.getEnd() != null) { // <-----> // --- intersection = inRange2.clone(); } else { // <-----> // ---> intersection = new IntRange() .setStart(inRange2.getStart()); } } } else { if (getEnd() != null) { if (inRange2.getEnd() != null) { // <----- // <--- intersection = new IntRange() .setEnd(getEnd() <= inRange2.getEnd() ? getEnd() : inRange2.getEnd()); } else { // <----- // <--------> intersection = clone(); } } else { if (inRange2.getEnd() != null) { // <-----> // <--- intersection = inRange2.clone(); } else { // <-----> // <-----> intersection = clone(); } } } } return intersection; } //-------------------------------------------------------------------------- public boolean intersects(IntRange inRange2) { boolean result = false; if (getStart() != null) { if (inRange2.getStart() != null) { if (getEnd() != null) { if (inRange2.getEnd() != null) { result = getStart() <= inRange2.getEnd() && getEnd() >= inRange2.getStart(); } else // Range 2 extends infinitely to the right { result = inRange2.getStart() <= getEnd(); } } else if (inRange2.getEnd() != null) { result = inRange2.getEnd() >= getStart(); } else { // Both ranges extend infinitely to the right result = true; } } else // Range 2 extends infinitely to the left { if (inRange2.getEnd() != null) { result = getStart() <= inRange2.getEnd(); } else { // Range2 is infinite result = true; } } } else if (inRange2.getStart() != null) { if (getEnd() != null) { result = inRange2.getStart() <= getEnd(); } else { result = true; } } else { // Both ranges extend infinitely to the left result = true; } return result; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy