Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: joelib/util/LineMatrixHelper.java


1   ///////////////////////////////////////////////////////////////////////////////
2   //  Filename: $RCSfile: LineMatrixHelper.java,v $
3   //  Purpose:  Atom representation.
4   //  Language: Java
5   //  Compiler: JDK 1.4
6   //  Authors:  Jan Bruecker
7   //  Version:  $Revision: 1.14 $
8   //            $Date: 2003/08/22 15:56:21 $
9   //            $Author: wegner $
10  //
11  //  Copyright (c) Dept. Computer Architecture, University of Tuebingen, Germany
12  //
13  //  This program is free software; you can redistribute it and/or modify
14  //  it under the terms of the GNU General Public License as published by
15  //  the Free Software Foundation version 2 of the License.
16  //
17  //  This program is distributed in the hope that it will be useful,
18  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  //  GNU General Public License for more details.
21  ///////////////////////////////////////////////////////////////////////////////
22  package joelib.util;
23  
24  import wsi.ra.text.DecimalFormatter;
25  
26  /*==========================================================================*
27   * IMPORTS
28   *==========================================================================      */
29  import java.io.LineNumberReader;
30  import java.io.StringReader;
31  
32  import java.util.StringTokenizer;
33  
34  import org.apache.log4j.Category;
35  
36  
37  /*==========================================================================*
38   * CLASS DECLARATION
39   *==========================================================================      */
40  
41  /**
42   * Helper methods for writing and loading line matrices.
43   *
44   * @author     wegnerj
45   * @license GPL
46   * @cvsversion    $Revision: 1.14 $, $Date: 2003/08/22 15:56:21 $
47   */
48  public class LineMatrixHelper
49  {
50      //~ Static fields/initializers /////////////////////////////////////////////
51  
52      /*-------------------------------------------------------------------------*
53       * public static member variables
54       *-------------------------------------------------------------------------      */
55      /*-------------------------------------------------------------------------*
56       * private static member variables
57       *-------------------------------------------------------------------------      */
58  
59      // Obtain a suitable logger.
60      private static Category logger = Category.getInstance(
61              "joelib.util.LineMatrixHelper");
62      private static LineMatrixHelper lineMatrixHelper;
63  
64      //~ Constructors ///////////////////////////////////////////////////////////
65  
66      /*-------------------------------------------------------------------------*
67       * private member variables
68       *-------------------------------------------------------------------------      */
69      /*-------------------------------------------------------------------------*
70       * constructor
71       *-------------------------------------------------------------------------      */
72  
73      /**
74       *  Constructor for the JOERandom object
75       */
76      private LineMatrixHelper()
77      {
78      }
79  
80      //~ Methods ////////////////////////////////////////////////////////////////
81  
82      /**
83       *  Description of the Method
84       *
85       * @param  sMatrix    Description of the Parameter
86       * @return            Description of the Return Value
87       */
88      public static double[][] doubleMatrixFromString(String sMatrix)
89      {
90          int columns;
91          int lines;
92          String matrix = sMatrix;
93          StringReader sr = new StringReader(matrix);
94          LineNumberReader lnr = new LineNumberReader(sr);
95  
96          try
97          {
98              String line = lnr.readLine();
99              StringTokenizer matrixSize = new StringTokenizer(line, " ");
100 
101             if ((line != null) && (line.trim().length() != 0))
102             {
103                 lines = Integer.valueOf(matrixSize.nextToken()).intValue();
104                 columns = Integer.valueOf(matrixSize.nextToken()).intValue();
105             }
106             else
107             {
108                 return null;
109             }
110 
111             double[][] doubleMatrix = new double[lines][columns];
112 
113             for (int i = 0; i < lines; i++)
114             {
115                 for (int j = 0; j < columns; j++)
116                 {
117                     line = lnr.readLine();
118                     doubleMatrix[i][j] = Double.valueOf(line).doubleValue();
119                 }
120             }
121 
122             return doubleMatrix;
123         }
124          catch (Exception ex)
125         {
126             ex.printStackTrace();
127 
128             return null;
129         }
130     }
131 
132     /*-------------------------------------------------------------------------*
133      * public methods
134      *-------------------------------------------------------------------------      */
135 
136     /**
137      *  Description of the Method
138      *
139      * @return    Description of the Return Value
140      */
141     public static synchronized LineMatrixHelper instance()
142     {
143         if (lineMatrixHelper == null)
144         {
145             if (logger.isDebugEnabled())
146             {
147                 logger.debug("Getting " + LineMatrixHelper.class.getName() +
148                     " instance.");
149             }
150 
151             lineMatrixHelper = new LineMatrixHelper();
152         }
153 
154         return lineMatrixHelper;
155     }
156 
157     /**
158      *  Description of the Method
159      *
160      * @param  sMatrix    Description of the Parameter
161      * @return            Description of the Return Value
162      */
163     public static int[][] intMatrixFromString(String sMatrix)
164     {
165         int columns;
166         int lines;
167         String matrix = sMatrix;
168         StringReader sr = new StringReader(matrix);
169         LineNumberReader lnr = new LineNumberReader(sr);
170 
171         try
172         {
173             String line = lnr.readLine();
174             StringTokenizer matrixSize = new StringTokenizer(line, " ");
175 
176             if ((line != null) && (line.trim().length() != 0))
177             {
178                 lines = Integer.valueOf(matrixSize.nextToken()).intValue();
179                 columns = Integer.valueOf(matrixSize.nextToken()).intValue();
180             }
181             else
182             {
183                 return null;
184             }
185 
186             int[][] intMatrix = new int[lines][columns];
187 
188             for (int i = 0; i < lines; i++)
189             {
190                 for (int j = 0; j < columns; j++)
191                 {
192                     line = lnr.readLine();
193 
194                     //try{
195                     intMatrix[i][j] = Integer.valueOf(line).intValue();
196 
197                     //}
198                     //catch(NumberFormatException nfe)
199                     //{
200                     //  logger.error(nfe.toString());
201                     //  return null;
202                     //}
203                 }
204             }
205 
206             return intMatrix;
207         }
208          catch (Exception ex)
209         {
210             ex.printStackTrace();
211 
212             return null;
213         }
214     }
215 
216     /**
217      *  Description of the Method
218      *
219      * @param  sb         Description of the Parameter
220      * @param  matrix     Description of the Parameter
221      * @return            Description of the Return Value
222      */
223     public static StringBuffer toString(StringBuffer sb, int[][] matrix)
224     {
225         if ((matrix == null) || (matrix.length == 0))
226         {
227             return sb;
228         }
229 
230         //System.out.println("\n\nLength: "+matrix.length);
231         sb.append(matrix.length);
232         sb.append(" ");
233         sb.append(matrix[0].length);
234         sb.append("\n");
235 
236         for (int i = 0; i < matrix.length; i++)
237         {
238             for (int j = 0; j < matrix[0].length; j++)
239             {
240                 sb.append(matrix[i][j]);
241                 sb.append("\n");
242             }
243         }
244 
245         return sb;
246     }
247 
248     /**
249      *  Description of the Method
250      *
251      * @param  sb         Description of the Parameter
252      * @param  matrix     Description of the Parameter
253      * @return            Description of the Return Value
254      */
255     public static StringBuffer toString(StringBuffer sb, double[][] matrix)
256     {
257         return toString(sb, matrix, null);
258     }
259 
260     /**
261      *  Description of the Method
262      *
263      * @param  sb         Description of the Parameter
264      * @param  matrix     Description of the Parameter
265      * @return            Description of the Return Value
266      */
267     public static StringBuffer toString(StringBuffer sb, double[][] matrix,
268         DecimalFormatter format)
269     {
270         if ((matrix == null) || (matrix.length == 0))
271         {
272             return sb;
273         }
274 
275         sb.append(matrix.length);
276         sb.append(" ");
277         sb.append(matrix[0].length);
278         sb.append("\n");
279 
280         for (int i = 0; i < matrix.length; i++)
281         {
282             for (int j = 0; j < matrix[0].length; j++)
283             {
284                 if (format == null)
285                 {
286                     sb.append(matrix[i][j]);
287                 }
288                 else
289                 {
290                     sb.append(format.format(matrix[i][j]));
291                 }
292 
293                 sb.append("\n");
294             }
295         }
296 
297         return sb;
298     }
299 }
300 ///////////////////////////////////////////////////////////////////////////////
301 //  END OF FILE.
302 ///////////////////////////////////////////////////////////////////////////////