mirror of https://github.com/iSoron/uhabits.git
parent
37a9e793e7
commit
15a4a2c002
@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.models;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class WeekdayList
|
||||||
|
{
|
||||||
|
public static WeekdayList EVERY_DAY = new WeekdayList(127);
|
||||||
|
|
||||||
|
private final boolean[] weekdays;
|
||||||
|
|
||||||
|
public WeekdayList(int packedList)
|
||||||
|
{
|
||||||
|
weekdays = new boolean[7];
|
||||||
|
int current = 1;
|
||||||
|
|
||||||
|
for (int i = 0; i < 7; i++)
|
||||||
|
{
|
||||||
|
if ((packedList & current) != 0) weekdays[i] = true;
|
||||||
|
current = current << 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public WeekdayList(boolean weekdays[])
|
||||||
|
{
|
||||||
|
this.weekdays = Arrays.copyOf(weekdays, 7);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean[] toArray()
|
||||||
|
{
|
||||||
|
return weekdays;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int toInteger()
|
||||||
|
{
|
||||||
|
int packedList = 0;
|
||||||
|
int current = 1;
|
||||||
|
|
||||||
|
for (int i = 0; i < 7; i++)
|
||||||
|
{
|
||||||
|
if (weekdays[i]) packedList |= current;
|
||||||
|
current = current << 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return packedList;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.utils;
|
||||||
|
|
||||||
|
import android.support.annotation.*;
|
||||||
|
|
||||||
|
import java.text.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import static android.os.Build.VERSION.*;
|
||||||
|
import static android.os.Build.VERSION_CODES.*;
|
||||||
|
import static android.text.format.DateFormat.*;
|
||||||
|
|
||||||
|
public class DateFormats
|
||||||
|
{
|
||||||
|
@NonNull
|
||||||
|
private static SimpleDateFormat fromSkeleton(@NonNull String skeleton,
|
||||||
|
@NonNull Locale locale)
|
||||||
|
{
|
||||||
|
SimpleDateFormat df = new SimpleDateFormat(skeleton, locale);
|
||||||
|
df.setTimeZone(TimeZone.getTimeZone("UTC"));
|
||||||
|
return df;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public static SimpleDateFormat fromSkeleton(@NonNull String skeleton)
|
||||||
|
{
|
||||||
|
Locale locale = Locale.getDefault();
|
||||||
|
|
||||||
|
if (SDK_INT >= JELLY_BEAN)
|
||||||
|
skeleton = getBestDateTimePattern(locale, skeleton);
|
||||||
|
|
||||||
|
return fromSkeleton(skeleton, locale);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SimpleDateFormat getBackupDateFormat()
|
||||||
|
{
|
||||||
|
return fromSkeleton("yyyy-MM-dd HHmmss", Locale.US);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static SimpleDateFormat getCSVDateFormat()
|
||||||
|
{
|
||||||
|
return fromSkeleton("yyyy-MM-dd", Locale.US);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.models;
|
||||||
|
|
||||||
|
import org.isoron.uhabits.*;
|
||||||
|
import org.junit.*;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.*;
|
||||||
|
import static org.hamcrest.core.IsEqual.*;
|
||||||
|
|
||||||
|
public class WeekdayListTest extends BaseUnitTest
|
||||||
|
{
|
||||||
|
@Test
|
||||||
|
public void test()
|
||||||
|
{
|
||||||
|
int daysInt = 124;
|
||||||
|
boolean[] daysArray = new boolean[]{
|
||||||
|
false, false, true, true, true, true, true
|
||||||
|
};
|
||||||
|
|
||||||
|
WeekdayList list = new WeekdayList(daysArray);
|
||||||
|
assertThat(list.toArray(), equalTo(daysArray));
|
||||||
|
assertThat(list.toInteger(), equalTo(daysInt));
|
||||||
|
|
||||||
|
list = new WeekdayList(daysInt);
|
||||||
|
assertThat(list.toArray(), equalTo(daysArray));
|
||||||
|
assertThat(list.toInteger(), equalTo(daysInt));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue