Source:
<code>protected Collection<Event> analyzePosition(Position position) { Device device = Context.getIdentityManager().getDeviceById(position.getDeviceId()); if (device == null) { return null; } if (!Context.getIdentityManager().isLatestPosition(position) || !position.getValid()) { return null; } List<Long> currentGeofences = geofenceManager.getCurrentDeviceGeofences(position); List<Long> oldGeofences = new ArrayList<>(); if (device.getGeofenceIds() != null) { oldGeofences.addAll(device.getGeofenceIds()); } List<Long> newGeofences = new ArrayList<>(currentGeofences); newGeofences.removeAll(oldGeofences); oldGeofences.removeAll(currentGeofences); device.setGeofenceIds(currentGeofences); Collection<Event> events = new ArrayList<>(); for (long geofenceId : newGeofences) { long calendarId = ((Geofence) geofenceManager.getById(geofenceId)).getCalendarId(); <START> if (calendarId == 0 || Context.getCalendarManager().getById(calendarId) == null <END> || ((Calendar) Context.getCalendarManager().getById(calendarId)) .checkMoment(position.getFixTime())) { Event event = new Event(Event.TYPE_GEOFENCE_ENTER, position.getDeviceId(), position.getId()); event.setGeofenceId(geofenceId); events.add(event); } } for (long geofenceId : oldGeofences) { long calendarId = ((Geofence) geofenceManager.getById(geofenceId)).getCalendarId(); if (calendarId == 0 || Context.getCalendarManager().getById(calendarId) == null || ((Calendar) Context.getCalendarManager().getById(calendarId)) .checkMoment(position.getFixTime())) { Event event = new Event(Event.TYPE_GEOFENCE_EXIT, position.getDeviceId(), position.getId()); event.setGeofenceId(geofenceId); events.add(event); } } return events; }</code><technical_language>I avoid calling getById multiple times</technical_language>

Target:
protected Collection<Event> analyzePosition(Position position) { Device device = Context.getIdentityManager().getDeviceById(position.getDeviceId()); if (device == null) { return null; } if (!Context.getIdentityManager().isLatestPosition(position) || !position.getValid()) { return null; } List<Long> currentGeofences = geofenceManager.getCurrentDeviceGeofences(position); List<Long> oldGeofences = new ArrayList<>(); if (device.getGeofenceIds() != null) { oldGeofences.addAll(device.getGeofenceIds()); } List<Long> newGeofences = new ArrayList<>(currentGeofences); newGeofences.removeAll(oldGeofences); oldGeofences.removeAll(currentGeofences); device.setGeofenceIds(currentGeofences); Collection<Event> events = new ArrayList<>(); for (long geofenceId : newGeofences) { long calendarId = ((Geofence) geofenceManager.getById(geofenceId)).getCalendarId(); Calendar calendar = calendarId != 0 ? (Calendar) Context.getCalendarManager().getById(calendarId) : null; if (calendar == null || calendar.checkMoment(position.getFixTime())) { Event event = new Event(Event.TYPE_GEOFENCE_ENTER, position.getDeviceId(), position.getId()); event.setGeofenceId(geofenceId); events.add(event); } } for (long geofenceId : oldGeofences) { long calendarId = ((Geofence) geofenceManager.getById(geofenceId)).getCalendarId(); Calendar calendar = calendarId != 0 ? (Calendar) Context.getCalendarManager().getById(calendarId) : null; if (calendar == null || calendar.checkMoment(position.getFixTime())) { Event event = new Event(Event.TYPE_GEOFENCE_EXIT, position.getDeviceId(), position.getId()); event.setGeofenceId(geofenceId); events.add(event); } } return events; }