source: branches/ralph/src/main/java/de/ugoe/cs/autoquest/tasktrees/alignment/pal/io/FormattedOutput.java @ 1573

Last change on this file since 1573 was 1573, checked in by rkrimmel, 10 years ago

Now really adding PAL Library

File size: 6.6 KB
Line 
1// FormattedOutput.java
2//
3// (c) 1999-2001 PAL Development Core Team
4//
5// This package may be distributed under the
6// terms of the Lesser GNU General Public License (LGPL)
7
8package de.ugoe.cs.autoquest.tasktrees.alignment.pal.io;
9
10import java.io.*;
11import java.text.*;
12import java.util.*;
13import java.math.*;
14
15/**
16 * tools to simplify formatted output to a stream
17 *
18 * @version $Id: FormattedOutput.java,v 1.14 2001/07/13 14:39:13 korbinian Exp $
19 *
20 * @author Korbinian Strimmer
21 * @author Alexei Drummond
22 */
23public class FormattedOutput implements Serializable
24{
25        //
26        // Public stuff
27        //
28
29        /**
30         * create instance of this class
31         * (note that there is no public constructor
32         * as this class is a singleton)
33         */
34        public synchronized static FormattedOutput getInstance()
35        {
36                if (singleton == null)
37                {
38                        singleton = new FormattedOutput();
39                }
40
41                return singleton;
42        }
43
44        /**
45         * print decimal number with a prespecified number
46         * of digits after the point
47         *
48         * @param out output stream
49         * @param number to be printed
50         * @param width number of fraction digits
51         *
52         * @return length of the string printed
53         */
54        public int displayDecimal(PrintWriter out, double number, int width)
55        {
56                String s = getDecimalString(number, width);
57
58                out.print(s);
59
60                return s.length();
61        }
62
63        /**
64         * Returns a decimal string representation of a number with
65         * constrained width.
66         */
67        public synchronized String getDecimalString(double number, int width) {
68                nf.setMinimumFractionDigits(width);
69                nf.setMaximumFractionDigits(width);
70
71                return nf.format(number);
72        }
73
74        private static final double round(double number, int sf) {
75                double decimals = Math.floor(Math.log(number) / Math.log(10.0));
76                double power =  Math.pow(10, decimals - sf+1);
77                number /= power;
78                number = Math.round(number);
79                number *= power;
80                return number;
81        }
82
83        //public String getSFString(double number, int sf) {
84        //      double decimals = Math.floor(Math.log(number) / Math.log(10.0));
85        //      double power =  Math.pow(10, decimals - sf);
86        //      number /= power;
87        //      number = Math.round(number);
88        //      number *= power;
89        //
90        //      return "" + number;
91        //}
92               
93        /** Used by getSFString3() */
94        private SFStringInfo getSFStringInfo(String buffer,int sf, boolean includeRightZeros, boolean previousCarry) {
95                char[] b2 = new char[(includeRightZeros ? buffer.length() : Math.min(sf,buffer.length()))];
96                boolean carry = (sf>=buffer.length() ? previousCarry : buffer.charAt(sf) >= '5');
97                for(int i = b2.length-1 ; i >=0; i--) {
98                        if(i<sf) {
99                                b2[i] = buffer.charAt(i);
100                                if(carry) {
101                                        if(b2[i] == '9') {
102                                                b2[i] = '0';
103                                        } else {
104                                                b2[i]++;
105                carry = false;
106                                        }
107                                }
108                        } else {
109                                carry = buffer.charAt(i)>='5';
110                                b2[i] = '0';
111                        }
112                }
113                return new SFStringInfo(new String(b2), carry);
114
115        }
116
117        /**
118         * An alternative version of getSFString which works on the actual string
119         * Returns a string representing the given number to the number
120         * of significant figures requested.
121         */
122         public String getSFString(double number, int sf) {
123                String s = number+"";
124                String preFullStop = null;
125                String postFullStop = null;
126                String postE = null;
127
128                int fullStopIndex = s.indexOf('.');
129                int eIndex = s.indexOf('e');
130                if(eIndex<0) {
131                        eIndex = s.indexOf('E');
132                }
133
134                if(eIndex>=0) {
135                        postE = s.substring(eIndex+1);
136                }
137                eIndex = s.length();
138                if(fullStopIndex>=0) {
139                        postFullStop = s.substring(fullStopIndex+1,eIndex);
140
141                } else {
142                        fullStopIndex = eIndex;
143                }
144
145                preFullStop = s.substring(0,fullStopIndex);
146
147                SFStringInfo postFSI = null;
148
149
150                boolean postCarry = false;
151                if(postFullStop!=null) {
152                        int postSF = sf - preFullStop.length();
153                        if(postSF>0) {
154                                postFSI =   getSFStringInfo(postFullStop, postSF, false,false);
155                                postCarry = postFSI.carry;
156                        } else {
157
158                                postCarry = postFullStop.charAt(0)>='5';
159                        }
160
161                }
162                SFStringInfo preFSI;
163                preFSI = getSFStringInfo(preFullStop,sf,true,postCarry);
164                if(preFSI.carry) {
165                        preFSI.string = "1"+preFSI.string;
166                }
167                String b;
168                if(postFSI!=null) {
169                        b = preFSI.string+"."+postFSI.string;
170                } else {
171                        b = preFSI.string;
172                }
173                if(postE != null) {
174                        b+='e'+postE;
175                }
176
177                while (b.charAt(b.length() - 1) == '0') {
178                        b = b.substring(0, b.length()-1);
179                }
180                if (b.charAt(b.length() - 1) == '.') {
181                        b = b.substring(0, b.length()-1);
182                }
183
184                return b;
185        }
186        /**
187         * print label with a prespecified length
188         * (label will be shortened or spaces will introduced, if necessary)
189         *
190         * @param out output stream
191         * @param label label to be printed
192         * @param width desired length
193         */
194        public void displayLabel(PrintWriter out, String label, int width)
195        {
196                int len = label.length();
197
198                if (len == width)
199                {
200                        // Print as is
201                        out.print(label);
202                }
203                else if (len < width)
204                {
205                        // fill rest with spaces
206                        out.print(label);
207                        multiplePrint(out, ' ', width - len);
208                }
209                else
210                {
211                        // Print first width characters
212                        for (int i = 0; i < width; i++)
213                        {
214                                out.print(label.charAt(i));
215                        }                       
216                }               
217        }
218       
219        /**
220         * print integer, aligned to a reference number,
221         * (introducing space at the left side)
222         *
223         * @param out output stream
224         * @param num number to be printed
225         * @param maxNum reference number
226         */
227        public void displayInteger(PrintWriter out, int num, int maxNum)
228        {
229                int lenNum = Integer.toString(num).length();
230                int lenMaxNum = Integer.toString(maxNum).length();
231               
232                if (lenNum < lenMaxNum)
233                {
234                        multiplePrint(out, ' ', lenMaxNum - lenNum);
235                }
236                out.print(num);
237        }
238
239        /**
240         * print whitespace of length of a string displaying a given integer
241         *
242         * @param output stream
243         * @param maxNum number
244         */
245        public void displayIntegerWhite(PrintWriter out, int maxNum)
246        {
247                int lenMaxNum = Integer.toString(maxNum).length();
248               
249                multiplePrint(out, ' ', lenMaxNum);
250        }
251
252        /**
253         * repeatedly print a character
254         *
255         * @param out output stream
256         * @param c   character
257         * @param num number of repeats
258         */
259        public void multiplePrint(PrintWriter out, char c, int num)
260        {
261                for (int i = 0; i < num; i++)
262                {
263                        out.print(c);
264                }
265        }
266
267        /**
268         * returns of string of a given length of a single character.
269         *
270         * @param size length of the string required
271         * @param c   character
272         */
273        public static String space(int size, char c) {
274                StringBuffer sb = new StringBuffer();
275                for (int i = 0; i < size; i++) {
276                        sb.append(c);
277                }               
278                return new String(sb);
279        }
280
281
282        //
283        // Private stuff
284        //
285       
286        // private constructor
287        private FormattedOutput()
288        {
289                nf = NumberFormat.getInstance(Locale.UK);
290                nf.setGroupingUsed(false);
291        }
292       
293        private static FormattedOutput singleton = null;
294       
295        private NumberFormat nf;
296}
297
298/**
299 * helper class
300 */
301class SFStringInfo
302{
303        String string;
304        boolean carry;
305
306        public SFStringInfo(String s, boolean c)
307        {
308                this.string = s;
309                this.carry = c;
310        }
311}
Note: See TracBrowser for help on using the repository browser.