List of usage examples for java.lang Double TYPE
Class TYPE
To view the source code for java.lang Double TYPE.
Click Source Link
From source file:org.nuxeo.ecm.automation.io.services.codec.ObjectCodecService.java
protected final void writeGenericObject(JsonGenerator jg, Class<?> clazz, Object object) throws IOException { jg.writeStartObject();//from w w w . ja va2s . co m if (clazz.isPrimitive()) { if (clazz == Boolean.TYPE) { jg.writeStringField("entity-type", "boolean"); jg.writeBooleanField("value", (Boolean) object); } else if (clazz == Double.TYPE || clazz == Float.TYPE) { jg.writeStringField("entity-type", "number"); jg.writeNumberField("value", ((Number) object).doubleValue()); } else if (clazz == Integer.TYPE || clazz == Long.TYPE || clazz == Short.TYPE || clazz == Byte.TYPE) { jg.writeStringField("entity-type", "number"); jg.writeNumberField("value", ((Number) object).longValue()); } else if (clazz == Character.TYPE) { jg.writeStringField("entity-type", "string"); jg.writeStringField("value", object.toString()); } return; } if (jg.getCodec() == null) { jg.setCodec(new ObjectMapper()); } if (object instanceof Iterable && clazz.getName().startsWith("java.")) { jg.writeStringField("entity-type", "list"); } else if (object instanceof Map && clazz.getName().startsWith("java.")) { if (object instanceof LinkedHashMap) { jg.writeStringField("entity-type", "orderedMap"); } else { jg.writeStringField("entity-type", "map"); } } else { jg.writeStringField("entity-type", clazz.getName()); } jg.writeObjectField("value", object); jg.writeEndObject(); }
From source file:com.jaliansystems.activeMQLite.impl.ObjectRepository.java
private boolean paramMatches(Method method, Object[] params) { Class<?>[] parameterTypes = method.getParameterTypes(); if (params != null && parameterTypes.length != params.length || params == null && parameterTypes.length != 0) return false; for (int i = 0; i < parameterTypes.length; i++) { Class<?> class1 = parameterTypes[i]; if (!class1.isPrimitive() && params[i] == null) continue; if (params[i] instanceof ObjectHandle) { params[i] = getObject(((ObjectHandle) params[i])); }//from w ww . j av a 2 s . c o m if (class1.isPrimitive()) { if (params[i] instanceof Boolean && class1 != Boolean.TYPE) return false; if (params[i] instanceof Integer && class1 != Integer.TYPE) return false; if (params[i] instanceof Long && class1 != Long.TYPE) return false; if (params[i] instanceof Short && class1 != Short.TYPE) return false; if (params[i] instanceof Float && class1 != Float.TYPE) return false; if (params[i] instanceof Double && class1 != Double.TYPE) return false; if (params[i] instanceof Byte && class1 != Byte.TYPE) return false; } else if (!class1.isInstance(params[i])) return false; } return true; }
From source file:javadz.beanutils.ConvertUtilsBean.java
/** * Sets the default value for Double conversions. * @param newDefaultDouble The default Double value * @deprecated Register replacement converters for Double.TYPE and * Double.class instead/*from ww w. j av a 2 s. c o m*/ */ public void setDefaultDouble(double newDefaultDouble) { defaultDouble = new Double(newDefaultDouble); register(new DoubleConverter(defaultDouble), Double.TYPE); register(new DoubleConverter(defaultDouble), Double.class); }
From source file:com.ricemap.spateDB.core.RTree.java
/** * Builds the RTree given a serialized list of elements. It uses the given * stockObject to deserialize these elements and build the tree. Also writes * the created tree to the disk directly. * /*from www . j av a 2 s .c o m*/ * @param elements * - serialization of elements to be written * @param offset * - index of the first element to use in the elements array * @param len * - number of bytes to user from the elements array * @param bytesAvailable * - size available (in bytes) to store the tree structures * @param dataOut * - an output to use for writing the tree to * @param fast_sort * - setting this to <code>true</code> allows the method to run * faster by materializing the offset of each element in the list * which speeds up the comparison. However, this requires an * additional 16 bytes per element. So, for each 1M elements, the * method will require an additional 16 M bytes (approximately). */ public void bulkLoadWrite(final byte[] element_bytes, final int offset, final int len, final int degree, DataOutput dataOut, final boolean fast_sort, final boolean columnarStorage) { try { columnar = columnarStorage; //TODO: the order of fields should be stable under Oracle JVM, but not guaranteed Field[] fields = stockObject.getClass().getDeclaredFields(); // Count number of elements in the given text int i_start = offset; final Text line = new Text(); while (i_start < offset + len) { int i_end = skipToEOL(element_bytes, i_start); // Extract the line without end of line character line.set(element_bytes, i_start, i_end - i_start - 1); stockObject.fromText(line); elementCount++; i_start = i_end; } LOG.info("Bulk loading an RTree with " + elementCount + " elements"); // It turns out the findBestDegree returns the best degree when the // whole // tree is loaded to memory when processed. However, as current // algorithms // process the tree while it's on disk, a higher degree should be // selected // such that a node fits one file block (assumed to be 4K). // final int degree = findBestDegree(bytesAvailable, elementCount); LOG.info("Writing an RTree with degree " + degree); int height = Math.max(1, (int) Math.ceil(Math.log(elementCount) / Math.log(degree))); int leafNodeCount = (int) Math.pow(degree, height - 1); if (elementCount < 2 * leafNodeCount && height > 1) { height--; leafNodeCount = (int) Math.pow(degree, height - 1); } int nodeCount = (int) ((Math.pow(degree, height) - 1) / (degree - 1)); int nonLeafNodeCount = nodeCount - leafNodeCount; // Keep track of the offset of each element in the text final int[] offsets = new int[elementCount]; final int[] ids = new int[elementCount]; final double[] ts = fast_sort ? new double[elementCount] : null; final double[] xs = fast_sort ? new double[elementCount] : null; final double[] ys = fast_sort ? new double[elementCount] : null; //initialize columnar data output ByteArrayOutputStream index_bos = new ByteArrayOutputStream(); DataOutputStream index_dos = new DataOutputStream(index_bos); ByteArrayOutputStream[] bos = new ByteArrayOutputStream[fields.length]; DataOutputStream[] dos = new DataOutputStream[fields.length]; for (int i = 0; i < bos.length; i++) { bos[i] = new ByteArrayOutputStream(); dos[i] = new DataOutputStream(bos[i]); } i_start = offset; line.clear(); for (int i = 0; i < elementCount; i++) { offsets[i] = i_start; ids[i] = i; int i_end = skipToEOL(element_bytes, i_start); if (xs != null) { // Extract the line with end of line character line.set(element_bytes, i_start, i_end - i_start - 1); stockObject.fromText(line); // Sample center of the shape ts[i] = (stockObject.getMBR().t1 + stockObject.getMBR().t2) / 2; xs[i] = (stockObject.getMBR().x1 + stockObject.getMBR().x2) / 2; ys[i] = (stockObject.getMBR().y1 + stockObject.getMBR().y2) / 2; //build columnar storage if (stockObject instanceof Point3d) { index_dos.writeDouble(ts[i]); index_dos.writeDouble(xs[i]); index_dos.writeDouble(ys[i]); } else { throw new RuntimeException("Indexing non-point shape with RTREE is not supported yet"); } for (int j = 0; j < fields.length; j++) { if (fields[j].getType().equals(Integer.TYPE)) { dos[j].writeInt(fields[j].getInt(stockObject)); } else if (fields[j].getType().equals(Double.TYPE)) { dos[j].writeDouble(fields[j].getDouble(stockObject)); } else if (fields[j].getType().equals(Long.TYPE)) { dos[j].writeLong(fields[j].getLong(stockObject)); } else { continue; //throw new RuntimeException("Field type is not supported yet"); } } } i_start = i_end; } index_dos.close(); for (int i = 0; i < dos.length; i++) { dos[i].close(); } /** A struct to store information about a split */ class SplitStruct extends Prism { /** Start and end index for this split */ int index1, index2; /** Direction of this split */ byte direction; /** Index of first element on disk */ int offsetOfFirstElement; static final byte DIRECTION_T = 0; static final byte DIRECTION_X = 1; static final byte DIRECTION_Y = 2; SplitStruct(int index1, int index2, byte direction) { this.index1 = index1; this.index2 = index2; this.direction = direction; } @Override public void write(DataOutput out) throws IOException { // if (columnarStorage) out.writeInt(index1); else out.writeInt(offsetOfFirstElement); super.write(out); } void partition(Queue<SplitStruct> toBePartitioned) { IndexedSortable sortableT; IndexedSortable sortableX; IndexedSortable sortableY; if (fast_sort) { // Use materialized xs[] and ys[] to do the comparisons sortableT = new IndexedSortable() { @Override public void swap(int i, int j) { // Swap ts double tempt = ts[i]; ts[i] = ts[j]; ts[j] = tempt; // Swap xs double tempx = xs[i]; xs[i] = xs[j]; xs[j] = tempx; // Swap ys double tempY = ys[i]; ys[i] = ys[j]; ys[j] = tempY; // Swap id int tempid = offsets[i]; offsets[i] = offsets[j]; offsets[j] = tempid; tempid = ids[i]; ids[i] = ids[j]; ids[j] = tempid; } @Override public int compare(int i, int j) { if (ts[i] < ts[j]) return -1; if (ts[i] > ts[j]) return 1; return 0; } }; sortableX = new IndexedSortable() { @Override public void swap(int i, int j) { // Swap ts double tempt = ts[i]; ts[i] = ts[j]; ts[j] = tempt; // Swap xs double tempx = xs[i]; xs[i] = xs[j]; xs[j] = tempx; // Swap ys double tempY = ys[i]; ys[i] = ys[j]; ys[j] = tempY; // Swap id int tempid = offsets[i]; offsets[i] = offsets[j]; offsets[j] = tempid; tempid = ids[i]; ids[i] = ids[j]; ids[j] = tempid; } @Override public int compare(int i, int j) { if (ts[i] < ts[j]) return -1; if (xs[i] < xs[j]) return -1; if (xs[i] > xs[j]) return 1; return 0; } }; sortableY = new IndexedSortable() { @Override public void swap(int i, int j) { // Swap ts double tempt = ts[i]; ts[i] = ts[j]; ts[j] = tempt; // Swap xs double tempx = xs[i]; xs[i] = xs[j]; xs[j] = tempx; // Swap ys double tempY = ys[i]; ys[i] = ys[j]; ys[j] = tempY; // Swap id int tempid = offsets[i]; offsets[i] = offsets[j]; offsets[j] = tempid; tempid = ids[i]; ids[i] = ids[j]; ids[j] = tempid; } @Override public int compare(int i, int j) { if (ys[i] < ys[j]) return -1; if (ys[i] > ys[j]) return 1; return 0; } }; } else { // No materialized xs and ys. Always deserialize objects // to compare sortableT = new IndexedSortable() { @Override public void swap(int i, int j) { // Swap id int tempid = offsets[i]; offsets[i] = offsets[j]; offsets[j] = tempid; tempid = ids[i]; ids[i] = ids[j]; ids[j] = tempid; } @Override public int compare(int i, int j) { // Get end of line int eol = skipToEOL(element_bytes, offsets[i]); line.set(element_bytes, offsets[i], eol - offsets[i] - 1); stockObject.fromText(line); double ti = (stockObject.getMBR().t1 + stockObject.getMBR().t2) / 2; eol = skipToEOL(element_bytes, offsets[j]); line.set(element_bytes, offsets[j], eol - offsets[j] - 1); stockObject.fromText(line); double tj = (stockObject.getMBR().t1 + stockObject.getMBR().t2) / 2; if (ti < tj) return -1; if (ti > tj) return 1; return 0; } }; sortableX = new IndexedSortable() { @Override public void swap(int i, int j) { // Swap id int tempid = offsets[i]; offsets[i] = offsets[j]; offsets[j] = tempid; tempid = ids[i]; ids[i] = ids[j]; ids[j] = tempid; } @Override public int compare(int i, int j) { // Get end of line int eol = skipToEOL(element_bytes, offsets[i]); line.set(element_bytes, offsets[i], eol - offsets[i] - 1); stockObject.fromText(line); double xi = (stockObject.getMBR().x1 + stockObject.getMBR().x2) / 2; eol = skipToEOL(element_bytes, offsets[j]); line.set(element_bytes, offsets[j], eol - offsets[j] - 1); stockObject.fromText(line); double xj = (stockObject.getMBR().x1 + stockObject.getMBR().x2) / 2; if (xi < xj) return -1; if (xi > xj) return 1; return 0; } }; sortableY = new IndexedSortable() { @Override public void swap(int i, int j) { // Swap id int tempid = offsets[i]; offsets[i] = offsets[j]; offsets[j] = tempid; tempid = ids[i]; ids[i] = ids[j]; ids[j] = tempid; } @Override public int compare(int i, int j) { int eol = skipToEOL(element_bytes, offsets[i]); line.set(element_bytes, offsets[i], eol - offsets[i] - 1); stockObject.fromText(line); double yi = (stockObject.getMBR().y1 + stockObject.getMBR().y2) / 2; eol = skipToEOL(element_bytes, offsets[j]); line.set(element_bytes, offsets[j], eol - offsets[j] - 1); stockObject.fromText(line); double yj = (stockObject.getMBR().y1 + stockObject.getMBR().y2) / 2; if (yi < yj) return -1; if (yi > yj) return 1; return 0; } }; } final IndexedSorter sorter = new QuickSort(); final IndexedSortable[] sortables = new IndexedSortable[3]; sortables[SplitStruct.DIRECTION_T] = sortableT; sortables[SplitStruct.DIRECTION_X] = sortableX; sortables[SplitStruct.DIRECTION_Y] = sortableY; sorter.sort(sortables[direction], index1, index2); // Partition into maxEntries partitions (equally) and // create a SplitStruct for each partition int i1 = index1; for (int iSplit = 0; iSplit < degree; iSplit++) { int i2 = index1 + (index2 - index1) * (iSplit + 1) / degree; SplitStruct newSplit; if (direction == 0) { newSplit = new SplitStruct(i1, i2, (byte) 1); } else if (direction == 1) { newSplit = new SplitStruct(i1, i2, (byte) 2); } else { newSplit = new SplitStruct(i1, i2, (byte) 0); } toBePartitioned.add(newSplit); i1 = i2; } } } // All nodes stored in level-order traversal Vector<SplitStruct> nodes = new Vector<SplitStruct>(); final Queue<SplitStruct> toBePartitioned = new LinkedList<SplitStruct>(); toBePartitioned.add(new SplitStruct(0, elementCount, SplitStruct.DIRECTION_X)); while (!toBePartitioned.isEmpty()) { SplitStruct split = toBePartitioned.poll(); if (nodes.size() < nonLeafNodeCount) { // This is a non-leaf split.partition(toBePartitioned); } nodes.add(split); } if (nodes.size() != nodeCount) { throw new RuntimeException( "Expected node count: " + nodeCount + ". Real node count: " + nodes.size()); } // Now we have our data sorted in the required order. Start building // the tree. // Store the offset of each leaf node in the tree FSDataOutputStream fakeOut = new FSDataOutputStream(new java.io.OutputStream() { // Null output stream @Override public void write(int b) throws IOException { // Do nothing } @Override public void write(byte[] b, int off, int len) throws IOException { // Do nothing } @Override public void write(byte[] b) throws IOException { // Do nothing } }, null, TreeHeaderSize + nodes.size() * NodeSize); for (int i_leaf = nonLeafNodeCount, i = 0; i_leaf < nodes.size(); i_leaf++) { nodes.elementAt(i_leaf).offsetOfFirstElement = (int) fakeOut.getPos(); if (i != nodes.elementAt(i_leaf).index1) throw new RuntimeException(); double t1, x1, y1, t2, x2, y2; // Initialize MBR to first object int eol = skipToEOL(element_bytes, offsets[i]); fakeOut.write(element_bytes, offsets[i], eol - offsets[i]); line.set(element_bytes, offsets[i], eol - offsets[i] - 1); stockObject.fromText(line); Prism mbr = stockObject.getMBR(); t1 = mbr.t1; x1 = mbr.x1; y1 = mbr.y1; t2 = mbr.t2; x2 = mbr.x2; y2 = mbr.y2; i++; while (i < nodes.elementAt(i_leaf).index2) { eol = skipToEOL(element_bytes, offsets[i]); fakeOut.write(element_bytes, offsets[i], eol - offsets[i]); line.set(element_bytes, offsets[i], eol - offsets[i] - 1); stockObject.fromText(line); mbr = stockObject.getMBR(); if (mbr.t1 < t1) t1 = mbr.t1; if (mbr.x1 < x1) x1 = mbr.x1; if (mbr.y1 < y1) y1 = mbr.y1; if (mbr.t2 > t2) t2 = mbr.t2; if (mbr.x2 > x2) x2 = mbr.x2; if (mbr.y2 > y2) y2 = mbr.y2; i++; } nodes.elementAt(i_leaf).set(t1, x1, y1, t2, x2, y2); } fakeOut.close(); fakeOut = null; // Calculate MBR and offsetOfFirstElement for non-leaves for (int i_node = nonLeafNodeCount - 1; i_node >= 0; i_node--) { int i_first_child = i_node * degree + 1; nodes.elementAt(i_node).offsetOfFirstElement = nodes.elementAt(i_first_child).offsetOfFirstElement; int i_child = 0; Prism mbr; mbr = nodes.elementAt(i_first_child + i_child); double t1 = mbr.t1; double x1 = mbr.x1; double y1 = mbr.y1; double t2 = mbr.t2; double x2 = mbr.x2; double y2 = mbr.y2; i_child++; while (i_child < degree) { mbr = nodes.elementAt(i_first_child + i_child); if (mbr.t1 < t1) t1 = mbr.t1; if (mbr.x1 < x1) x1 = mbr.x1; if (mbr.y1 < y1) y1 = mbr.y1; if (mbr.t2 > t2) t2 = mbr.t2; if (mbr.x2 > x2) x2 = mbr.x2; if (mbr.y2 > y2) y2 = mbr.y2; i_child++; } nodes.elementAt(i_node).set(t1, x1, y1, t2, x2, y2); } // Start writing the tree // write tree header (including size) // Total tree size. (== Total bytes written - 8 bytes for the size // itself) dataOut.writeInt(TreeHeaderSize + NodeSize * nodeCount + len); // Tree height dataOut.writeInt(height); // Degree dataOut.writeInt(degree); dataOut.writeInt(elementCount); //isColumnar dataOut.writeInt(columnarStorage ? 1 : 0); // write nodes for (SplitStruct node : nodes) { node.write(dataOut); } // write elements if (columnarStorage) { byte[] index_bs = index_bos.toByteArray(); byte[][] bss = new byte[bos.length][]; for (int i = 0; i < bss.length; i++) { bss[i] = bos[i].toByteArray(); } for (int element_i = 0; element_i < elementCount; element_i++) { //int eol = skipToEOL(element_bytes, offsets[element_i]); //dataOut.write(element_bytes, offsets[element_i], eol - offsets[element_i]); dataOut.write(index_bs, ids[element_i] * IndexUnitSize, IndexUnitSize); } for (int i = 0; i < fields.length; i++) { int fieldSize = 0; if (fields[i].getType().equals(Integer.TYPE)) { fieldSize = 4; } else if (fields[i].getType().equals(Long.TYPE)) { fieldSize = 8; } else if (fields[i].getType().equals(Double.TYPE)) { fieldSize = 8; } else { //throw new RuntimeException("Unsupported field type: " + fields[i].getType().getName()); continue; } for (int element_i = 0; element_i < elementCount; element_i++) { //int eol = skipToEOL(element_bytes, offsets[element_i]); //dataOut.write(element_bytes, offsets[element_i], eol - offsets[element_i]); dataOut.write(bss[i], ids[element_i] * fieldSize, fieldSize); } } } else { for (int element_i = 0; element_i < elementCount; element_i++) { int eol = skipToEOL(element_bytes, offsets[element_i]); dataOut.write(element_bytes, offsets[element_i], eol - offsets[element_i]); } } } catch (IOException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:ca.sqlpower.architect.swingui.TestPlayPenComponent.java
/** * Returns a new value that is not equal to oldVal. The * returned object will be a new instance compatible with oldVal. * //from ww w. ja va2 s. c o m * @param property The property that should be modified. * @param oldVal The existing value of the property to modify. The returned value * will not equal this one at the time this method was first called. */ private Object getNewDifferentValue(PropertyDescriptor property, Object oldVal) throws SQLObjectException { Object newVal; // don't init here so compiler can warn if the // following code doesn't always give it a value if (property.getPropertyType() == String.class) { newVal = "new " + oldVal; } else if (property.getPropertyType() == Boolean.class || property.getPropertyType() == Boolean.TYPE) { if (oldVal == null) { newVal = new Boolean(false); } else { newVal = new Boolean(!((Boolean) oldVal).booleanValue()); } } else if (property.getPropertyType() == Integer.class || property.getPropertyType() == Integer.TYPE) { if (oldVal == null) { newVal = new Integer(0); } else { newVal = new Integer(((Integer) oldVal).intValue() + 1); } } else if (property.getPropertyType() == Double.class || property.getPropertyType() == Double.TYPE) { if (oldVal == null) { newVal = new Double(0); } else { newVal = new Double(((Double) oldVal).doubleValue() + 1); } } else if (property.getPropertyType() == Color.class) { if (oldVal == null) { newVal = new Color(0xFAC157); } else { Color oldColor = (Color) oldVal; newVal = new Color((oldColor.getRGB() + 0xF00) % 0x1000000); } } else if (property.getPropertyType() == Font.class) { if (oldVal == null) { newVal = FontManager.getDefaultPhysicalFont(); } else { Font oldFont = (Font) oldVal; newVal = new Font(oldFont.getFontName(), oldFont.getSize() + 2, oldFont.getStyle()); } } else if (property.getPropertyType() == Point.class) { if (oldVal == null) { newVal = new Point(); } else { Point oldPoint = (Point) oldVal; newVal = new Point(oldPoint.x + 10, oldPoint.y + 10); } } else if (property.getPropertyType() == Insets.class) { if (oldVal == null) { newVal = new Insets(0, 0, 0, 0); } else { Insets oldInsets = (Insets) oldVal; newVal = new Insets(oldInsets.top + 10, oldInsets.left + 10, oldInsets.bottom + 10, oldInsets.right + 10); } } else if (property.getPropertyType() == Set.class) { newVal = new HashSet(); if (property.getName().equals("hiddenColumns")) { ((Set) newVal).add(new SQLColumn()); } else { ((Set) newVal).add("Test"); } } else if (property.getPropertyType() == List.class) { newVal = new ArrayList(); if (property.getName().equals("selectedColumns")) { ((List) newVal).add(new SQLColumn()); } else { ((List) newVal).add("Test"); } } else if (property.getPropertyType() == TablePane.class) { SQLTable t = new SQLTable(); t.initFolders(true); newVal = new TablePane(t, pp.getContentPane()); } else if (property.getPropertyType() == SQLTable.class) { newVal = new SQLTable(); ((SQLTable) newVal).initFolders(true); } else if (property.getPropertyType() == Dimension.class) { newVal = new Dimension(); if (oldVal != null) { ((Dimension) newVal).width = ((Dimension) oldVal).width + 1; } } else { throw new RuntimeException("This test case lacks a value for " + property.getName() + " (type " + property.getPropertyType().getName() + ") in getNewDifferentValue()"); } return newVal; }
From source file:com.icesoft.faces.renderkit.dom_html_basic.MenuRenderer.java
protected Object convertArray(FacesContext facesContext, UISelectMany uiSelectMany, Class componentType, String[] newSubmittedValues) throws ConverterException { // component type of String means no conversion is necessary if (componentType.equals(String.class)) { return newSubmittedValues; }/*from w w w. j av a2 s. c om*/ // if newSubmittedValue is null return zero-length array if (newSubmittedValues == null) { return Array.newInstance(componentType, 0); } // create the array with specified component length int numberOfValues = newSubmittedValues.length; Object convertedValues = Array.newInstance(componentType, numberOfValues); // Determine if a converter is explicitly registered with the component Converter converter = uiSelectMany.getConverter(); if (converter == null) { // Determine if there is a default converter for the class converter = getConverterForClass(componentType); } if (converter == null) { // we don't need to convert base Object types if (componentType.equals(Object.class)) { return newSubmittedValues; } else { throw new ConverterException("Converter is null"); } } for (int index = 0; index < numberOfValues; index++) { // convert the next element Object nextConvertedElement = converter.getAsObject(facesContext, uiSelectMany, newSubmittedValues[index]); if (!componentType.isPrimitive()) { Array.set(convertedValues, index, nextConvertedElement); } else if (componentType.equals(Boolean.TYPE)) { Array.setBoolean(convertedValues, index, ((Boolean) nextConvertedElement).booleanValue()); } else if (componentType.equals(Integer.TYPE)) { Array.setInt(convertedValues, index, ((Integer) nextConvertedElement).intValue()); } else if (componentType.equals(Long.TYPE)) { Array.setLong(convertedValues, index, ((Long) nextConvertedElement).longValue()); } else if (componentType.equals(Short.TYPE)) { Array.setShort(convertedValues, index, ((Short) nextConvertedElement).shortValue()); } else if (componentType.equals(Byte.TYPE)) { Array.setByte(convertedValues, index, ((Byte) nextConvertedElement).byteValue()); } else if (componentType.equals(Float.TYPE)) { Array.setFloat(convertedValues, index, ((Float) nextConvertedElement).floatValue()); } else if (componentType.equals(Double.TYPE)) { Array.setDouble(convertedValues, index, ((Double) nextConvertedElement).doubleValue()); } else if (componentType.equals(Character.TYPE)) { Array.setChar(convertedValues, index, ((Character) nextConvertedElement).charValue()); } } return convertedValues; }
From source file:com.sun.faces.renderkit.html_basic.MenuRenderer.java
protected Object handleArrayCase(FacesContext context, UISelectMany uiSelectMany, Class arrayClass, String[] newValues) throws ConverterException { Object result = null;// w w w . ja va 2 s .c o m Class elementType = null; Converter converter = null; int i = 0, len = (null != newValues ? newValues.length : 0); elementType = arrayClass.getComponentType(); // Optimization: If the elementType is String, we don't need // conversion. Just return newValues. if (elementType.equals(String.class)) { return newValues; } try { result = Array.newInstance(elementType, len); } catch (Exception e) { throw new ConverterException(e); } // bail out now if we have no new values, returning our // oh-so-useful zero-length array. if (null == newValues) { return result; } // obtain a converter. // attached converter takes priority if (null == (converter = uiSelectMany.getConverter())) { // Otherwise, look for a by-type converter if (null == (converter = Util.getConverterForClass(elementType, context))) { // if that fails, and the attached values are of Object type, // we don't need conversion. if (elementType.equals(Object.class)) { return newValues; } String valueStr = ""; for (i = 0; i < newValues.length; i++) { valueStr = valueStr + " " + newValues[i]; } Object[] params = { valueStr, "null Converter" }; throw new ConverterException(Util.getExceptionMessage(Util.CONVERSION_ERROR_MESSAGE_ID, params)); } } Util.doAssert(null != result); if (elementType.isPrimitive()) { for (i = 0; i < len; i++) { if (elementType.equals(Boolean.TYPE)) { Array.setBoolean(result, i, ((Boolean) converter.getAsObject(context, uiSelectMany, newValues[i])).booleanValue()); } else if (elementType.equals(Byte.TYPE)) { Array.setByte(result, i, ((Byte) converter.getAsObject(context, uiSelectMany, newValues[i])).byteValue()); } else if (elementType.equals(Double.TYPE)) { Array.setDouble(result, i, ((Double) converter.getAsObject(context, uiSelectMany, newValues[i])).doubleValue()); } else if (elementType.equals(Float.TYPE)) { Array.setFloat(result, i, ((Float) converter.getAsObject(context, uiSelectMany, newValues[i])).floatValue()); } else if (elementType.equals(Integer.TYPE)) { Array.setInt(result, i, ((Integer) converter.getAsObject(context, uiSelectMany, newValues[i])).intValue()); } else if (elementType.equals(Character.TYPE)) { Array.setChar(result, i, ((Character) converter.getAsObject(context, uiSelectMany, newValues[i])).charValue()); } else if (elementType.equals(Short.TYPE)) { Array.setShort(result, i, ((Short) converter.getAsObject(context, uiSelectMany, newValues[i])).shortValue()); } else if (elementType.equals(Long.TYPE)) { Array.setLong(result, i, ((Long) converter.getAsObject(context, uiSelectMany, newValues[i])).longValue()); } } } else { for (i = 0; i < len; i++) { if (log.isDebugEnabled()) { Object converted = converter.getAsObject(context, uiSelectMany, newValues[i]); log.debug("String value: " + newValues[i] + " converts to : " + converted.toString()); } Array.set(result, i, converter.getAsObject(context, uiSelectMany, newValues[i])); } } return result; }
From source file:com.exadel.flamingo.flex.messaging.amf.io.AMF0Serializer.java
protected Object[] convertPrimitiveArrayToObjectArray(Object array) { Class<?> componentType = array.getClass().getComponentType(); Object[] result = null;//from ww w . j a v a 2 s . c o m if (componentType == null) { throw new NullPointerException("componentType is null"); } else if (componentType == Character.TYPE) { char[] carray = (char[]) array; result = new Object[carray.length]; for (int i = 0; i < carray.length; i++) { result[i] = new Character(carray[i]); } } else if (componentType == Byte.TYPE) { byte[] barray = (byte[]) array; result = new Object[barray.length]; for (int i = 0; i < barray.length; i++) { result[i] = new Byte(barray[i]); } } else if (componentType == Short.TYPE) { short[] sarray = (short[]) array; result = new Object[sarray.length]; for (int i = 0; i < sarray.length; i++) { result[i] = new Short(sarray[i]); } } else if (componentType == Integer.TYPE) { int[] iarray = (int[]) array; result = new Object[iarray.length]; for (int i = 0; i < iarray.length; i++) { result[i] = Integer.valueOf(iarray[i]); } } else if (componentType == Long.TYPE) { long[] larray = (long[]) array; result = new Object[larray.length]; for (int i = 0; i < larray.length; i++) { result[i] = new Long(larray[i]); } } else if (componentType == Double.TYPE) { double[] darray = (double[]) array; result = new Object[darray.length]; for (int i = 0; i < darray.length; i++) { result[i] = new Double(darray[i]); } } else if (componentType == Float.TYPE) { float[] farray = (float[]) array; result = new Object[farray.length]; for (int i = 0; i < farray.length; i++) { result[i] = new Float(farray[i]); } } else if (componentType == Boolean.TYPE) { boolean[] barray = (boolean[]) array; result = new Object[barray.length]; for (int i = 0; i < barray.length; i++) { result[i] = new Boolean(barray[i]); } } else { throw new IllegalArgumentException("unexpected component type: " + componentType.getClass().getName()); } return result; }
From source file:Classes.java
/** * This method acts equivalently to invoking classLoader.loadClass(className) * but it also supports primitive types and array classes of object types or * primitive types.// w w w . j av a2s. com * * @param className * the qualified name of the class or the name of primitive type or * array in the same format as returned by the * java.lang.Class.getName() method. * @param classLoader * the ClassLoader used to load classes * @return the Class object for the requested className * * @throws ClassNotFoundException * when the <code>classLoader</code> can not find the requested * class */ public static Class loadClass(String className, ClassLoader classLoader) throws ClassNotFoundException { // ClassLoader.loadClass() does not handle primitive types: // // B byte // C char // D double // F float // I int // J long // S short // Z boolean // V void // if (className.length() == 1) { char type = className.charAt(0); if (type == 'B') return Byte.TYPE; if (type == 'C') return Character.TYPE; if (type == 'D') return Double.TYPE; if (type == 'F') return Float.TYPE; if (type == 'I') return Integer.TYPE; if (type == 'J') return Long.TYPE; if (type == 'S') return Short.TYPE; if (type == 'Z') return Boolean.TYPE; if (type == 'V') return Void.TYPE; // else throw... throw new ClassNotFoundException(className); } // Check for a primative type if (isPrimitive(className) == true) return (Class) Classes.PRIMITIVE_NAME_TYPE_MAP.get(className); // Check for the internal vm format: Lclassname; if (className.charAt(0) == 'L' && className.charAt(className.length() - 1) == ';') return classLoader.loadClass(className.substring(1, className.length() - 1)); // first try - be optimistic // this will succeed for all non-array classes and array classes that have // already been resolved // try { return classLoader.loadClass(className); } catch (ClassNotFoundException e) { // if it was non-array class then throw it if (className.charAt(0) != '[') throw e; } // we are now resolving array class for the first time // count opening braces int arrayDimension = 0; while (className.charAt(arrayDimension) == '[') arrayDimension++; // resolve component type - use recursion so that we can resolve primitive // types also Class componentType = loadClass(className.substring(arrayDimension), classLoader); // construct array class return Array.newInstance(componentType, new int[arrayDimension]).getClass(); }
From source file:net.sf.qooxdoo.rpc.RemoteCallUtils.java
/** * Converts "normal" java types to JSON stuff. * * @param obj the object to convert. */// w w w .j a va2 s . c o m public Object fromJava(Object obj) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { if (obj == null) { return JSONObject.NULL; } if (obj instanceof String) { return obj; } if (obj instanceof Date) { return obj; } if (obj instanceof Integer || obj instanceof Double || obj instanceof Boolean) { return obj; } if (obj instanceof Float) { return new Double(((Float) obj).doubleValue()); } // FIXME: find a better way to handle longs if (obj instanceof Long) { return new Double(((Long) obj).doubleValue()); } if (obj instanceof Object[]) { Object[] objectArray = (Object[]) obj; JSONArray jsonArray = new JSONArray(); for (int i = 0; i < objectArray.length; ++i) { jsonArray.put(fromJava(objectArray[i])); } return jsonArray; } Class componentType = obj.getClass().getComponentType(); if (componentType == Integer.TYPE) { JSONArray jsonArray = new JSONArray(); int[] intArray = (int[]) obj; for (int i = 0; i < intArray.length; ++i) { jsonArray.put(intArray[i]); } return jsonArray; } if (componentType == Float.TYPE) { JSONArray jsonArray = new JSONArray(); float[] floatArray = (float[]) obj; for (int i = 0; i < floatArray.length; ++i) { jsonArray.put((double) (floatArray[i])); } return jsonArray; } // FIXME: find a better way to handle longs if (componentType == Long.TYPE) { JSONArray jsonArray = new JSONArray(); long[] longArray = (long[]) obj; for (int i = 0; i < longArray.length; ++i) { jsonArray.put((double) (longArray[i])); } return jsonArray; } if (componentType == Double.TYPE) { JSONArray jsonArray = new JSONArray(); double[] doubleArray = (double[]) obj; for (int i = 0; i < doubleArray.length; ++i) { jsonArray.put(doubleArray[i]); } return jsonArray; } if (componentType == Boolean.TYPE) { JSONArray jsonArray = new JSONArray(); boolean[] booleanArray = (boolean[]) obj; for (int i = 0; i < booleanArray.length; ++i) { jsonArray.put(booleanArray[i]); } return jsonArray; } if (obj instanceof Map) { Map map = (Map) obj; Iterator keyIterator = map.keySet().iterator(); JSONObject jsonObject = new JSONObject(); Object key; while (keyIterator.hasNext()) { key = keyIterator.next(); jsonObject.put((key == null ? null : key.toString()), fromJava(map.get(key))); } return jsonObject; } if (obj instanceof Set) { Set set = (Set) obj; Iterator iterator = set.iterator(); JSONObject jsonObject = new JSONObject(); Object key; while (iterator.hasNext()) { key = iterator.next(); jsonObject.put((key == null ? null : key.toString()), true); } return jsonObject; } return fromJava(filter(obj, PropertyUtils.describe(obj))); }