Skip to content

Commit 33f4700

Browse files
hagbardMichael Bolin
authored and
Michael Bolin
committed
Made S1Interval and R1Interval immutable.
------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=25081675
1 parent 89b8770 commit 33f4700

File tree

6 files changed

+18
-49
lines changed

6 files changed

+18
-49
lines changed

src/com/google/common/geometry/R1Interval.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424

2525
public final strictfp class R1Interval {
2626

27-
// TODO(dbeaumont): Make these final and make this class fully immutable.
28-
private double lo;
29-
private double hi;
27+
private final double lo;
28+
private final double hi;
3029

3130
/** Interval constructor. If lo > hi, the interval is empty. */
3231
public R1Interval(double lo, double hi) {
@@ -70,14 +69,6 @@ public double hi() {
7069
return hi;
7170
}
7271

73-
public void setLo(double p) {
74-
this.lo = p;
75-
}
76-
77-
public void setHi(double p) {
78-
this.hi = p;
79-
}
80-
8172
/**
8273
* Return true if the interval is empty, i.e. it contains no points.
8374
*/

src/com/google/common/geometry/S1Interval.java

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@
3838

3939
public final strictfp class S1Interval implements Cloneable {
4040

41-
// TODO(dbeaumont): Make this class immutable and fix callers.
42-
private double lo;
43-
private double hi;
41+
private final double lo;
42+
private final double hi;
4443

4544
/**
4645
* Both endpoints must be in the range -Pi to Pi inclusive. The value -Pi is
@@ -123,16 +122,6 @@ public double hi() {
123122
return hi;
124123
}
125124

126-
public void setLo(double lo) {
127-
this.lo = lo;
128-
// assert (isValid());
129-
}
130-
131-
public void setHi(double hi) {
132-
this.hi = hi;
133-
// assert (isValid());
134-
}
135-
136125
/**
137126
* An interval is valid if neither bound exceeds Pi in absolute value, and the
138127
* value -Pi appears only in the Empty() and Full() intervals.
@@ -350,8 +339,8 @@ public S1Interval addPoint(double p) {
350339
}
351340

352341
/**
353-
* Return an interval that contains all points with a distance "radius" of a
354-
* point in this interval. Note that the expansion of an empty interval is
342+
* Return an interval that contains all points within a distance "radius" of
343+
* a point in this interval. Note that the expansion of an empty interval is
355344
* always empty. The radius must be non-negative.
356345
*/
357346
public S1Interval expanded(double radius) {
@@ -366,13 +355,13 @@ public S1Interval expanded(double radius) {
366355
return full();
367356
}
368357

369-
S1Interval result =
370-
new S1Interval(Math.IEEEremainder(lo() - radius, 2 * S2.M_PI),
371-
Math.IEEEremainder(hi() + radius, 2 * S2.M_PI));
372-
if (result.lo() == -S2.M_PI) {
373-
result.setLo(S2.M_PI);
358+
// NOTE(dbeaumont): Should this remainder be 2 * M_PI or just M_PI ??
359+
double lo = Math.IEEEremainder(lo() - radius, 2 * S2.M_PI);
360+
double hi = Math.IEEEremainder(hi() + radius, 2 * S2.M_PI);
361+
if (lo == -S2.M_PI) {
362+
lo = S2.M_PI;
374363
}
375-
return result;
364+
return new S1Interval(lo, hi);
376365
}
377366

378367
/**
@@ -516,12 +505,4 @@ public static double positiveDistance(double a, double b) {
516505
// the return result is approximately 2*Pi and not zero.
517506
return (b + S2.M_PI) - (a - S2.M_PI);
518507
}
519-
520-
@Override
521-
public Object clone() throws CloneNotSupportedException {
522-
S1Interval clone = (S1Interval) super.clone();
523-
clone.setLo(lo());
524-
clone.setHi(hi());
525-
return clone;
526-
}
527508
}

src/com/google/common/geometry/S2EdgeUtil.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,14 @@ public void addPoint(S2Point b) {
204204
// Minimum/maximum latitude occurs in the edge interior. This affects
205205
// the latitude bounds but not the longitude bounds.
206206
double absLat = Math.acos(Math.abs(aCrossB.get(2) / aCrossB.norm()));
207+
R1Interval lat = bound.lat();
207208
if (da < 0) {
208209
// It's possible that absLat < lat.lo() due to numerical errors.
209-
bound.lat().setHi((Math.max(absLat, bound.lat().hi())));
210+
lat = new R1Interval(lat.lo(), Math.max(absLat, bound.lat().hi()));
210211
} else {
211-
bound.lat().setLo(Math.min(-absLat, bound.lat().lo()));
212+
lat = new R1Interval(Math.min(-absLat, bound.lat().lo()), lat.hi());
212213
}
214+
bound = new S2LatLngRect(lat, bound.lng());
213215
}
214216
}
215217
a = b;

src/com/google/common/geometry/S2Loop.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ private void initBound() {
849849
// north pole in which case b.lng().isFull() due to the test above.
850850

851851
if (b.lng().isFull() && contains(new S2Point(0, 0, -1))) {
852-
b.lat().setLo(-S2.M_PI_2);
852+
b = new S2LatLngRect(new R1Interval(-S2.M_PI_2, b.lat().hi()), b.lng());
853853
}
854854
bound = b;
855855
}

tests/com/google/common/geometry/R1IntervalTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ public void testBasic() {
4242
assertEquals(unit.hi(), 1.0);
4343
assertEquals(negunit.lo(), -1.0);
4444
assertEquals(negunit.hi(), 0.0);
45-
R1Interval ten = new R1Interval(0, 0);
46-
ten.setHi(10);
47-
assertEquals(ten.hi(), 10.0);
4845

4946
// is_empty()
5047
R1Interval half = new R1Interval(0.5, 0.5);

tests/com/google/common/geometry/S1IntervalTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,7 @@ public void testBasic() {
6767
S1Interval quad23 = new S1Interval(S2.M_PI_2, -S2.M_PI_2); // inverted
6868
assertEquals(quad23.lo(), S2.M_PI_2);
6969
assertEquals(quad23.hi(), -S2.M_PI_2);
70-
S1Interval quad1 = new S1Interval(0, 0);
71-
quad1.setHi(S2.M_PI_2);
72-
assertEquals(quad1.hi(), S2.M_PI_2);
70+
S1Interval quad1 = new S1Interval(0, S2.M_PI_2);
7371

7472
// is_valid(), is_empty(), is_inverted()
7573
S1Interval zero = new S1Interval(0, 0);

0 commit comments

Comments
 (0)