Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
postcodes-io-java
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
James Coyle
postcodes-io-java
Commits
a5902013
Commit
a5902013
authored
Aug 14, 2016
by
Deepak Prabhakar
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored PostCode to avoid cyclic complexity
parent
491db88f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
83 additions
and
75 deletions
+83
-75
src/main/java/com/postcode/io/PostCode.java
src/main/java/com/postcode/io/PostCode.java
+83
-75
No files found.
src/main/java/com/postcode/io/PostCode.java
View file @
a5902013
...
...
@@ -418,6 +418,8 @@ public class PostCode {
}
/**
* Generate {@link PostCode} from the given {@link PostcodeLookup}
*
* @param postcodeLookup
* @return
*/
...
...
@@ -425,87 +427,93 @@ public class PostCode {
JSONObject
json
;
try
{
json
=
JsonFetcher
.
urlToJson
(
new
URL
(
postcodeLookup
.
getUrl
()));
if
(
json
.
has
(
"result"
))
{
json
=
json
.
getJSONObject
(
"result"
);
pc
=
new
PostCode
();
if
(
json
.
has
(
"postcode"
)
&&
!
json
.
get
(
"postcode"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
postcode
=
json
.
getString
(
"postcode"
);
}
if
(
json
.
has
(
"quality"
)
&&
!
json
.
get
(
"quality"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
quality
=
json
.
getInt
(
"quality"
);
}
if
(
json
.
has
(
"eastings"
)
&&
!
json
.
get
(
"eastings"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
eastings
=
json
.
getInt
(
"eastings"
);
}
if
(
json
.
has
(
"northings"
)
&&
!
json
.
get
(
"northings"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
northings
=
json
.
getInt
(
"northings"
);
}
if
(
json
.
has
(
"country"
)
&&
!
json
.
get
(
"country"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
country
=
json
.
getString
(
"country"
);
}
if
(
json
.
has
(
"nhs_ha"
)
&&
!
json
.
get
(
"nhs_ha"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
nhs_ha
=
json
.
getString
(
"nhs_ha"
);
}
if
(
json
.
has
(
"longitude"
)
&&
!
json
.
get
(
"longitude"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
longitude
=
json
.
getDouble
(
"longitude"
);
}
if
(
json
.
has
(
"latitude"
)
&&
!
json
.
get
(
"latitude"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
latitude
=
json
.
getDouble
(
"latitude"
);
}
if
(
json
.
has
(
"parliamentary_constituency"
)
&&
!
json
.
get
(
"parliamentary_constituency"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
parliamentary_constituency
=
json
.
getString
(
"parliamentary_constituency"
);
}
if
(
json
.
has
(
"european_electoral_region"
)
&&
!
json
.
get
(
"european_electoral_region"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
european_electoral_region
=
json
.
getString
(
"european_electoral_region"
);
}
if
(
json
.
has
(
"primary_care_trust"
)
&&
!
json
.
get
(
"primary_care_trust"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
primary_care_trust
=
json
.
getString
(
"primary_care_trust"
);
}
if
(
json
.
has
(
"region"
)
&&
!
json
.
get
(
"region"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
region
=
json
.
getString
(
"region"
);
}
if
(
json
.
has
(
"lsoa"
)
&&
!
json
.
get
(
"lsoa"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
lsoa
=
json
.
getString
(
"lsoa"
);
}
if
(
json
.
has
(
"msoa"
)
&&
!
json
.
get
(
"msoa"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
msoa
=
json
.
getString
(
"msoa"
);
}
if
(
json
.
has
(
"incode"
)
&&
!
json
.
get
(
"incode"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
incode
=
json
.
getString
(
"incode"
);
}
if
(
json
.
has
(
"outcode"
)
&&
!
json
.
get
(
"outcode"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
outcode
=
json
.
getString
(
"outcode"
);
}
if
(
json
.
has
(
"admin_district"
)
&&
!
json
.
get
(
"admin_district"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
admin_district
=
json
.
getString
(
"admin_district"
);
}
if
(
json
.
has
(
"parish"
)
&&
!
json
.
get
(
"parish"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
parish
=
json
.
getString
(
"parish"
);
}
if
(
json
.
has
(
"admin_county"
)
&&
!
json
.
get
(
"admin_county"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
admin_county
=
json
.
getBoolean
(
"admin_county"
);
}
if
(
json
.
has
(
"admin_ward"
)
&&
!
json
.
get
(
"admin_ward"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
admin_ward
=
json
.
getString
(
"admin_ward"
);
}
if
(
json
.
has
(
"ccg"
)
&&
!
json
.
get
(
"ccg"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
ccg
=
json
.
getString
(
"ccg"
);
}
if
(
json
.
has
(
"nuts"
)
&&
!
json
.
get
(
"nuts"
).
equals
(
JSONObject
.
NULL
))
{
pc
.
nuts
=
json
.
getString
(
"nuts"
);
}
generateCodes
(
json
);
}
else
{
throw
new
IllegalArgumentException
(
"Postcode not found"
);
}
generateResult
(
json
);
generateCodes
(
json
.
getJSONObject
(
"result"
));
}
catch
(
MalformedURLException
e
)
{
e
.
printStackTrace
();
}
return
pc
;
}
private
static
void
generateResult
(
JSONObject
json
)
{
if
(
json
.
has
(
"result"
))
{
json
=
json
.
getJSONObject
(
"result"
);
pc
=
new
PostCode
();
if
(
isJSONPresentAndNotNull
(
json
,
"postcode"
))
{
pc
.
postcode
=
json
.
getString
(
"postcode"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"quality"
))
{
pc
.
quality
=
json
.
getInt
(
"quality"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"eastings"
))
{
pc
.
eastings
=
json
.
getInt
(
"eastings"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"northings"
))
{
pc
.
northings
=
json
.
getInt
(
"northings"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"country"
))
{
pc
.
country
=
json
.
getString
(
"country"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"nhs_ha"
))
{
pc
.
nhs_ha
=
json
.
getString
(
"nhs_ha"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"longitude"
))
{
pc
.
longitude
=
json
.
getDouble
(
"longitude"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"latitude"
))
{
pc
.
latitude
=
json
.
getDouble
(
"latitude"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"parliamentary_constituency"
))
{
pc
.
parliamentary_constituency
=
json
.
getString
(
"parliamentary_constituency"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"european_electoral_region"
))
{
pc
.
european_electoral_region
=
json
.
getString
(
"european_electoral_region"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"primary_care_trust"
))
{
pc
.
primary_care_trust
=
json
.
getString
(
"primary_care_trust"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"region"
))
{
pc
.
region
=
json
.
getString
(
"region"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"lsoa"
))
{
pc
.
lsoa
=
json
.
getString
(
"lsoa"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"msoa"
))
{
pc
.
msoa
=
json
.
getString
(
"msoa"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"incode"
))
{
pc
.
incode
=
json
.
getString
(
"incode"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"outcode"
))
{
pc
.
outcode
=
json
.
getString
(
"outcode"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"admin_district"
))
{
pc
.
admin_district
=
json
.
getString
(
"admin_district"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"parish"
))
{
pc
.
parish
=
json
.
getString
(
"parish"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"admin_county"
))
{
pc
.
admin_county
=
json
.
getBoolean
(
"admin_county"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"admin_ward"
))
{
pc
.
admin_ward
=
json
.
getString
(
"admin_ward"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"ccg"
))
{
pc
.
ccg
=
json
.
getString
(
"ccg"
);
}
if
(
isJSONPresentAndNotNull
(
json
,
"nuts"
))
{
pc
.
nuts
=
json
.
getString
(
"nuts"
);
}
}
else
{
throw
new
IllegalArgumentException
(
"Postcode not found"
);
}
}
private
static
boolean
isJSONPresentAndNotNull
(
JSONObject
json
,
String
jsonString
)
{
return
json
.
has
(
jsonString
)
&&
!
json
.
get
(
jsonString
).
equals
(
JSONObject
.
NULL
);
}
private
static
void
generateCodes
(
JSONObject
json
)
{
if
(
json
.
has
(
"codes"
)
&&
!
json
.
get
(
"codes"
).
equals
(
JSONObject
.
NULL
))
{
JSONObject
jcodes
=
json
.
getJSONObject
(
"codes"
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment