@VisibleForTesting Optional<GeoLocationInformation> extractGeoLocationInformation(Object fieldValue) { final InetAddress ipAddress; if (fieldValue instanceof InetAddress) { ipAddress = (InetAddress) fieldValue; } else if (fieldValue instanceof String) { ipAddress = getIpFromFieldValue((String) fieldValue); } else { ipAddress = null; } GeoLocationInformation geoLocationInformation = null; if (ipAddress != null) { try (Timer.Context ignored = resolveTime.time()) { final CityResponse response = databaseReader.city(ipAddress); final Location location = response.getLocation(); final Country country = response.getCountry(); final City city = response.getCity(); geoLocationInformation = GeoLocationInformation.create( location.getLatitude(), location.getLongitude(), country.getGeoNameId() != null ? country.getIsoCode() : "N/A", city.getGeoNameId() != null ? city.getName() : "N/A" // calling to .getName() may throw a NPE ); } catch (Exception e) { LOG.debug("Could not get location from IP {}", ipAddress.getHostAddress(), e); } } return Optional.ofNullable(geoLocationInformation); }
attrs.put(new StringBuilder(ipAttributeName).append(".geo.city").toString(), response.getCity().getName());
private Record createRecord(final CityResponse city) { if (city == null) { return null; } final Map<String, Object> values = new HashMap<>(); values.put(CitySchema.CITY.getFieldName(), city.getCity().getName()); final Location location = city.getLocation(); values.put(CitySchema.ACCURACY.getFieldName(), location.getAccuracyRadius()); values.put(CitySchema.METRO_CODE.getFieldName(), location.getMetroCode()); values.put(CitySchema.TIMEZONE.getFieldName(), location.getTimeZone()); values.put(CitySchema.LATITUDE.getFieldName(), location.getLatitude()); values.put(CitySchema.LONGITUDE.getFieldName(), location.getLongitude()); values.put(CitySchema.CONTINENT.getFieldName(), city.getContinent().getName()); values.put(CitySchema.POSTALCODE.getFieldName(), city.getPostal().getCode()); values.put(CitySchema.COUNTRY.getFieldName(), createRecord(city.getCountry())); final Object[] subdivisions = new Object[city.getSubdivisions().size()]; int i = 0; for (final Subdivision subdivision : city.getSubdivisions()) { subdivisions[i++] = createRecord(subdivision); } values.put(CitySchema.SUBDIVISIONS.getFieldName(), subdivisions); return new MapRecord(CitySchema.GEO_SCHEMA, values); }
address.setPostalCode(response.getPostal().getCode()); address.setZone(response.getMostSpecificSubdivision().getIsoCode()); address.setCity(response.getCity().getName());
GeoProps.CITY.set(geoInfo, MaxMindDbUtilities.convertNullToEmptyString(city.getName())); GeoProps.POSTAL_CODE.set(geoInfo, MaxMindDbUtilities.convertNullToEmptyString(postal.getCode())); GeoProps.DMA_CODE.set(geoInfo, MaxMindDbUtilities.convertNullToEmptyString(location.getMetroCode()));
public void displayAdjacentCities(City aCity) { for(int x = 0; x < size; x++) { if (aCity.getName().equalsIgnoreCase(allCities[x].getName())) { if (adjacents[x].size() == 0) continue; System.out.print(aCity.getName()); for (City other : adjacents[x]) { System.out.print(" >>> " + other.getName()); } System.out.println(); } } }
public boolean equals(Object object) { boolean areEquals = false; if (object instanceof City) { City otherCity = (City) object; areEquals = (this.getName() == otherCity.getName() && this.getDepartment() == otherCity.getDepartment()); } return areEquals; }
public class SomeDto { private String foo; private String bar; private City city; public String getFoo() {...}; public String getBar() {...}; public String getCity() {return city.getName();}; public String getCountry() {return city.getCountry();}; }
@Override public String getValue(CityResponse response) { return response.getCity().getName(); } },
Vertex nl = countries.iterator().next().asVertex(); GremlinPipeline<Vertex, Vertex> pipe = new GremlinPipeline<>(); pipe.start(nl).inE("LOCATED_IN").outV(); Iterable<City> cities = framedGraph.frameVertices(pipe, City.class); for (City city : cities) { System.out.println(city.getName()); }
private GeoInfo _getGeoInfo(String ipAddress) { GeoInfo geoInfo = new GeoInfo(); if (ipAddress == null) { return geoInfo; } try{ InetAddress inetAddress = InetAddress.getByName(ipAddress); CityResponse response = reader.city(inetAddress); Country country = response.getCountry(); Continent continent = response.getContinent(); City city = response.getCity(); Postal postal = response.getPostal(); Location location = response.getLocation(); Subdivision subdivision = response.getMostSpecificSubdivision(); geoInfo.setRegion(subdivision.getName()); geoInfo.setCountryIsoCode(country.getIsoCode()); geoInfo.setCountry(country.getName()); geoInfo.setContinent(continent.getCode()); geoInfo.setCity(city.getName()); geoInfo.setPostalCode(postal.getCode()); geoInfo.setLatitude(location.getLatitude()); geoInfo.setLongitude(location.getLongitude()); return geoInfo; } catch(Exception ex){ return null; } }
@Override public View getView ( int position, View convertView, ViewGroup parent ) { /* create a new view of my layout and inflate it in the row */ convertView = ( RelativeLayout ) inflater.inflate( resource, null ); /* Extract the city's object to show */ City city = getItem( position ); /* Take the TextView from layout and set the city's name */ TextView txtName = (TextView) convertView.findViewById(R.id.cityName); txtName.setText(city.getName()); /* Take the TextView from layout and set the city's wiki link */ TextView txtWiki = (TextView) convertView.findViewById(R.id.cityLinkWiki); txtWiki.setText(city.getUrlWiki()); Typeface face=Typeface.createFromAsset(getAssets(),"fonts/roboto.ttf"); txtName.setTypeface(face); txtWiki.setTypeface(face); return convertView; }
break; case "city": put(properties,"_city", city.getCity().getName()); break; case "latitude":
@Override public View getView ( int position, View convertView, ViewGroup parent ) { ViewHolder holder; if(convertView == null) { convertView = ( RelativeLayout ) inflater.inflate( resource, parent, false ); holder = new ViewHolder(); holder.txtName = (TextView) convertView.findViewById(R.id.cityName); holder.txtWiki = (TextView) convertView.findViewById(R.id.cityLinkWiki); holder.imageCity = (ImageView) convertView.findViewById(R.id.ImageCity); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } City city = (City)getItem( position ); holder.txtName.setText(city.getName()); holder.txtWiki.setText(city.getUrlWiki()); return convertView; }
@Override public View getView ( int position, View convertView, ViewGroup parent ) { /* create a new view of my layout and inflate it in the row */ convertView = ( RelativeLayout ) inflater.inflate( resource, null ); /* Extract the city's object to show */ City city = getItem( position ); /* Take the TextView from layout and set the city's name */ TextView txtName = (TextView) convertView.findViewById(R.id.cityName); txtName.setText(city.getName()); /* Take the TextView from layout and set the city's wiki link */ TextView txtWiki = (TextView) convertView.findViewById(R.id.cityLinkWiki); txtWiki.setText(city.getUrlWiki()); /* Take the ImageView from layout and set the city's image */ ImageView imageCity = (ImageView) convertView.findViewById(R.id.ImageCity); String uri = "drawable/" + city.getImage(); int imageResource = context.getResources().getIdentifier(uri, null, context.getPackageName()); Drawable image = context.getResources().getDrawable(imageResource); imageCity.setImageDrawable(image); return convertView; }
public class Test { public static void main(String[] args) { String json = ... ObjectMapper mapper = new ObjectMapper(); Cities cities = mapper.readValue(json, Cities.class); for(City city : cities) { System.out.println(city.getName()); for(Office office : city.getOffices()) { System.out.println("\t" + office.getName()); for(Room room : office.getRooms()) { System.out.println("\t\t" + room.getName()); System.out.println("\t\t\t" + room.getPropertyTwo()); } } } } }
mPagerAdapter.removeAllFragments(); for(City city: cityList){ PageFragment fragment = PageFragment.newFragment(city.getName()); mPagerAdapter.addFragment(fragment);
City city = ... // create a city instance city.getName(); city.getPopulation(); // possibly city specific city.getCoordinates(); city.getState(); // most likely city specific!
@Override public Location resolve(String ip) { try { CityResponse response = reader.city(InetAddress.getByName(ip)); Location location = new Location(); location.setCity(response.getCity().getName()); location.setCountryName(response.getCountry().getName()); location.setCountryCode(response.getCountry().getIsoCode()); location.setIp(ip); return location; } catch (IOException | GeoIp2Exception ex) { LOG.debug("Unable to resolve ip location", ex); } return null; }