# HG changeset patch # User jgindin@chagall # Date 1649384205 14400 # Thu Apr 07 22:16:45 2022 -0400 # Node ID 11c3b51f9ce059cd081c45f2cabfd863077eb929 # Parent 6f4c01d36878d787724cb3e7a6b14e8b085a2caa Move validation of inputs to the builder. The actual ZmanimLocation class is immutable; since the properties can only be set from the builder, it makes more sense to consolidate all the validation in there. diff --git a/ZmanLib/src/main/java/com/gindin/zmanlib/location/ZmanimLocation.java b/ZmanLib/src/main/java/com/gindin/zmanlib/location/ZmanimLocation.java --- a/ZmanLib/src/main/java/com/gindin/zmanlib/location/ZmanimLocation.java +++ b/ZmanLib/src/main/java/com/gindin/zmanlib/location/ZmanimLocation.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021. Jay R. Gindin + * Copyright (c) 2022. Jay R. Gindin */ package com.gindin.zmanlib.location; @@ -103,12 +103,24 @@ } public Builder atElevation( double elevation ) { - this.elevation = elevation; + if ( ( elevation < 0 ) || Double.isInfinite( elevation ) || Double.isNaN( elevation ) ) { + this.elevation = 0.0; + } + else { + this.elevation = elevation; + } + return this; } public Builder withAccuracy( float accuracy ) { - this.accuracy = accuracy; + if ( accuracy >= 0 ) { + this.accuracy = accuracy; + } + else { + this.accuracy = -1; + } + return this; } @@ -178,29 +190,12 @@ this.providerName = builder.providerName; this.locationTime = builder.locationTime; this.originalUserName = builder.userName; + this.accuracy = builder.accuracy; setLocationName( builder.locationName ); setLatitude( builder.latitude ); setLongitude( builder.longitude ); - - if ( builder.accuracy >= 0 ) { - this.accuracy = builder.accuracy; - } - else { - this.accuracy = -1; - } - - if ( builder.elevation < 0 ) { - builder.elevation = 0.0; - } setElevation( builder.elevation ); - - // If we don't set a time zone, then it defaults to GMT... - if ( null == builder.timeZone ) { - setTimeZone( TimeZone.getDefault() ); - } - else { - setTimeZone( builder.timeZone ); - } + setTimeZone( builder.timeZone ); } @@ -210,10 +205,11 @@ private ZmanimLocation() { this.isInvalid = true; - this.accuracy = -1; + this.providerName = UNKNOWN_LOCATION; this.locationTime = 0; this.originalUserName = ""; + this.accuracy = -1; }