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
|
package org.litesoft.core.simpletypes.nonpublic;
import org.litesoft.core.util.*;
/**
* @noinspection EqualsAndHashcode
*/
public abstract class AbstractXY<T extends AbstractXY> extends CompareSupport<T> implements SimpleType
{
protected int mX, mY;
/**
* @deprecated - for Serialization
*/
protected AbstractXY()
{
}
protected AbstractXY( int pX, int pY )
{
setX( pX );
setY( pY );
}
private void setX( int pX )
{
mX = validate( "X", pX );
}
public int getX()
{
return mX;
}
private void setY( int pY )
{
mY = validate( "Y", pY );
}
public int getY()
{
return mY;
}
public int hashCode()
{
return hashCodeEm( calcHashCode( mX ), //
calcHashCode( mY ) );
}
protected boolean LLequals( AbstractXY them )
{
return (this == them) || //
((them != null) //
&& equal( this.mX, them.mX ) //
&& equal( this.mY, them.mY ) //
);
}
/**
* Row (Y) then Column (X)
*/
public int compareTo( T them )
{
return compareEm( compare( this.mY, them.mY ), //
compare( this.mX, them.mX ) );
}
abstract protected int validate( String pWhat, int pValue );
private static IllegalArgumentException populateProblem( String pWhat, String pWhy )
{
return new IllegalArgumentException( pWhat + "XY from string value" + pWhy );
}
private static int parseValue( String pWhat, String pXorY, String pValue )
{
if ( (pValue = pValue.trim()).length() == 0 )
{
throw populateProblem( pWhat, ": did not have a '" + pXorY + "' value" );
}
try
{
return Integer.parseInt( pValue );
}
catch ( NumberFormatException e )
{
throw populateProblem( pWhat, ", '" + pXorY + "' value: " + e.getMessage() );
}
}
protected static void populate( String pWhat, AbstractXY pXY, String pToString )
{
if ( pToString == null )
{
throw populateProblem( pWhat, UtilsCommon.NOT_ALLOWED_TO_BE_NULL );
}
pToString = pToString.trim();
if ( !pToString.startsWith( "(" ) )
{
throw populateProblem( pWhat, ": did not start with '('" );
}
if ( !pToString.startsWith( "(" ) )
{
throw populateProblem( pWhat, ": did not end with ')'" );
}
int zCommaAt = pToString.indexOf( ',' );
if ( zCommaAt == -1 )
{
throw populateProblem( pWhat, ": No X-Y sep ','" );
}
pXY.setX( parseValue( pWhat, "X", pToString.substring( 1, zCommaAt ) ) );
pXY.setY( parseValue( pWhat, "Y", pToString.substring( zCommaAt + 1, pToString.length() - 1 ) ) );
}
public String toString()
{
return "(" + mX + "," + mY + ")";
}
}
|