Advertisement

04.16.2008 at 05:20PM PDT, ID: 23329379
[x]
Attachment Details

Java generics

Asked by corey_mcm in Java Programming Language

Tags: Java

I have a java class which is used to create a list of points from a BLOB in a MS SQL database. This is then used by a charting utility to generate a graph.

Two different methods are used, one for a 2D set of data to plot, and another for multiple 2D data sets where more than one line is graphed. The first method seems to work ok, but the second doesn't

Code is listed below.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
public float[][] read2D(byte[] byteArray, float scaling)
        throws IOException
    {
        int				  structSize = 8;   // 2 floats
        int				  pointCount = byteArray.length / structSize;
        float[][]		  floatArray = new float[pointCount][2];
        LEDataInputStream dis		 = new LEDataInputStream(new ByteArrayInputStream(byteArray));
        
        for (int i = 0; i < pointCount; i++)
        {
            Float xValueReadIn = dis.readFloat();
            Float yValueReadIn = dis.readFloat();
//
// Check the x value
//
            if (xValueReadIn.isNaN())
            {
                floatArray[i][0] = 0;   
            }	 
            else
            {
                floatArray[i][0] = xValueReadIn.floatValue();   
            }
//
// Check the y value
//
            if (yValueReadIn.isNaN())
            {
                floatArray[i][1] = 0;   
            }	 
            else
            {
                floatArray[i][1] = yValueReadIn.floatValue();   
            }
        }
 
        return floatArray;
    }
 
/**
 *
 * readDouble2D(byte[] byteArray)
 *
 * Two 2-dimensional trace point arrays is an array of structures.
 * x1,y1a,y1b,
 * x2,y2a,y2b,
 * ...
 * xn,yna,ynb
 *
 */
    public Vector<Float>[] readDouble2D(byte[] byteArray)
        throws IOException
    {
        return readDouble2D(byteArray, 1);
    }
 
    public Vector<Float>[] readDouble2D(byte[] byteArray, float scaling)
        throws IOException
    {
        int				    structSize  = 12;   // 3 floats
        int				    pointCount  = byteArray.length / structSize;
        List<Vector<Float>> vectorArray = new ArrayList<Vector<Float>>(pointCount);
        LEDataInputStream   dis		  = new LEDataInputStream(new ByteArrayInputStream(byteArray));
        
        for (int i = 0; i < pointCount; i++)
        {
            Vector<Float> values = new Vector<Float>(3);
//
// No scaling for x value
//
 
            values.add(new Float(dis.readFloat() * scaling));
            
            for (int j = 0; j < 2; j++)
            {
                values.add(new Float(dis.readFloat() * scaling));
            }
            
            vectorArray.add(values);
        }
        
        return (Vector<Float>[])vectorArray.toArray();
    }
 
/*
 * ---------------------------
 * Here is the misbehaving class
 */
public Map<String, Float>[] read2DMultiPoint(byte[] byteArray, int points, String[] hashNames, float scaling)
        throws IOException
    {
        int                                              structSize = 4 * points;
        int                                              pointCount = byteArray.length / structSize;
        List<Map<String, Float>> mapArray   = new ArrayList<Map<String, Float>>(pointCount);
        LEDataInputStream                dis            = new LEDataInputStream(new ByteArrayInputStream(byteArray));
        
        for (int i = 0; i < pointCount; i++)
        {
            HashMap<String, Float> pointMap = new HashMap<String, Float>(points);
//
// No scaling for x value
//
            pointMap.put(hashNames[0], new Float(dis.readFloat()));
            
            for (int j = 1; j < points; j++)
            {
                pointMap.put(hashNames[j], new Float(dis.readFloat() * scaling));
            }
            
            mapArray.add(pointMap);
        }
            
        return (Map<String, Float>[])mapArray.toArray();
    }
[+][-]04.16.2008 at 11:22PM PDT, ID: 21374592

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.16.2008 at 11:38PM PDT, ID: 21374669

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.16.2008 at 11:39PM PDT, ID: 21374673

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.16.2008 at 11:41PM PDT, ID: 21374682

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.16.2008 at 11:49PM PDT, ID: 21374704

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.16.2008 at 11:51PM PDT, ID: 21374717

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.17.2008 at 12:04AM PDT, ID: 21374772

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Java Programming Language
Tags: Java
Sign Up Now!
Solution Provided By: Bart_Cr
Participating Experts: 3
Solution Grade: A
 
 
[+][-]04.17.2008 at 01:18AM PDT, ID: 21375099

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628