Added function PlaceOpeningHoursAdapter.buildWeekByFirstDay(int firstDayOfWeek) to build array of week days.

Covered it with tests.

Signed-off-by: S. Kozyr <s.trump@gmail.com>
This commit is contained in:
Sergiy Kozyr 2022-02-09 10:38:35 +02:00 committed by Viktor Govako
parent 5ae8f8033f
commit 179906be92
2 changed files with 57 additions and 14 deletions

View file

@ -1,6 +1,5 @@
package com.mapswithme.maps.widget.placepage;
import android.util.ArraySet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -9,21 +8,16 @@ import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.mapswithme.maps.R;
import static com.mapswithme.maps.editor.data.TimeFormatUtils.formatWeekdays;
import static com.mapswithme.maps.editor.data.TimeFormatUtils.formatWeekdaysRange;
import static com.mapswithme.maps.editor.data.TimeFormatUtils.formatNonBusinessTime;
import com.mapswithme.maps.editor.data.Timespan;
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.HashSet;
import java.util.List;
import java.util.Set;
import static com.mapswithme.maps.editor.data.TimeFormatUtils.formatNonBusinessTime;
import static com.mapswithme.maps.editor.data.TimeFormatUtils.formatWeekdaysRange;
public class PlaceOpeningHoursAdapter extends RecyclerView.Adapter<PlaceOpeningHoursAdapter.ViewHolder>
{
@ -38,11 +32,7 @@ public class PlaceOpeningHoursAdapter extends RecyclerView.Adapter<PlaceOpeningH
public void setTimetables(Timetable[] timetables, int firstDayOfWeek)
{
int[] weekDays = null;
if (firstDayOfWeek == Calendar.SUNDAY)
weekDays = new int[]{1, 2, 3, 4, 5, 6, 7};
else
weekDays = new int[]{2, 3, 4, 5, 6, 7, 1};
int[] weekDays = buildWeekByFirstDay(firstDayOfWeek);
final List<WeekScheduleData> scheduleData = new ArrayList<>();
@ -82,6 +72,21 @@ public class PlaceOpeningHoursAdapter extends RecyclerView.Adapter<PlaceOpeningH
notifyDataSetChanged();
}
public static int[] 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;
}
public static Timetable findScheduleForWeekDay(Timetable[] tables, int weekDay)
{
for(Timetable tt : tables)

View file

@ -8,9 +8,11 @@ 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;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.spy;
@ -35,6 +37,42 @@ public class PlaceOpeningHoursAdapterTest {
mWeekScheduleField.setAccessible(true);
}
@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});
}
@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});
}
@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});
}
@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});
}
@Test
public void test_build_week_errors()
{
assertThrows(IllegalArgumentException.class, () -> PlaceOpeningHoursAdapter.buildWeekByFirstDay(-1));
assertThrows(IllegalArgumentException.class, () -> PlaceOpeningHoursAdapter.buildWeekByFirstDay(0));
assertThrows(IllegalArgumentException.class, () -> PlaceOpeningHoursAdapter.buildWeekByFirstDay(8));
}
@Test
public void test_single_closed_day_sunday() throws IllegalAccessException
{