Example usage for java.util Collection getClass

List of usage examples for java.util Collection getClass

Introduction

In this page you can find the example usage for java.util Collection getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.fornax.cartridges.sculptor.smartclient.server.ScServlet.java

private void sendResponse(PrintWriter output, int startRow, int endRow, Collection<? extends Object> inputData)
        throws IllegalArgumentException {
    inputData = inputData == null ? new ArrayList<Object>() : inputData;
    int showEndRow = endRow < 1 ? 0 : endRow - 1;
    log.log(Level.FINE, "Sending JSON response from {0} to {1} of size {2}",
            new Object[] { startRow, showEndRow, inputData.size() });
    output.write("{response: { status:0, startRow:" + startRow + ", endRow:" + showEndRow + ", totalRows:"
            + inputData.size() + ", data:[");

    // If inputData is Set convert to array
    Object[] setData = null;//  w w  w  .  j a  va2  s  .  c  om
    if (inputData instanceof Set) {
        setData = inputData.toArray();
    } else if (!(inputData instanceof List)) {
        throw new IllegalArgumentException("Datatype " + inputData.getClass().getName()
                + " is not supported (only implementations of java.util.Set and java.util.List)");
    }
    for (int i = startRow; i < endRow; i++) {
        // Get object from collection
        Object obj;
        if (inputData instanceof Set) {
            obj = setData[i];
        } else {
            obj = ((List<? extends Object>) inputData).get(i);
        }

        // Write out separator except first
        if (i != startRow) {
            output.write(", ");
        }

        // Prepare string representation and write out
        String objString = mapObjToOutput(obj, DEF_DEPTH, new Stack(), false, false);
        log.log(Level.FINER, "    >>>>>>>>>>> {0}", objString);
        output.write(objString);
    }
    output.write(" ] } }");
}

From source file:org.apache.ojb.otm.core.ConcreteEditingContext.java

private void setCollectionField(Object obj, PersistentField f, List newCol) {
    Class type = f.getType();//w w w . ja va  2  s .c  o m

    if (Collection.class.isAssignableFrom(type)) {
        Collection col = (Collection) f.get(obj);

        if (col == null) {
            if (type == List.class || type == Collection.class) {
                col = new ArrayList();
            } else if (type == Set.class) {
                col = new HashSet();
            } else {
                try {
                    col = (Collection) type.newInstance();
                } catch (Throwable ex) {
                    System.err.println("Cannot instantiate collection field: " + f);
                    ex.printStackTrace();
                    return;
                }
            }
        } else {
            if (col instanceof CollectionProxyDefaultImpl) {
                CollectionProxyDefaultImpl cp = (CollectionProxyDefaultImpl) col;
                if (col instanceof List) {
                    col = new ListProxyDefaultImpl(_pb.getPBKey(), cp.getData().getClass(), null);
                } else if (col instanceof Set) {
                    col = new SetProxyDefaultImpl(_pb.getPBKey(), cp.getData().getClass(), null);
                } else {
                    col = new CollectionProxyDefaultImpl(_pb.getPBKey(), cp.getData().getClass(), null);
                }
                col.clear();
            } else {
                try {
                    col = (Collection) col.getClass().newInstance();
                } catch (Exception ex) {
                    System.err.println("Cannot instantiate collection field: " + f);
                    ex.printStackTrace();
                    return;
                }
            }
        }
        col.addAll(newCol);
        f.set(obj, col);
    } else if (type.isArray()) {
        int length = newCol.size();
        Object array = Array.newInstance(type.getComponentType(), length);

        for (int i = 0; i < length; i++) {
            Array.set(array, i, newCol.get(i));
        }
        f.set(obj, array);
    }
}

From source file:de.innovationgate.webgate.api.jdbc.WGDatabaseImpl.java

/**
 * @param list/*from   ww w  .j  av a  2s .co m*/
 * @return
 */
private Collection cloneCollection(Collection col) {

    if (col == null) {
        return null;
    }

    if (col instanceof List) {
        List list = new ArrayList();
        list.addAll((List) col);
        return list;
    } else if (col instanceof Set) {
        Set set = new HashSet();
        set.addAll((Set) col);
        return set;
    } else if (col instanceof Bag) {
        Bag bag = new HashBag();
        bag.addAll((Bag) col);
        return bag;
    } else {
        throw new IllegalArgumentException("Cannot clone collections of type: " + col.getClass().getName());
    }

}

From source file:ca.oson.json.Oson.java

private <E> Collection json2Collection(FieldData objectDTO) {
    Object value = objectDTO.valueToProcess;
    Collection<E> returnObj = (Collection<E>) objectDTO.returnObj;
    Class<Collection<E>> returnType = objectDTO.returnType;
    Collection<E> defaultValue = (Collection<E>) objectDTO.defaultValue;

    if (returnType == null) {
        if (returnType == null && returnObj != null) {
            returnType = (Class<Collection<E>>) returnObj.getClass();
        }/* w ww. j  a va 2  s. com*/

        if (returnType == null) {
            returnType = (Class<Collection<E>>) DefaultValue.collection(returnType).getClass();
        }

        objectDTO.returnType = returnType;
    }

    // isEmpty
    if (!StringUtil.isNull(value)) {
        Collection<E> collection = null;
        try {
            collection = (Collection<E>) value;
        } catch (Exception e) {
            collection = DefaultValue.collection(returnType); // new ArrayList();
        }

        if (collection.size() > 0) {
            Function function = objectDTO.getDeserializer();

            if (function != null) {
                try {
                    Object returnedValue = null;
                    // suppose to return String, but in case not, try to process
                    if (function instanceof Json2DataMapperFunction) {
                        DataMapper classData = new DataMapper(returnType, collection, objectDTO.classMapper,
                                objectDTO.level, getPrettyIndentation());
                        returnedValue = ((Json2DataMapperFunction) function).apply(classData);

                    } else if (function instanceof Json2FieldDataFunction) {
                        Json2FieldDataFunction f = (Json2FieldDataFunction) function;
                        FieldData fieldData = objectDTO.clone();

                        returnedValue = f.apply(fieldData);

                    } else if (function instanceof Json2CollectionFunction) {
                        return ((Json2CollectionFunction) function).apply(collection);

                    } else {
                        returnedValue = function.apply(collection);
                    }

                    if (returnedValue == null) {
                        return null;

                    } else if (Collection.class.isAssignableFrom(returnedValue.getClass())) {
                        return (Collection) returnedValue;

                    } else if (returnedValue.getClass().isArray()) {
                        return Arrays.asList((Object[]) returnedValue);
                    } else {
                        // do not know what to do
                    }

                } catch (Exception e) {
                }
            }

            if (returnObj == null) {
                if (defaultValue != null) {
                    returnObj = defaultValue;
                }

                if (EnumSet.class.isAssignableFrom(returnType)) {
                    ComponentType type = getComponentType();
                    Class enm = type.getComponentType().getClassType();
                    if (enm.isEnum()) {
                        returnObj = (Collection<E>) EnumSet.allOf(enm);
                    }
                }

                if (returnObj == null) {
                    returnObj = newInstance(new HashMap(), returnType);
                }

                if (returnObj == null) {
                    returnObj = DefaultValue.collection(returnType);
                }

                objectDTO.returnObj = returnObj;
            }

            objectDTO.incrLevel();

            objectDTO.valueToProcess = collection;
            //Class<E> componentType = objectDTO.getComponentType(getJsonClassType());
            Class<E> componentType = guessComponentType(objectDTO);

            for (E val : collection) {
                if (val != null) {
                    FieldData newFieldData = new FieldData(val, objectDTO.returnType, objectDTO.json2Java,
                            objectDTO.level, objectDTO.set);
                    newFieldData.componentType = objectDTO.componentType;
                    if (ObjectUtil.isSameDataType(componentType, val.getClass())
                            && ObjectUtil.isBasicDataType(val.getClass())) {
                        newFieldData.returnType = componentType;
                    } else {
                        newFieldData.returnType = guessComponentType(newFieldData);
                    }
                    newFieldData.fieldMapper = objectDTO.fieldMapper;
                    returnObj.add(json2Object(newFieldData));
                }
            }

            if (objectDTO.classMapper.orderArrayAndList) {
                try {
                    if (!List.class.isAssignableFrom(returnObj.getClass())) {
                        returnObj = new ArrayList(returnObj);
                    }
                    Collections.sort((List) returnObj);
                } catch (Exception ex) {
                }
            }

            return returnObj;

        } else {
            return collection;
        }
    }

    return json2CollectionDefault(objectDTO);
}