@Override @SuppressWarnings("unchecked") public Map toMap() { Map m = new HashMap(); Iterator i = this.keySet().iterator(); while (i.hasNext()) { Object s = i.next(); m.put(s, this.get(String.valueOf(s))); } return m; }
/** * Gets a value at an index. For interface compatibility. Must be passed a String that is parsable to an int. * * @param key the index * @return the value, if found, or null * @throws IllegalArgumentException if {@code key} cannot be parsed into an {@code int} */ public Object get(final String key) { int i = _getInt(key); if (i < 0) { return null; } if (i >= size()) { return null; } return get(i); }
/** * Converts a BSONObject to a map. * * @return the Map Object */ @SuppressWarnings("unchecked") public Map toMap() { Map m = new HashMap(); Iterator i = this.keySet().iterator(); while (i.hasNext()) { Object s = i.next(); m.put(s, this.get(String.valueOf(s))); } return m; }
public RPCRequest(BSONObject obj) { BasicBSONList list = (BasicBSONList) obj.get("data"); this.requestId = (Integer) list.get(1); this.methodName = list.get(2).toString(); this.parameters = ((BasicBSONList) list.get(3)).toArray(); if (obj.containsField("return")) { this.responseQueue = obj.get("return").toString(); } }
/** * Gets a value at an index. For interface compatibility. Must be passed a * String that is parsable to an int. * * @param key * the index * @return the value, if found, or null * @throws IllegalArgumentException * if <code>key</code> cannot be parsed into an <code>int</code> */ public Object get(String key) { int i = _getInt(key); if (i < 0) return null; if (i >= size()) return null; return get(i); }
/** * Get all the BSON values. * @return All the BSON values by "java.util#Collection<Object>". * @throws Exception IllegalArgumentException when the type of BSON value is not supported. */ public Object asList() { Collection<Object> colletion = new LinkedList<Object>(); for (String key : this.keySet()) { Object v = this.get(key); if (v == null) { continue; } else if (BSON.IsBasicType(v)) { colletion.add(v); } else if (v instanceof BasicBSONList) { colletion.add(((BasicBSONList) v).asList()); } else if (v instanceof BasicBSONObject) { colletion.add(((BasicBSONObject) v).asMap()); } else { throw new IllegalArgumentException( "can't support in list. value_type=" + v.getClass()); } } return colletion; } }
eleObj = this.get(key);
BSONObject nodeInfo = (BSONObject) nodesInfoList.get(i); Object nodeIdValue = nodeInfo.get(SdbConstants.FIELD_NAME_NODEID); if (nodeIdValue == null) { nodeInfoObj = (BSONObject) nodesInfoList.get(0); } else if (validPositions.size() == 1) { nodeInfoObj = (BSONObject) nodesInfoList.get(nodeIndex); } else { int position = 0; nodeInfoObj = (BSONObject) nodesInfoList.get(nodeIndex);
@Test public void testCreateFromExistingConfig() { // Replica set of one member int version = 44; BasicBSONObject config = makeSetConfig("replica-set-name", version, makeSetMember(33, "example.com:7777")); // Use existing set to add two more members BasicBSONObject newConfig = ReplicaSetConfig.fromExistingConfig(config) .member("foo", 8888, 34) .member("bar", 9999, 35) .build(); assertEquals(newConfig.get("_id"), "replica-set-name"); assertEquals(newConfig.get("version"), version + 1); BasicBSONList members = (BasicBSONList) newConfig.get("members"); assertEquals(members.size(), 3); BSONObject original = (BSONObject) members.get(0); assertEquals(original.get("_id"), 33); assertEquals(original.get("host"), "example.com:7777"); BSONObject second = (BSONObject) members.get(1); assertEquals(second.get("_id"), 34); assertEquals(second.get("host"), "foo:8888"); BSONObject third = (BSONObject) members.get(2); assertEquals(third.get("_id"), 35); assertEquals(third.get("host"), "bar:9999"); }
@Test public void testRemoveMember() { int version = 44; BasicBSONObject config = makeSetConfig("replica-set-name", version, makeSetMember(33, "example.com:7777"), makeSetMember(34, "example.com:7778")); // Use existing set to add two more members BasicBSONObject newConfig = ReplicaSetConfig.fromExistingConfig(config) .remove("example.com", 7777) .build(); assertEquals(newConfig.get("version"), version + 1); BasicBSONList members = (BasicBSONList) newConfig.get("members"); assertEquals(members.size(), 1); assertEquals(BSONObject.class.cast(members.get(0)).get("host"), "example.com:7778"); newConfig = ReplicaSetConfig.fromExistingConfig(newConfig) .remove("example.com", 7778) .build(); members = (BasicBSONList) newConfig.get("members"); assertTrue(members.isEmpty()); }