001 package market;
002
003 import java.math.BigDecimal;
004 import java.text.DecimalFormat;
005 import java.text.SimpleDateFormat;
006 import java.util.Calendar;
007 import java.util.StringTokenizer;
008
009 import data.NumberValue;
010 import data.Value;
011 import data.ooimpl.CatalogItemImpl;
012 import data.ooimpl.StockItemImpl;
013 import data.swing.CountingStockTableModel;
014
015 /**
016 * A collection of methods to convert Strings, numbers and other Objects into another format.
017 */
018 public abstract class Conversions {
019
020 /**
021 * Converts a double into a String with a fixed number of decimal places.
022 * @param d the double to be converted.
023 * @param decimalPlace the number of decimal places
024 * @return the formatted String.
025 */
026 public static String fixedDecimal(double d, int decimalPlace) {
027 char[] dp = new char[2+decimalPlace];
028 for (int i=0; i<2+decimalPlace; i++) dp[i] = '0';
029 dp[1] = '.';
030 String pattern = String.valueOf(dp);
031 DecimalFormat df = new DecimalFormat(pattern);
032 return df.format(d);
033 }
034
035 /**
036 * Converts a Value that represents an amount of money in cents into a String,
037 * that represents the money in Euros (or Dollars...).
038 * @param v the value to be converted.
039 * @return the formatted String.
040 */
041 public static String valueToCurrency(Value v) {
042 return fixedDecimal(new Double(v.toString()).doubleValue()/100, 2);
043 }
044
045 /**
046 * Converts a Value that represents an amount of money in cents into a String,
047 * that represents the money in Euros (or Dollars...).
048 * @param v the value to be converted.
049 * @param extra an extra String added behind the converted value.
050 * @return the formatted String.
051 */
052 public static String valueToCurrency(Value v, String extra) {
053 return valueToCurrency(v) + extra;
054 }
055
056 /**
057 * Converts a double that represents an amount of money in cents into a String.
058 * that represents the money in Euros (or Dollars...).
059 * @param d the double to be converted.
060 * @return the formatted String.
061 */
062 public static String doubleToCurrency(double d) {
063 return fixedDecimal(d/100, 2);
064 }
065
066 /**
067 * Converts a double that represents an amount of money in cents into a String,
068 * that represents the money in Euros (or Dollars...).
069 * @param d the double to be converted.
070 * @param extra an extra String added behind the converted double.
071 * @return the formatted String.
072 */
073 public static String doubleToCurrency(double d, String extra) {
074 return doubleToCurrency(d) + extra;
075 }
076
077 /**
078 * Converts a Value to a Double.
079 * @param v the Value to be converted.
080 * @return the converted Value as Double.
081 */
082 public static Double valueToDouble(Value v) {
083 return new Double(v.toString());
084 }
085
086 /**
087 * Converts a currency string into an int.
088 *
089 * @param s the string to be converted.
090 * @param euroToCents if <code>true</code>, the string is interpreted as Euro value and multiplied by
091 * 100, otherwise the string is assumed to represent cents already.
092 * @return the money as cents.
093 */
094 public static int currencyToInt(String s, boolean euroToCents) {
095 int multiply = euroToCents ? 100 : 1;
096 return new Double(Double.valueOf(convertComma(s)).doubleValue()*multiply).intValue();
097 }
098
099 /**
100 * Cuts a double after the specified number of decimal places.
101 * @param d the double to be rounded.
102 * @param decimalPlace the decimal place after which is cut.
103 * @return the rounded double.
104 */
105 public static double round(double d, int decimalPlace) {
106 BigDecimal myDec = new BigDecimal(d);
107 return myDec.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP).doubleValue();
108 }
109
110 /**
111 * Converts all commas in a string into points (e.g. 1,225 => 1.225).
112 * @param s the String to be converted.
113 * @return the converted String.
114 */
115 public static String convertComma(String s) {
116 String newString = "";
117 if (s.startsWith(",")) newString = ".";
118 StringTokenizer st = new StringTokenizer(s, ",");
119 while (st.hasMoreTokens()) {
120 newString += st.nextToken();
121 if (st.hasMoreTokens()) newString += ".";
122 }
123 try {
124 return new Double(Double.parseDouble(newString)).toString();
125 }
126 catch (Exception e) {
127 return null;
128 }
129 }
130
131 /**
132 * Casts a CIArticle, a CountingStockTableModel.Record or a StockItemImpl into a CIArticle
133 * if possible. If the object cannot be converted <code>null</code> is returned.
134 * @param record the Object to be cast.
135 * @return the cast object.
136 */
137 public static CIArticle recordToCIArticle(Object record) {
138 if (record instanceof CIArticle) return (CIArticle)record;
139 if (record instanceof CountingStockTableModel.Record)
140 return (CIArticle)((CountingStockTableModel.Record)record).getDescriptor();
141 if (record instanceof StockItemImpl)
142 return (CIArticle)((StockItemImpl)record).getAssociatedItem(null);
143 return null;
144 }
145
146 /**
147 * Creates a CatalogItemImpl from a UCustomer.
148 * @param customer the customer to create the CatalogItemImpl from.
149 * @return the created CatalogItemImpl.
150 */
151 public static CatalogItemImpl customerToCI(UCustomer customer){
152 CatalogItemImpl cii = new CatalogItemImpl(customer.getName()){
153 protected CatalogItemImpl getShallowClone() {
154 return null;
155 }
156 };
157 return cii;
158 }
159
160 /**
161 * Converts a date string ("dd.MM.yyyy") to a Calendar date.
162 * @param dateString the String representing the date.
163 * @return the Calendar.
164 */
165 public static Calendar stringToCalendar(String dateString) {
166 boolean error = false;
167 SimpleDateFormat sdf = new SimpleDateFormat ("dd.MM.yyyy");
168 sdf.setLenient(false);
169 try {
170 sdf.parse(dateString);
171 //Parsing also accepts dates without left handed zeros,
172 //so this extra check for the String's length is being done
173 error = (dateString.length() != 10);
174 }
175 catch (java.text.ParseException e) {
176 error = true;
177 }
178 if (error) return null;
179 else {
180 StringTokenizer strTok = new StringTokenizer((String)dateString,".",false);
181 int day = new Integer(strTok.nextToken()).intValue();
182 int month = new Integer(strTok.nextToken()).intValue();
183 int year = new Integer(strTok.nextToken()).intValue();
184 return new MarketCalendar(year, month - 1, day);
185 }
186 }
187
188
189 /**
190 * @return today's (today means "realtime today") date with time set to 0:00.00.
191 */
192 public static Calendar createToday() {
193 Calendar c = new MarketCalendar();
194 return new MarketCalendar(c.get(Calendar.YEAR), c.get(Calendar.MONTH),
195 c.get(Calendar.DATE));
196 }
197
198 /**
199 * Converts a Value to an int.
200 * @param v the Value to be converted.
201 * @return the converted Value as int.
202 */
203 public static int valueToInt(Value v){
204 if(v instanceof NumberValue) return ((NumberValue)v).getValue().intValue();
205 return 0;
206 }
207
208 /**
209 * Computes the difference between two dates in days.
210 * @param cEarly the first date.
211 * @param cLate the second date.
212 * @return the difference in days.
213 */
214 public static int dayDifference(Calendar cEarly, Calendar cLate) {
215 long l1 = cEarly.getTime().getTime();
216 long l2 = cLate.getTime().getTime();
217 return new Long((l2 - l1)/86400000).intValue();
218 }
219 }