@Test public void testGetAll() { Hosts hosts = new Hosts(RuntimeEnvironment.application, "hosts.db"); List<Pair<String, String>> all = hosts.getAll(); assertEquals(0, all.size()); hosts.put("ni.hao", "127.0.0.1"); hosts.put("wo.hao", "127.0.0.2"); all = hosts.getAll(); assertEquals(2, all.size()); assertEquals("ni.hao", all.get(0).first); assertEquals("127.0.0.1", all.get(0).second); assertEquals("wo.hao", all.get(1).first); assertEquals("127.0.0.2", all.get(1).second); }
public List<Pair<String, String>> getAll() { List<Pair<String, String>> result = new ArrayList<>(); Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_HOSTS + ";", null); try { while (cursor.moveToNext()) { String host = SqlUtils.getString(cursor, COLUMN_HOST, null); String ip = SqlUtils.getString(cursor, COLUMN_IP, null); InetAddress inetAddress = toInetAddress(host, ip); if (inetAddress == null) { continue; } result.add(new Pair<>(host, ip)); } } finally { cursor.close(); } return result; }
Hosts { public List<Pair<String, String>> getAll() { List<Pair<String, String>> result = new ArrayList<>(); Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_HOSTS + ";", null); try { while (cursor.moveToNext()) { String host = SqlUtils.getString(cursor, COLUMN_HOST, null); String ip = SqlUtils.getString(cursor, COLUMN_IP, null); InetAddress inetAddress = toInetAddress(host, ip); if (inetAddress == null) { continue; } result.add(new Pair<>(host, ip)); } } finally { cursor.close(); } return result; } }
Hosts { public List<Pair<String, String>> getAll() { List<Pair<String, String>> result = new ArrayList<>(); Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_HOSTS + ";", null); try { while (cursor.moveToNext()) { String host = SqlUtils.getString(cursor, COLUMN_HOST, null); String ip = SqlUtils.getString(cursor, COLUMN_IP, null); InetAddress inetAddress = toInetAddress(host, ip); if (inetAddress == null) { continue; } result.add(new Pair<>(host, ip)); } } finally { cursor.close(); } return result; } Hosts(Context context, String name); }
Hosts { public List<Pair<String, String>> getAll() { List<Pair<String, String>> result = new ArrayList<>(); Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_HOSTS + ";", null); try { while (cursor.moveToNext()) { String host = SqlUtils.getString(cursor, COLUMN_HOST, null); String ip = SqlUtils.getString(cursor, COLUMN_IP, null); InetAddress inetAddress = toInetAddress(host, ip); if (inetAddress == null) { continue; } result.add(new Pair<>(host, ip)); } } finally { cursor.close(); } return result; } Hosts(Context context, String name); @Nullable InetAddress get(String host); boolean put(String host, String ip); void delete(String host); List<Pair<String, String>> getAll(); @Nullable static InetAddress toInetAddress(String host, String ip); static boolean isValidHost(String host); static boolean isValidIp(String ip); }
Hosts { public List<Pair<String, String>> getAll() { List<Pair<String, String>> result = new ArrayList<>(); Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_HOSTS + ";", null); try { while (cursor.moveToNext()) { String host = SqlUtils.getString(cursor, COLUMN_HOST, null); String ip = SqlUtils.getString(cursor, COLUMN_IP, null); InetAddress inetAddress = toInetAddress(host, ip); if (inetAddress == null) { continue; } result.add(new Pair<>(host, ip)); } } finally { cursor.close(); } return result; } Hosts(Context context, String name); @Nullable InetAddress get(String host); boolean put(String host, String ip); void delete(String host); List<Pair<String, String>> getAll(); @Nullable static InetAddress toInetAddress(String host, String ip); static boolean isValidHost(String host); static boolean isValidIp(String ip);  }