private static Object valueToArray(Class<?> arrayComponentType, Object value) throws Exception { if (value instanceof Array) { Array valueArray = (Array)value; int arraySize = valueArray.size(); Object newArray = java.lang.reflect.Array.newInstance(arrayComponentType, arraySize); for (int i = 0; i < ((Array)value).size(); i++) { Object element = valueArray.get(i); element = convertValueToType(arrayComponentType, element); java.lang.reflect.Array.set(newArray, i, element); } return newArray; } else if (value instanceof String) { // Parse the string into a comma-separated set of values (this works if it's just a single value) ... String strValue = (String)value; if (strValue.length() > 0) { String[] stringValues = strValue.split(","); Object newArray = java.lang.reflect.Array.newInstance(arrayComponentType, stringValues.length); for (int i = 0; i < stringValues.length; i++) { Object element = convertValueToType(arrayComponentType, stringValues[i]); java.lang.reflect.Array.set(newArray, i, element); } return newArray; } } // Otherwise, just initialize it to an empty array ... return java.lang.reflect.Array.newInstance(arrayComponentType, 0); }
@Override public List<String> getParentIds() { List<String> parents = new ArrayList<String>(); if (!federatedDocument.containsField(DocumentTranslator.PARENT)) { return parents; } Object parentFieldValue = federatedDocument.get(DocumentTranslator.PARENT); if (parentFieldValue instanceof Array) { for (Array.Entry entry : ((Array)parentFieldValue).getEntries()) { parents.add(entry.getValue().toString()); } } else { parents.add(parentFieldValue.toString()); } return parents; }
@Override public MutableArray clone() { BasicArray clone = new BasicArray(); for (Object value : this) { value = unwrap(value); if (value instanceof Array) { value = ((Array)value).clone(); } else if (value instanceof Document) { value = ((Document)value).clone(); }// every other kind of value is immutable clone.addValue(value); } return clone; }
@Override public MutableArray clone() { BasicArray clone = new BasicArray(); for (Object value : this) { value = unwrap(value); if (value instanceof Array) { value = ((Array)value).clone(); } else if (value instanceof Document) { value = ((Document)value).clone(); }// every other kind of value is immutable clone.addValue(value); } return clone; }
private static Object valueToArray(Class<?> arrayComponentType, Object value) throws Exception { if (value instanceof Array) { Array valueArray = (Array)value; int arraySize = valueArray.size(); Object newArray = java.lang.reflect.Array.newInstance(arrayComponentType, arraySize); for (int i = 0; i < ((Array)value).size(); i++) { Object element = valueArray.get(i); element = convertValueToType(arrayComponentType, element); java.lang.reflect.Array.set(newArray, i, element); } return newArray; } else if (value instanceof String) { // Parse the string into a comma-separated set of values (this works if it's just a single value) ... String strValue = (String)value; if (strValue.length() > 0) { String[] stringValues = strValue.split(","); Object newArray = java.lang.reflect.Array.newInstance(arrayComponentType, stringValues.length); for (int i = 0; i < stringValues.length; i++) { Object element = convertValueToType(arrayComponentType, stringValues[i]); java.lang.reflect.Array.set(newArray, i, element); } return newArray; } } // Otherwise, just initialize it to an empty array ... return java.lang.reflect.Array.newInstance(arrayComponentType, 0); }
@Override public List<String> getParentIds() { List<String> parents = new ArrayList<String>(); if (!federatedDocument.containsField(DocumentTranslator.PARENT)) { return parents; } Object parentFieldValue = federatedDocument.get(DocumentTranslator.PARENT); if (parentFieldValue instanceof Array) { for (Array.Entry entry : ((Array)parentFieldValue).getEntries()) { parents.add(entry.getValue().toString()); } } else { parents.add(parentFieldValue.toString()); } return parents; }
@Override public MutableDocument clone() { BasicDocument clone = new BasicDocument(); for (Field field : this.fields()) { Object value = unwrap(field.getValue()); if (value instanceof Array) { value = ((Array)value).clone(); } else if (value instanceof Document) { value = ((Document)value).clone(); }// every other kind of value is immutable clone.put(field.getName(), value); } return clone; }
protected void assertEquals( Array array1, Array array2 ) { assertTrue(array1 != array2); Assert.assertEquals(array1, array2); for (int i = 0; i != array1.size(); ++i) { Object value1 = array1.get(i); Object value2 = array2.get(i); Assert.assertEquals(value1, value2); if (value2 instanceof Array) { assertTrue(value1 != value2); assertEquals((Array) value1, (Array) value2); } else if (value2 instanceof Document) { assertTrue(value1 != value2); assertEquals((Document)value1, (Document)value2); } else { // The values can actually be the same instances since they're immutable ... } } } }
@Override public MutableDocument clone() { BasicDocument clone = new BasicDocument(); for (Field field : this.fields()) { Object value = unwrap(field.getValue()); if (value instanceof Array) { value = ((Array)value).clone(); } else if (value instanceof Document) { value = ((Document)value).clone(); }// every other kind of value is immutable clone.put(field.getName(), value); } return clone; }