下面列出了org.apache.commons.lang3.ArrayUtils#toObject ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public List<Double> getValue() {
try {
String values = textField.getText().replaceAll("\\s", "");
String[] strValues = values.split(",");
double[] doubleValues = new double[strValues.length];
for (int i = 0; i < strValues.length; i++) {
try {
doubleValues[i] = Double.parseDouble(strValues[i]);
} catch (NumberFormatException nfe) {
// The string does not contain a parsable integer.
}
}
Double[] doubleArray = ArrayUtils.toObject(doubleValues);
List<Double> ranges = Arrays.asList(doubleArray);
return ranges;
} catch (Exception e) {
return null;
}
}
private static <T> T[] genericArrayToJavaArray(GenericArray value, LogicalType eleType) {
Object array = value.getArray();
if (value.isPrimitiveArray()) {
switch (eleType.getTypeRoot()) {
case BOOLEAN:
return (T[]) ArrayUtils.toObject((boolean[]) array);
case TINYINT:
return (T[]) ArrayUtils.toObject((byte[]) array);
case SMALLINT:
return (T[]) ArrayUtils.toObject((short[]) array);
case INTEGER:
return (T[]) ArrayUtils.toObject((int[]) array);
case BIGINT:
return (T[]) ArrayUtils.toObject((long[]) array);
case FLOAT:
return (T[]) ArrayUtils.toObject((float[]) array);
case DOUBLE:
return (T[]) ArrayUtils.toObject((double[]) array);
default:
throw new RuntimeException("Not a primitive type: " + eleType);
}
} else {
return (T[]) array;
}
}
public static List asList(Object value)
{
if (value instanceof List) return (List)value;
else if (value.getClass().isArray())
{
Class componentClass = value.getClass().getComponentType();
if (componentClass.isPrimitive())
{
if (componentClass.equals(char.class)) value = ArrayUtils.toObject((char[])value);
else if (componentClass.equals(long.class)) value = ArrayUtils.toObject((long[])value);
else if (componentClass.equals(int.class)) value = ArrayUtils.toObject((int[])value);
else if (componentClass.equals(short.class)) value = ArrayUtils.toObject((short[])value);
else if (componentClass.equals(byte.class)) value = ArrayUtils.toObject((byte[])value);
else if (componentClass.equals(double.class)) value = ArrayUtils.toObject((double[])value);
else if (componentClass.equals(float.class)) value = ArrayUtils.toObject((float[])value);
}
return new ArrayList(Arrays.asList((Object[])value));
}
else
{
List ret = new ArrayList();
ret.add(value);
return ret;
}
}
/**
* deletes all selected columns if it is not present in the <code>exp</code>
* List
*
* @param table the table to DELETE columns
* @param exp columns to avoid deleting
* @see #deletecol(javax.swing.JTable, int)
*/
static void deletecols(JTable table, int[] exp) {
Integer[] selcols;
try {
TableColumnModel tcm = table.getColumnModel();
selcols = ArrayUtils.toObject(table.getSelectedColumns());
Arrays.sort(selcols, Collections.reverseOrder());
List<Integer> explist = Ints.asList(exp);
for (int i : selcols) {
if (!explist.contains(i)) {
tcm.removeColumn(tcm.getColumn(i));
}
}
} catch (Exception e) {
Logger.getLogger(JtableUtils.class.getName()).log(Level.SEVERE, null, e);
}
}
@SuppressWarnings("unchecked")
private static <T> T[] genericArrayToJavaArray(GenericArrayData value, LogicalType eleType) {
if (value.isPrimitiveArray()) {
switch (eleType.getTypeRoot()) {
case BOOLEAN:
return (T[]) ArrayUtils.toObject(value.toBooleanArray());
case TINYINT:
return (T[]) ArrayUtils.toObject(value.toByteArray());
case SMALLINT:
return (T[]) ArrayUtils.toObject(value.toShortArray());
case INTEGER:
return (T[]) ArrayUtils.toObject(value.toIntArray());
case BIGINT:
return (T[]) ArrayUtils.toObject(value.toLongArray());
case FLOAT:
return (T[]) ArrayUtils.toObject(value.toFloatArray());
case DOUBLE:
return (T[]) ArrayUtils.toObject(value.toDoubleArray());
default:
throw new RuntimeException("Not a primitive type: " + eleType);
}
} else {
return (T[]) value.toObjectArray();
}
}
@Override
public void loadValueFromXML(Element xmlElement) {
String values = xmlElement.getTextContent().replaceAll("\\s", "");
String[] strValues = values.split(",");
double[] doubleValues = new double[strValues.length];
for (int i = 0; i < strValues.length; i++) {
try {
doubleValues[i] = Double.parseDouble(strValues[i]);
} catch (NumberFormatException nfe) {
// The string does not contain a parsable integer.
}
}
Double[] doubleArray = ArrayUtils.toObject(doubleValues);
List<Double> ranges = Arrays.asList(doubleArray);
value = ranges;
}
public List<Double> getValue() {
try {
String values = textField.getText().replaceAll("\\s", "");
String[] strValues = values.split(",");
double[] doubleValues = new double[strValues.length];
for (int i = 0; i < strValues.length; i++) {
try {
doubleValues[i] = Double.parseDouble(strValues[i]);
} catch (NumberFormatException nfe) {
// The string does not contain a parsable integer.
}
}
Double[] doubleArray = ArrayUtils.toObject(doubleValues);
List<Double> ranges = Arrays.asList(doubleArray);
return ranges;
} catch (Exception e) {
return null;
}
}
public static Byte[] getConstantArgument(Mutable<ILogicalExpression> searchM, int arg) {
AbstractFunctionCallExpression searchFunction = (AbstractFunctionCallExpression) searchM.getValue();
ILogicalExpression argType = searchFunction.getArguments().get(arg).getValue();
searchFunction.getArguments().size();
if (argType.getExpressionTag() != LogicalExpressionTag.CONSTANT) {
return null;
}
TaggedValuePointable tvp = (TaggedValuePointable) TaggedValuePointable.FACTORY.createPointable();
ExpressionToolbox.getConstantAsPointable((ConstantExpression) argType, tvp);
return ArrayUtils.toObject(tvp.getByteArray());
}
@Test
public void givenArray_whenUsingSortWithLambdas_thenSortedArray() {
Integer[] integersToSort = ArrayUtils.toObject(toSort);
Arrays.sort(integersToSort, Comparator.comparingInt(a -> a));
assertTrue(Arrays.equals(integersToSort, ArrayUtils.toObject(sortedInts)));
}
public static int[] removeDuplicateWithOrderIntArray(int[] array) {
// Box
Integer[] list = ArrayUtils.toObject(array);
// Remove duplicates
Set<Integer> set = new LinkedHashSet<Integer>(Arrays.asList(list));
// Create array and unbox
return ArrayUtils.toPrimitive(set.toArray(new Integer[set.size()]));
}
/**
* Convert a value so it can be injected into a UIMA component. UIMA only supports several
* parameter types. If the value is not of these types, this method can be used to coerce the
* value into a supported type (typically String). It is also used to convert primitive arrays to
* object arrays when necessary.
*
* @param param
* the configuration parameter.
* @param aValue
* the parameter value.
* @return the converted value.
*/
protected static Object convertParameterValue(ConfigurationParameter param, Object aValue) {
Object value = aValue;
if (value.getClass().isArray()
&& value.getClass().getComponentType().getName().equals("boolean")) {
value = ArrayUtils.toObject((boolean[]) value);
} else if (value.getClass().isArray()
&& value.getClass().getComponentType().getName().equals("int")) {
value = ArrayUtils.toObject((int[]) value);
} else if (value.getClass().isArray()
&& value.getClass().getComponentType().getName().equals("float")) {
value = ArrayUtils.toObject((float[]) value);
} else {
try {
if (param.getType().equals(ConfigurationParameter.TYPE_STRING)) {
SimpleTypeConverter converter = new SimpleTypeConverter();
PropertyEditorUtil.registerUimaFITEditors(converter);
if (value.getClass().isArray() || value instanceof Collection) {
value = converter.convertIfNecessary(value, String[].class);
} else {
value = converter.convertIfNecessary(value, String.class);
}
}
} catch (TypeMismatchException e) {
throw new IllegalArgumentException(e.getMessage(), e);
}
}
return value;
}
@Override
public Long[] findCategoryIdsByProductId(long productId) {
List<Record> records = Db.find("select * from product_category_mapping where product_id = ?", productId);
if (records == null || records.isEmpty()) {
return null;
}
return ArrayUtils.toObject(records.stream().mapToLong(record -> record.get("category_id")).toArray());
}
public RelatedBinaryContentImpl(
Date dateOfLoading,
String uri,
SSP ssp,
byte[] binaryContent) {
super(dateOfLoading, uri, ssp);
this.binaryContent = ArrayUtils.toObject(binaryContent);
}
public Byte[] convertConstantToInteger(Mutable<ILogicalExpression> finds) {
TaggedValuePointable tvp = (TaggedValuePointable) TaggedValuePointable.FACTORY.createPointable();
ExpressionToolbox.getConstantAsPointable((ConstantExpression) finds.getValue(), tvp);
return ArrayUtils.toObject(tvp.getByteArray());
}
@Override
public boolean isValid(Double value, ConstraintValidatorContext context) {
return super.isValid(value, ArrayUtils.toObject(annotation.value()), Objects::equals, context);
}
@Override
public boolean isValid(Integer value, ConstraintValidatorContext context) {
return super.isValid(value, ArrayUtils.toObject(annotation.value()), Objects::equals, context);
}
/**
* @return
* @since 1.10.3
*/
private static Object[][] build(){
int[] int1 = { 2, 1 };
// Object[] array = toArray(int1);
Object[] array = ArrayUtils.toObject(int1);
ToStringConfig toStringConfig = new ToStringConfig(",");
Object[] arrays = { "222", "1111" };
Integer[] array1 = { 2, 1 };
ToStringConfig toStringConfig1 = new ToStringConfig(",", false);
Integer[] array2 = { 2, 1, null };
Integer[] array3 = { 2, null, 1, null };
String[] ss = { null };
return new Object[][] {
{ array, null, "2,1" },
{ toArray(2), toStringConfig, "2" },
{ toArray(",", ","), new ToStringConfig(",", true), ",,," },
{ null, toStringConfig, EMPTY },
{ toArray(), toStringConfig, EMPTY },
//---------------------------------------------------------------
//since 1.12.9
{ toArray("189", "150"), new ToStringConfig(",", false, "code:"), "code:189,code:150" },
{ toArray("189", null, "150"), new ToStringConfig(",", false, "code:"), "code:189,code:150" },
{ toArray("189", "", "150"), new ToStringConfig(",", false, "code:"), "code:189,code:150" },
{ toArray("189", "150"), new ToStringConfig(" OR ", false, "code:"), "code:189 OR code:150" },
{ toArray("189", "", "150"), new ToStringConfig(" OR ", false, "code:"), "code:189 OR code:150" },
{ toArray("189", "", "150", ""), new ToStringConfig(" OR ", false, "code:"), "code:189 OR code:150" },
{ toArray("189", "", "150", ""), new ToStringConfig(" OR ", false, null), "189 OR 150" },
{ toArray("189", "", "150", ""), new ToStringConfig(" OR ", false, ""), "189 OR 150" },
{ toArray("189", "", "150", ""), new ToStringConfig(" OR ", false, " "), " 189 OR 150" },
{ toArray("", "189", "", "150", ""), new ToStringConfig(" OR ", false, "code:"), "code:189 OR code:150" },
{ toArray("189", "", "150"), new ToStringConfig(",", true, "code:"), "code:189,code:,code:150" },
{ toArray("189", " ", "150"), new ToStringConfig(",", true, "code:"), "code:189,code: ,code:150" },
//---------------------------------------------------------------
{ arrays, toStringConfig, "222,1111" },
{ array1, toStringConfig, "2,1" },
{ array2, toStringConfig1, "2,1" },
{ array3, toStringConfig1, "2,1" },
{ toArray(new Integer(2), null), new ToStringConfig(",", true), "2," },
{ toArray(new Integer(2), null), new ToStringConfig(",", true), "2," },
{ ss, new ToStringConfig(",", true), EMPTY }, };
}
private static Object getDefaultValue(AnnotatedElement element, Type type, InjectAnnotationProcessor2 annotationProcessor) {
if (annotationProcessor != null && annotationProcessor.hasDefault()) {
return annotationProcessor.getDefault();
}
Default defaultAnnotation = element.getAnnotation(Default.class);
if (defaultAnnotation == null) {
return null;
}
Object value = null;
if (type instanceof Class) {
Class<?> injectedClass = (Class<?>) type;
if (injectedClass.isArray()) {
Class<?> componentType = injectedClass.getComponentType();
if (componentType == String.class) {
value = defaultAnnotation.values();
} else if (componentType == Integer.TYPE) {
value = defaultAnnotation.intValues();
} else if (componentType == Integer.class) {
value = ArrayUtils.toObject(defaultAnnotation.intValues());
} else if (componentType == Long.TYPE) {
value = defaultAnnotation.longValues();
} else if (componentType == Long.class) {
value = ArrayUtils.toObject(defaultAnnotation.longValues());
} else if (componentType == Boolean.TYPE) {
value = defaultAnnotation.booleanValues();
} else if (componentType == Boolean.class) {
value = ArrayUtils.toObject(defaultAnnotation.booleanValues());
} else if (componentType == Short.TYPE) {
value = defaultAnnotation.shortValues();
} else if (componentType == Short.class) {
value = ArrayUtils.toObject(defaultAnnotation.shortValues());
} else if (componentType == Float.TYPE) {
value = defaultAnnotation.floatValues();
} else if (componentType == Float.class) {
value = ArrayUtils.toObject(defaultAnnotation.floatValues());
} else if (componentType == Double.TYPE) {
value = defaultAnnotation.doubleValues();
} else if (componentType == Double.class) {
value = ArrayUtils.toObject(defaultAnnotation.doubleValues());
} else {
log.warn("Default values for {} are not supported", componentType);
}
} else {
if (injectedClass == String.class) {
value = defaultAnnotation.values().length == 0 ? "" : defaultAnnotation.values()[0];
} else if (injectedClass == Integer.class) {
value = defaultAnnotation.intValues().length == 0 ? 0 : defaultAnnotation.intValues()[0];
} else if (injectedClass == Long.class) {
value = defaultAnnotation.longValues().length == 0 ? 0l : defaultAnnotation.longValues()[0];
} else if (injectedClass == Boolean.class) {
value = defaultAnnotation.booleanValues().length == 0 ? false : defaultAnnotation.booleanValues()[0];
} else if (injectedClass == Short.class) {
value = defaultAnnotation.shortValues().length == 0 ? ((short) 0) : defaultAnnotation.shortValues()[0];
} else if (injectedClass == Float.class) {
value = defaultAnnotation.floatValues().length == 0 ? 0f : defaultAnnotation.floatValues()[0];
} else if (injectedClass == Double.class) {
value = defaultAnnotation.doubleValues().length == 0 ? 0d : defaultAnnotation.doubleValues()[0];
} else {
log.warn("Default values for {} are not supported", injectedClass);
}
}
} else {
log.warn("Cannot provide default for {}", type);
}
return value;
}
Byte[] toBytes(Integer v) {
Byte[] barr = ArrayUtils.toObject(ByteBuffer.allocate(9).putLong(1, v).array());
barr[0] = ValueTag.XS_INTEGER_TAG;
return barr;
}
@Override
public Character[] getValue() {
return ArrayUtils.toObject(value.toCharArray());
}