Preferences: allow storing Long arrays

pull/428/head
Alinson S. Xavier 8 years ago
parent def71d8141
commit 1ad06bcc15

@ -21,6 +21,7 @@ package org.isoron.uhabits.core.preferences;
import android.support.annotation.*;
import org.apache.commons.lang3.*;
import org.isoron.uhabits.core.models.*;
import org.isoron.uhabits.core.ui.*;
@ -364,5 +365,17 @@ public class Preferences
void putString(String key, String value);
void remove(String key);
default void putLongArray(String key, Long[] values) {
putString(key, StringUtils.join(values, ','));
}
default Long[] getLongArray(String key) {
String strNumbers = getString(key, "");
String parts[] = StringUtils.split(strNumbers, ',');
Long numbers[] = new Long[parts.length];
for (int i = 0; i < parts.length; i++) numbers[i] = Long.valueOf(parts[i]);
return numbers;
}
}
}

@ -23,7 +23,9 @@ import org.isoron.uhabits.core.*;
import org.junit.*;
import java.io.*;
import java.util.*;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
@ -88,4 +90,30 @@ public class PropertiesStorageTest extends BaseUnitTest
assertThat(storage2.getLong("intKey", 200L), equalTo(64L));
assertThat(storage2.getString("stringKey", ""), equalTo("Hello"));
}
@Test
public void testLongArray() throws Exception
{
Long[] expected1 = new Long[]{1L, 2L, 3L, 5L};
Long[] expected2 = new Long[]{1L};
Long[] expected3 = new Long[]{};
Long[] expected4 = new Long[]{};
storage.putLongArray("key1", expected1);
storage.putLongArray("key2", expected2);
storage.putLongArray("key3", expected3);
Long[] actual1 = storage.getLongArray("key1");
Long[] actual2 = storage.getLongArray("key2");
Long[] actual3 = storage.getLongArray("key3");
Long[] actual4 = storage.getLongArray("invalidKey");
assertTrue(Arrays.equals(actual1, expected1));
assertTrue(Arrays.equals(actual2, expected2));
assertTrue(Arrays.equals(actual3, expected3));
assertTrue(Arrays.equals(actual4, expected4));
assertEquals("1,2,3,5", storage.getString("key1", ""));
assertEquals(1, storage.getLong("key2", -1));
}
}

Loading…
Cancel
Save