litesoft
@ 821
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.core.simpletypes; import org.litesoft.core.simpletypes.nonpublic.*; import org.litesoft.core.typeutils.*; public final class Line extends CompareSupport<Line> implements SimpleType { private int mOffset; private int mLength; /** * @deprecated - for Serialization */ protected Line() { } public Line( int pOffset, int pLength ) { mOffset = pOffset; if ( (mLength = pLength) < 0 ) { throw new IllegalArgumentException( "A Line 'length'" + Numerics.CANNOT_BE_NEGATIVE ); } } public int getOffset() { return mOffset; } public int getLength() { return mLength; } public int getEndOffset() { return mOffset + (mLength - 1); } private boolean doWeObscure( int pOffset ) { return (getOffset() <= pOffset) && (pOffset <= getEndOffset()); } /** * Determine if a Line would be obscured by (covered by / inclusively contained by) 'this'. * * @param pThem !null */ public boolean obscures( Line pThem ) { return doWeObscure( pThem.getOffset() ) && doWeObscure( pThem.getEndOffset() ); } public boolean equals( Object o ) { return (this == o) || // ((o instanceof Line) && equals( (Line) o )); } public boolean equals( Line them ) { return (this == them) || // ((them != null) // && equal( this.mOffset, them.mOffset ) // && equal( this.mLength, them.mLength ) // ); } public int hashCode() { return hashCodeEm( calcHashCode( mOffset ), // calcHashCode( mLength ) ); } /** * Order by Offsets and then Lengths */ public int compareTo( Line them ) { return compareEm( compare( this.mOffset, them.mOffset ), // compare( this.mLength, them.mLength ) ); } public String toString() { return toStringBuilder().toString(); } public StringBuilder toStringBuilder() { return appendTo( new StringBuilder() ); } public StringBuilder appendTo( StringBuilder pSB ) { return pSB.append( "Line @ " ).append( mOffset ).append( " len: " ).append( mLength ); } /** * @return this or a new Line if pNewOffset is !null and is a change */ public Line moveTo( Integer pNewOffset ) { return (pNewOffset == null) ? this : moveTo( pNewOffset.intValue() ); } /** * @return this or a new Line if pNewOffset is a change */ public Line moveTo( int pNewOffset ) { return (getOffset() == pNewOffset) ? this : new Line( pNewOffset, getLength() ); } /** * @return this or a new Line if pNewLength is !null and is a change */ public Line resizeTo( Integer pNewLength ) { return (pNewLength == null) ? this : resizeTo( pNewLength.intValue() ); } /** * @return this or a new Line if pNewLength is a change */ public Line resizeTo( int pNewLength ) { return (getLength() == pNewLength) ? this : new Line( getOffset(), pNewLength ); } /** * @return this or a new Line if offset &/ length was changed, prefer adjusting offset over length */ public Line adjustToBeObscuredBy( Line pThem ) { if ( pThem == null ) { throw new IllegalArgumentException( "Can NOT adjust to be obscured by 'null' Line" ); } return resizeTo( Math.min( this.getLength(), pThem.getLength() ) ).adjustOffsetToBeObscuredBy( pThem ); } private Line adjustOffsetToBeObscuredBy( Line pThem ) // Note the Lengths are compatible { if ( this.getOffset() < pThem.getOffset() ) { return moveTo( pThem.getOffset() ); } int zThisEndOffset = this.getEndOffset(); int zThemEndOffset = pThem.getEndOffset(); if ( zThisEndOffset > zThemEndOffset ) { return moveTo( this.getOffset() - (zThisEndOffset - zThemEndOffset) ); } return this; } } |