annotate at/ofai/music/util/Format.java @ 5:bcb4c9697967 tip

Add README and CITATION files
author Chris Cannam
date Tue, 03 Dec 2013 12:58:05 +0000
parents 4c3f5bc01c97
children
rev   line source
Chris@2 1 /*
Chris@2 2 Copyright (C) 2001, 2006 by Simon Dixon
Chris@2 3
Chris@2 4 This program is free software; you can redistribute it and/or modify
Chris@2 5 it under the terms of the GNU General Public License as published by
Chris@2 6 the Free Software Foundation; either version 2 of the License, or
Chris@2 7 (at your option) any later version.
Chris@2 8
Chris@2 9 This program is distributed in the hope that it will be useful,
Chris@2 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@2 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@2 12 GNU General Public License for more details.
Chris@2 13
Chris@2 14 You should have received a copy of the GNU General Public License along
Chris@2 15 with this program (the file gpl.txt); if not, download it from
Chris@2 16 http://www.gnu.org/licenses/gpl.txt or write to the
Chris@2 17 Free Software Foundation, Inc.,
Chris@2 18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Chris@2 19 */
Chris@2 20
Chris@2 21 package at.ofai.music.util;
Chris@2 22
Chris@2 23 import java.text.NumberFormat;
Chris@2 24 import java.io.*;
Chris@2 25
Chris@2 26 /** A simple utility class for easier formatted output of numeric data.
Chris@2 27 * Formatting is controlled by static objects, so that number widths and
Chris@2 28 * precisions can be set once, and used multiple times.
Chris@2 29 */
Chris@2 30 public class Format {
Chris@2 31
Chris@2 32 /** The object which performs formatting of integers */
Chris@2 33 protected static NumberFormat intFormat = NumberFormat.getInstance();
Chris@2 34
Chris@2 35 /** The object which performs formatting of doubles */
Chris@2 36 protected static NumberFormat doubleFormat = NumberFormat.getInstance();
Chris@2 37
Chris@2 38 /** The preferred notation for positive numbers (default is no '+' sign) */
Chris@2 39 protected static char plusSign = ' ';
Chris@2 40
Chris@2 41 /** Set the number of digits to appear after the decimal point
Chris@2 42 * @param dp the number of characters after the decimal point
Chris@2 43 */
Chris@2 44 public static void setPostDigits(int dp) {
Chris@2 45 doubleFormat.setMinimumFractionDigits(dp);
Chris@2 46 doubleFormat.setMaximumFractionDigits(dp);
Chris@2 47 } // setPostDigits()
Chris@2 48
Chris@2 49 /** Set the number of digits to appear before the decimal point.
Chris@2 50 * If the number does not require this many digits, it will be
Chris@2 51 * padded with spaces.
Chris@2 52 * @param dp the number of characters before the decimal point
Chris@2 53 */
Chris@2 54 public static void setPreDigits(int dp) {
Chris@2 55 doubleFormat.setMinimumIntegerDigits(dp);
Chris@2 56 } // setPreDigits()
Chris@2 57
Chris@2 58 /** Set the number of digits for displaying integers.
Chris@2 59 * If the number does not require this many digits, it will be
Chris@2 60 * padded with spaces.
Chris@2 61 * @param dp the number of digits for displaying integers
Chris@2 62 */
Chris@2 63 public static void setIntDigits(int dp) {
Chris@2 64 intFormat.setMinimumIntegerDigits(dp);
Chris@2 65 intFormat.setMinimumFractionDigits(0);
Chris@2 66 intFormat.setMaximumFractionDigits(0);
Chris@2 67 } // setIntegerDigits()
Chris@2 68
Chris@2 69 /** Set whether digits should be grouped in 3's as in 12,000,000.
Chris@2 70 * @param flag true if grouping should be used, false if not
Chris@2 71 */
Chris@2 72 public static void setGroupingUsed(boolean flag) {
Chris@2 73 doubleFormat.setGroupingUsed(flag);
Chris@2 74 intFormat.setGroupingUsed(flag);
Chris@2 75 } // setGroupingUsed()
Chris@2 76
Chris@2 77 /** Sets the initial character for positive numbers (usually blank or '+')
Chris@2 78 * @param c the character to prefix to positive numbers
Chris@2 79 */
Chris@2 80 public static void setPlusSign(char c) {
Chris@2 81 plusSign = c;
Chris@2 82 } // setPlusSign()
Chris@2 83
Chris@2 84 /** Initialise the formatting objects with the desired settings.
Chris@2 85 * @param id the number of characters for displaying integers
Chris@2 86 * @param did the number of characters before the decimal point
Chris@2 87 * @param dfd the number of characters after the decimal point
Chris@2 88 * @param grouping true if grouping should be used, false if not
Chris@2 89 */
Chris@2 90 public static void init(int id, int did, int dfd, boolean grouping) {
Chris@2 91 setIntDigits(id);
Chris@2 92 setPreDigits(did);
Chris@2 93 setPostDigits(dfd);
Chris@2 94 setGroupingUsed(grouping);
Chris@2 95 } // init()
Chris@2 96
Chris@2 97 /** Convert a double to a String with a set number of decimal places and
Chris@2 98 * padding to the desired minimum number of characters before the decimal
Chris@2 99 * point.
Chris@2 100 * @param n the number to convert
Chris@2 101 * @param id the number of characters before the decimal point
Chris@2 102 * @param fd the number of characters after the decimal point
Chris@2 103 */
Chris@2 104 public static String d(double n, int id, int fd) {
Chris@2 105 setPreDigits(id);
Chris@2 106 return d(n, fd);
Chris@2 107 } // d()
Chris@2 108
Chris@2 109 /** Convert a double to a String with a set number of decimal places
Chris@2 110 * @param n the number to convert
Chris@2 111 * @param fd the number of characters after the decimal point
Chris@2 112 */
Chris@2 113 public static String d(double n, int fd) {
Chris@2 114 setPostDigits(fd);
Chris@2 115 return d(n);
Chris@2 116 } // d()
Chris@2 117
Chris@2 118 /** Convert a double to a String. The number of decimal places and
Chris@2 119 * characters before the decimal point are stored in the static members
Chris@2 120 * of this class.
Chris@2 121 * @param n the number to convert
Chris@2 122 */
Chris@2 123 public static String d(double n) {
Chris@2 124 String s;
Chris@2 125 if (Double.isNaN(n))
Chris@2 126 return "NaN";
Chris@2 127 s = doubleFormat.format(n);
Chris@2 128 if (n >= 0)
Chris@2 129 s = plusSign + s;
Chris@2 130 char[] c = s.toCharArray();
Chris@2 131 int i;
Chris@2 132 for (i = 1; (i < c.length-1) && (c[i] == '0') && (c[i+1] != '.'); i++) {
Chris@2 133 c[i] = c[i-1];
Chris@2 134 c[i-1] = ' ';
Chris@2 135 }
Chris@2 136 if (i > 1)
Chris@2 137 s = new String(c);
Chris@2 138 return s;
Chris@2 139 } // d()
Chris@2 140
Chris@2 141 /** Convert an integer to a String with padding to the desired minimum width
Chris@2 142 * @param n the number to convert
Chris@2 143 * @param id the desired minimum width
Chris@2 144 */
Chris@2 145 public static String i(int n, int id) {
Chris@2 146 setIntDigits(id);
Chris@2 147 return i(n);
Chris@2 148 } // i()
Chris@2 149
Chris@2 150 /** Convert an integer to a String with padding to the desired minimum width
Chris@2 151 * @param n the number to convert
Chris@2 152 */
Chris@2 153 public static String i(int n) {
Chris@2 154 return (n < 0)? intFormat.format(n): plusSign+intFormat.format(n);
Chris@2 155 } // i()
Chris@2 156
Chris@2 157 /** Output an array to file as an assignment statement for input to Matlab
Chris@2 158 * @param data the values to print to 4 decimal places
Chris@2 159 * @param name the variable name in the Matlab assignment statement; the
Chris@2 160 * file name is the same name with ".m" appended, or standard out if
Chris@2 161 * unable to open this file
Chris@2 162 */
Chris@2 163 public static void matlab(double[] data, String name) {
Chris@2 164 matlab(data, name, 4);
Chris@2 165 } // matlab()
Chris@2 166
Chris@2 167 /** Output an array to file as an assignment statement for input to Matlab
Chris@2 168 * @param data the values to print
Chris@2 169 * @param name the variable name in the Matlab assignment statement; the
Chris@2 170 * file name is the same name with ".m" appended, or standard out if
Chris@2 171 * unable to open this file
Chris@2 172 * @param dp the number of decimal places to print
Chris@2 173 */
Chris@2 174 public static void matlab(double[] data, String name, int dp) {
Chris@2 175 setPostDigits(dp);
Chris@2 176 PrintStream out;
Chris@2 177 try {
Chris@2 178 out = new PrintStream(new FileOutputStream(name+".m"));
Chris@2 179 } catch (FileNotFoundException e) {
Chris@2 180 out = System.out;
Chris@2 181 }
Chris@2 182 matlab(data, name, out);
Chris@2 183 if (out != System.out)
Chris@2 184 out.close();
Chris@2 185 } // matlab
Chris@2 186
Chris@2 187 /** Output an array to a printstream as an assignment statement for input to
Chris@2 188 * Matlab
Chris@2 189 * @param data the values to print
Chris@2 190 * @param name the variable name in the Matlab assignment statement
Chris@2 191 * @param out the output stream to print to
Chris@2 192 */
Chris@2 193 public static void matlab(double[] data, String name, PrintStream out) {
Chris@2 194 out.println(name + " = [");
Chris@2 195 setGroupingUsed(false);
Chris@2 196 for (int i=0; i < data.length; i++)
Chris@2 197 out.println(d(data[i]));
Chris@2 198 out.println("];");
Chris@2 199 } // matlab()
Chris@2 200
Chris@2 201 } // class Format