@Test public void testPut() { Hosts hosts = new Hosts(RuntimeEnvironment.application, "hosts.db"); assertEquals(null, hosts.get("ni.hao")); assertEquals(true, hosts.put("ni.hao", "127.0.0.1")); assertEquals("ni.hao/127.0.0.1", hosts.get("ni.hao").toString()); assertEquals(true, hosts.put("ni.hao", "127.0.0.2")); assertEquals("ni.hao/127.0.0.2", hosts.get("ni.hao").toString()); assertEquals(false, hosts.put(".wo.hao", "127.0.0.1")); assertEquals(null, hosts.get(".wo.hao")); assertEquals(false, hosts.put("wo.hao", ".127.0.0.1")); assertEquals(null, hosts.get("wo.hao")); assertEquals(false, hosts.put(null, "127.0.0.1")); assertEquals(false, hosts.put("wo.hao", null)); assertEquals(false, hosts.put(null, null)); assertEquals(null, hosts.get("wo.hao")); }
public boolean put(String host, String ip) { if (!isValidHost(host) || !isValidIp(ip)) { return false; } ContentValues values = new ContentValues(); values.put(COLUMN_HOST, host); values.put(COLUMN_IP, ip); if (contains(host)) { db.update(TABLE_HOSTS, values, COLUMN_HOST + " = ?", new String[] { host }); } else { db.insert(TABLE_HOSTS, null, values); } return true; }
Hosts { public boolean put(String host, String ip) { if (!isValidHost(host) || !isValidIp(ip)) { return false; } ContentValues values = new ContentValues(); values.put(COLUMN_HOST, host); values.put(COLUMN_IP, ip); if (contains(host)) { db.update(TABLE_HOSTS, values, COLUMN_HOST + " = ?", new String[] { host }); } else { db.insert(TABLE_HOSTS, null, values); } return true; } }
Hosts { public boolean put(String host, String ip) { if (!isValidHost(host) || !isValidIp(ip)) { return false; } ContentValues values = new ContentValues(); values.put(COLUMN_HOST, host); values.put(COLUMN_IP, ip); if (contains(host)) { db.update(TABLE_HOSTS, values, COLUMN_HOST + " = ?", new String[] { host }); } else { db.insert(TABLE_HOSTS, null, values); } return true; } Hosts(Context context, String name); }
Hosts { public boolean put(String host, String ip) { if (!isValidHost(host) || !isValidIp(ip)) { return false; } ContentValues values = new ContentValues(); values.put(COLUMN_HOST, host); values.put(COLUMN_IP, ip); if (contains(host)) { db.update(TABLE_HOSTS, values, COLUMN_HOST + " = ?", new String[] { host }); } else { db.insert(TABLE_HOSTS, null, values); } return true; } 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 boolean put(String host, String ip) { if (!isValidHost(host) || !isValidIp(ip)) { return false; } ContentValues values = new ContentValues(); values.put(COLUMN_HOST, host); values.put(COLUMN_IP, ip); if (contains(host)) { db.update(TABLE_HOSTS, values, COLUMN_HOST + " = ?", new String[] { host }); } else { db.insert(TABLE_HOSTS, null, values); } return true; } 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);  }