forked from organicmaps/organicmaps
Replaced array of ints with list.
Updated unittests. Signed-off-by: S. Kozyr <s.trump@gmail.com>
This commit is contained in:
parent
2ea51603d8
commit
662de4858b
2 changed files with 27 additions and 30 deletions
|
@ -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<PlaceOpeningH
|
|||
|
||||
public void setTimetables(Timetable[] timetables, int firstDayOfWeek)
|
||||
{
|
||||
int[] weekDays = buildWeekByFirstDay(firstDayOfWeek);
|
||||
|
||||
final List<Integer> weekDays = buildWeekByFirstDay(firstDayOfWeek);
|
||||
final List<WeekScheduleData> 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<PlaceOpeningH
|
|||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public static int[] buildWeekByFirstDay(int firstDayOfWeek)
|
||||
public static List<Integer> 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<Integer> 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)
|
||||
|
|
|
@ -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<Integer> 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<Integer> 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<Integer> 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<Integer> weekDays = PlaceOpeningHoursAdapter.buildWeekByFirstDay(Calendar.FRIDAY);
|
||||
assertEquals(weekDays, List.of(6, 7, 1, 2, 3, 4, 5));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
Loading…
Add table
Reference in a new issue