diff --git a/android/src/com/mapswithme/maps/widget/placepage/PlaceOpeningHoursAdapter.java b/android/src/com/mapswithme/maps/widget/placepage/PlaceOpeningHoursAdapter.java index 4f9e24e206..779eb8e5f8 100644 --- a/android/src/com/mapswithme/maps/widget/placepage/PlaceOpeningHoursAdapter.java +++ b/android/src/com/mapswithme/maps/widget/placepage/PlaceOpeningHoursAdapter.java @@ -13,6 +13,8 @@ import com.mapswithme.maps.editor.data.Timetable; import com.mapswithme.util.UiUtils; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Calendar; import java.util.Collections; import java.util.List; @@ -32,38 +34,37 @@ public class PlaceOpeningHoursAdapter extends RecyclerView.Adapter weekDays = buildWeekByFirstDay(firstDayOfWeek); final List scheduleData = new ArrayList<>(); // Timetables array contains only working days. We need to fill non-working gaps. - for (int i = 0; i < weekDays.length; i++) + for (int i = 0; i < weekDays.size(); i++) { - int weekDay = weekDays[i]; + int weekDay = weekDays.get(i); Timetable tt = findScheduleForWeekDay(timetables, weekDay); if (tt != null) { - int startWeekDay = weekDays[i]; - while (i < weekDays.length && tt.containsWeekday(weekDays[i])) + int startWeekDay = weekDays.get(i); + while (i < weekDays.size() && tt.containsWeekday(weekDays.get(i))) i++; i--; - int endWeekDay = weekDays[i]; + int endWeekDay = weekDays.get(i); scheduleData.add(new WeekScheduleData(startWeekDay, endWeekDay, tt)); } else { - int startWeekDay = weekDays[i]; - // Search next working day in timetables - while (i+1 < weekDays.length) + int startWeekDay = weekDays.get(i); + // Search next working day in timetables. + while (i + 1 < weekDays.size()) { - if (findScheduleForWeekDay(timetables, weekDays[i + 1]) != null) + if (findScheduleForWeekDay(timetables, weekDays.get(i + 1)) != null) break; i++; } - scheduleData.add(new WeekScheduleData(startWeekDay, weekDays[i], null)); + scheduleData.add(new WeekScheduleData(startWeekDay, weekDays.get(i), null)); } } @@ -72,19 +73,16 @@ public class PlaceOpeningHoursAdapter extends RecyclerView.Adapter buildWeekByFirstDay(int firstDayOfWeek) { if (firstDayOfWeek < 1 || firstDayOfWeek > 7) throw new IllegalArgumentException("First day of week "+firstDayOfWeek+" is out of range [1..7]"); - int[] weekDays = new int[7]; - for (int i=0; i<7; i++) - { - weekDays[i] = (i + firstDayOfWeek); - if (weekDays[i] > 7) - weekDays[i] -= 7; - } - return weekDays; + final List list = Arrays.asList(Calendar.SUNDAY, Calendar.MONDAY, Calendar.TUESDAY, + Calendar.WEDNESDAY, Calendar.THURSDAY, Calendar.FRIDAY, + Calendar.SATURDAY); + Collections.rotate(list, 1 - firstDayOfWeek); + return list; } public static Timetable findScheduleForWeekDay(Timetable[] tables, int weekDay) diff --git a/android/tests/java/com/mapswithme/maps/widget/placepage/PlaceOpeningHoursAdapterTest.java b/android/tests/java/com/mapswithme/maps/widget/placepage/PlaceOpeningHoursAdapterTest.java index 36ce763f54..17bd0ef625 100644 --- a/android/tests/java/com/mapswithme/maps/widget/placepage/PlaceOpeningHoursAdapterTest.java +++ b/android/tests/java/com/mapswithme/maps/widget/placepage/PlaceOpeningHoursAdapterTest.java @@ -8,7 +8,6 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -40,29 +39,29 @@ public class PlaceOpeningHoursAdapterTest { @Test public void test_build_week_from_sunday() { - int[] weekDays = PlaceOpeningHoursAdapter.buildWeekByFirstDay(Calendar.SUNDAY); - assertArrayEquals(weekDays, new int[]{1, 2, 3, 4, 5, 6, 7}); + List weekDays = PlaceOpeningHoursAdapter.buildWeekByFirstDay(Calendar.SUNDAY); + assertEquals(weekDays, List.of(1, 2, 3, 4, 5, 6, 7)); } @Test public void test_build_week_from_monday() { - int[] weekDays = PlaceOpeningHoursAdapter.buildWeekByFirstDay(Calendar.MONDAY); - assertArrayEquals(weekDays, new int[]{2, 3, 4, 5, 6, 7, 1}); + List weekDays = PlaceOpeningHoursAdapter.buildWeekByFirstDay(Calendar.MONDAY); + assertEquals(weekDays, List.of(2, 3, 4, 5, 6, 7, 1)); } @Test public void test_build_week_from_saturday() { - int[] weekDays = PlaceOpeningHoursAdapter.buildWeekByFirstDay(Calendar.SATURDAY); - assertArrayEquals(weekDays, new int[]{7, 1, 2, 3, 4, 5, 6}); + List weekDays = PlaceOpeningHoursAdapter.buildWeekByFirstDay(Calendar.SATURDAY); + assertEquals(weekDays, List.of(7, 1, 2, 3, 4, 5, 6)); } @Test public void test_build_week_from_friday() { - int[] weekDays = PlaceOpeningHoursAdapter.buildWeekByFirstDay(Calendar.FRIDAY); - assertArrayEquals(weekDays, new int[]{6, 7, 1, 2, 3, 4, 5}); + List weekDays = PlaceOpeningHoursAdapter.buildWeekByFirstDay(Calendar.FRIDAY); + assertEquals(weekDays, List.of(6, 7, 1, 2, 3, 4, 5)); } @Test