001: /*
002: The contents of this file are subject to the Common Public Attribution License
003: Version 1.0 (the "License"); you may not use this file except in compliance with
004: the License. You may obtain a copy of the License at
005: http://www.projity.com/license . The License is based on the Mozilla Public
006: License Version 1.1 but Sections 14 and 15 have been added to cover use of
007: software over a computer network and provide for limited attribution for the
008: Original Developer. In addition, Exhibit A has been modified to be consistent
009: with Exhibit B.
010:
011: Software distributed under the License is distributed on an "AS IS" basis,
012: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
013: specific language governing rights and limitations under the License. The
014: Original Code is OpenProj. The Original Developer is the Initial Developer and
015: is Projity, Inc. All portions of the code written by Projity are Copyright (c)
016: 2006, 2007. All Rights Reserved. Contributors Projity, Inc.
017:
018: Alternatively, the contents of this file may be used under the terms of the
019: Projity End-User License Agreeement (the Projity License), in which case the
020: provisions of the Projity License are applicable instead of those above. If you
021: wish to allow use of your version of this file only under the terms of the
022: Projity License and not to allow others to use your version of this file under
023: the CPAL, indicate your decision by deleting the provisions above and replace
024: them with the notice and other provisions required by the Projity License. If
025: you do not delete the provisions above, a recipient may use your version of this
026: file under either the CPAL or the Projity License.
027:
028: [NOTE: The text of this license may differ slightly from the text of the notices
029: in Exhibits A and B of the license at http://www.projity.com/license. You should
030: use the latest text at http://www.projity.com/license for your modifications.
031: You may not remove this license text from the source files.]
032:
033: Attribution Information: Attribution Copyright Notice: Copyright © 2006, 2007
034: Projity, Inc. Attribution Phrase (not exceeding 10 words): Powered by OpenProj,
035: an open source solution from Projity. Attribution URL: http://www.projity.com
036: Graphic Image as provided in the Covered Code as file: openproj_logo.png with
037: alternatives listed on http://www.projity.com/logo
038:
039: Display of Attribution Information is required in Larger Works which are defined
040: in the CPAL as a work which combines Covered Code or portions thereof with code
041: not governed by the terms of the CPAL. However, in addition to the other notice
042: obligations, all copies of the Covered Code in Executable and Source Code form
043: distributed must, as a form of attribution of the original author, include on
044: each user interface screen the "OpenProj" logo visible to all users. The
045: OpenProj logo should be located horizontally aligned with the menu bar and left
046: justified on the top left of the screen adjacent to the File menu. The logo
047: must be at least 100 x 25 pixels. When users click on the "OpenProj" logo it
048: must direct them back to http://www.projity.com.
049: */
050: package com.projity.datatype;
051:
052: import java.text.FieldPosition;
053: import java.text.NumberFormat;
054: import java.text.ParsePosition;
055: import java.util.regex.Pattern;
056: import java.util.regex.Matcher;
057:
058: import com.projity.options.EditOption;
059: import com.projity.options.ScheduleOption;
060: import com.projity.strings.Messages;
061: import java.text.Format;
062:
063: /**
064: * Instead of creating a true Duration type, I use unused bits of the long which
065: * stores durations. The idea is that the algorithms will run faster because
066: * there is no object churn and fewer function calls.
067: */
068: public class DurationFormat extends Format {
069: private boolean showPlusSign = false;
070: private boolean isWork = false;
071: private boolean canBeNonTemporal = false;
072: private static Format instance = null;
073:
074: public static Format getInstance() {
075: if (instance == null)
076: instance = new DurationFormat(false);
077: return instance;
078: }
079:
080: private static Format signedInstance = null;
081:
082: public static Format getSignedInstance() {
083: if (signedInstance == null)
084: signedInstance = new DurationFormat(true);
085: return signedInstance;
086: }
087:
088: private static Format workInstance = null;
089:
090: public static Format getWorkInstance() {
091: if (workInstance == null) {
092: workInstance = new DurationFormat(false);
093: ((DurationFormat) workInstance).isWork = true;
094: }
095: return workInstance;
096: }
097:
098: private static Format nonTemporalWorkInstance = null;
099:
100: public static Format getNonTemporalWorkInstance() {
101: if (nonTemporalWorkInstance == null) {
102: nonTemporalWorkInstance = new DurationFormat(false);
103: ((DurationFormat) nonTemporalWorkInstance).isWork = true;
104: ((DurationFormat) nonTemporalWorkInstance).canBeNonTemporal = true;
105: }
106: return nonTemporalWorkInstance;
107: }
108:
109: // these strings are themselves parts of string ids in properties file and as such must be hard coded as below
110: private static String[] types = { "minute", "hour", "day", "week",
111: "month", "year", "percent", "eminute", "ehour", "eday",
112: "eweek", "emonth", "eyear", "epercent" };
113:
114: private static final int SINGULAR = 0;
115: private static final int PLURAL = 1;
116: private static final String multiple[] = { ".singular", ".plural" };
117: private static int TYPE_COUNT = types.length;
118: private static int NAME_COUNT = 4;
119: private static String[][][] typesArray = new String[NAME_COUNT][multiple.length][TYPE_COUNT];
120: private static Pattern[] pattern = new Pattern[TYPE_COUNT];
121: private static String estimatedSymbol = Messages
122: .getString("Units.estimatedSymbol");
123:
124: //private constructor initializes values.
125: private DurationFormat(boolean showPlusSign) {
126: this .showPlusSign = showPlusSign;
127: String estimated = Messages
128: .getString("Units.estimatedSymbolRegex"); // Like ?
129:
130: // a bunch of init code which reads the possible values for durations from localized messages
131: for (int i = 0; i < TYPE_COUNT; i++) {
132: String singularNames = null;
133: String pluralNames = null;
134: for (int j = 0; j < multiple.length; j++) {
135: String names = new String(Messages.getString("Units."
136: + types[i] + multiple[j]));
137: if (j == SINGULAR)
138: singularNames = names;
139: if (j == PLURAL)
140: pluralNames = names;
141: String[] units = names.split("\\|"); // index into the names list, getting string
142: //split has a big memory cost so names are pre-splited
143: for (int k = 0; k < NAME_COUNT; k++)
144: typesArray[k][j][i] = units[k];
145: }
146: // The pattern represents the following:
147: // Singular type OR Plural type, optinally followed by white space and the estimated symbol (?)
148: // Two groups are saved: Group 1 is the type, Group 2 is the estimated symbol
149: pattern[i] = Pattern.compile("((?:" + singularNames + ")"
150: + "|(?:" + pluralNames + "))?" + "(\\s*"
151: + estimated + "?)");
152: }
153: }
154:
155: private static NumberFormat DECIMAL_FORMAT = NumberFormat
156: .getNumberInstance();
157:
158: /* (non-Javadoc)
159: * @see java.text.Format#parseObject(java.lang.String, java.text.ParsePosition)
160: */
161: public Object parseObject(String durationString, ParsePosition pos) {
162: Object result = null;
163: if (durationString.length() == 0)
164: return null;
165:
166: if (durationString.charAt(pos.getIndex()) == '+') // if string begins with + sign, ignore it
167: pos.setIndex(pos.getIndex() + 1);
168:
169: Number numberResult = DECIMAL_FORMAT.parse(durationString, pos);
170: if (numberResult == null)
171: return null;
172: String durationPart = durationString.substring(pos.getIndex());
173: durationPart = durationPart.trim();
174: Matcher matcher;
175: for (int i = 0; i < TYPE_COUNT; i++) { // find hte appropriate units
176: matcher = pattern[i].matcher(durationPart);
177: if (matcher.matches()) {
178: int timeUnit = (matcher.group(1) != null) ? i
179: : TimeUnit.NONE; // first group is units. If no units, then it will match, but should use default: NONE
180: double value = numberResult.doubleValue();
181: if (timeUnit == TimeUnit.PERCENT
182: || timeUnit == TimeUnit.ELAPSED_PERCENT)
183: value /= 100.0;
184: if (timeUnit == TimeUnit.NONE && isWork) {
185: if (canBeNonTemporal)
186: timeUnit = TimeUnit.NON_TEMPORAL;
187: else
188: timeUnit = ScheduleOption.getInstance()
189: .getWorkUnit(); // use default work unit if work and nothing entered
190: }
191: long longResult = Duration.getInstance(value, timeUnit);
192: if (Duration.millis(longResult) > Duration.MAX_DURATION) // check for too big
193: return null;
194: if (matcher.group(2).length() != 0) { // second group is estimated
195: longResult = Duration.setAsEstimated(longResult,
196: true);
197: }
198:
199: result = new Duration(longResult);
200:
201: return result;
202: }
203: }
204:
205: return null;
206: }
207:
208: /* (non-Javadoc)
209: * @see java.text.Format#format(java.lang.Object, java.lang.StringBuffer, java.text.FieldPosition)
210: */
211: public StringBuffer format(Object durationObject,
212: StringBuffer toAppendTo, FieldPosition pos) {
213: long duration = ((Duration) durationObject).getEncodedMillis();
214: if (((Duration) durationObject).isWork()
215: && Duration.getType(duration) != TimeUnit.NON_TEMPORAL) {
216: duration = Duration.setAsTimeUnit(duration, ScheduleOption
217: .getInstance().getWorkUnit());
218: }
219:
220: double value = Duration.getValue(duration);
221: int type = Duration.getEffectiveType(duration);
222: if (value > 0D && showPlusSign)
223: toAppendTo.append("+");
224: boolean isPercent = Duration.isPercent(duration);
225: if (isPercent)
226: value *= 100.0;
227: DECIMAL_FORMAT.format(value, toAppendTo, pos);
228:
229: String unit = formatTypeUnit(type, (Math.abs(value) == 1.0),
230: EditOption.getInstance().isAddSpaceBeforeLabel(),
231: Duration.isPercent(duration), Duration
232: .isEstimated(duration), EditOption
233: .getInstance().getViewAs(type));
234: toAppendTo.append(unit);
235: return toAppendTo;
236: }
237:
238: public String formatCompact(Object durationObject) {
239: StringBuffer toAppendTo = new StringBuffer();
240: long duration = ((Duration) durationObject).getEncodedMillis();
241: if (((Duration) durationObject).isWork()
242: && Duration.getType(duration) != TimeUnit.NON_TEMPORAL) {
243: duration = Duration.setAsTimeUnit(duration, ScheduleOption
244: .getInstance().getWorkUnit());
245: }
246:
247: double value = Duration.getValue(duration);
248: int type = Duration.getEffectiveType(duration);
249: if (value > 0D && showPlusSign)
250: toAppendTo.append("+");
251: boolean isPercent = Duration.isPercent(duration);
252: if (isPercent)
253: value *= 100.0;
254: toAppendTo.append(DECIMAL_FORMAT.format(value));
255:
256: String unit = formatTypeUnit(type, (Math.abs(value) == 1.0),
257: false, Duration.isPercent(duration), Duration
258: .isEstimated(duration), 3);
259: toAppendTo.append(unit);
260: return toAppendTo.toString();
261: }
262:
263: public static String formatTypeUnit(int type, boolean isSingular,
264: boolean addSpace, boolean isPercent, boolean isEstimated,
265: int displayIndex) {
266: StringBuffer toAppendTo = new StringBuffer();
267: if (type == TimeUnit.NON_TEMPORAL)
268: return "";
269: if (addSpace && !isPercent) {
270: toAppendTo.append(" ");
271: }
272: String unit = typesArray[displayIndex][isSingular ? SINGULAR
273: : PLURAL][type]; // get either singular or plural names list
274: toAppendTo.append(unit);
275: if (isEstimated)
276: toAppendTo.append(estimatedSymbol);
277: return toAppendTo.toString();
278: }
279:
280: public static String formatTypeUnit(int type) {
281: DurationFormat.getInstance(); // make sure it is initialized
282: return formatTypeUnit(type, true, false, false, false,
283: EditOption.getInstance().getViewAs(type));
284: }
285:
286: public static String format(long millis) {
287: return getInstance().format(new Duration(millis)).toString();
288: }
289:
290: public static String formatCompact(long millis) {
291: return ((DurationFormat) getInstance()).formatCompact(
292: new Duration(millis)).toString();
293: }
294:
295: public static String formatWork(long millis) {
296: return getWorkInstance().format(new Work(millis)).toString();
297: }
298:
299: public static String formatWork(Object millis) {
300: if (millis != null && millis instanceof Long)
301: return formatWork(((Long) millis).longValue());
302: return getWorkInstance().format(millis);
303: }
304: }
|