use of mondrian.util.Format in project mondrian by pentaho.
the class CurrentDateMemberUdf method execute.
public Object execute(Evaluator evaluator, Argument[] arguments) {
if (resultDateMember != null) {
return resultDateMember;
}
// determine the current date
Object formatArg = arguments[1].evaluateScalar(evaluator);
final Locale locale = Locale.getDefault();
final Format format = new Format((String) formatArg, locale);
String currDateStr = format.format(getDate(evaluator, arguments));
// determine the match type
MatchType matchType;
if (arguments.length == 3) {
String matchStr = arguments[2].evaluateScalar(evaluator).toString();
matchType = Enum.valueOf(MatchType.class, matchStr);
} else {
matchType = MatchType.EXACT;
}
List<Id.Segment> uniqueNames = Util.parseIdentifier(currDateStr);
resultDateMember = evaluator.getSchemaReader().getMemberByUniqueName(uniqueNames, false, matchType);
if (resultDateMember != null) {
return resultDateMember;
}
// if there is no matching member, return the null member for
// the specified dimension/hierarchy
Object arg0 = arguments[0].evaluate(evaluator);
if (arg0 instanceof Hierarchy) {
resultDateMember = ((Hierarchy) arg0).getNullMember();
} else {
resultDateMember = ((Dimension) arg0).getHierarchy().getNullMember();
}
return resultDateMember;
}
use of mondrian.util.Format in project pentaho-platform by pentaho.
the class InlineMemberFormatter method formatMember.
@Override
public String formatMember(Member member) {
Annotation annot = member.getLevel().getAnnotationMap().get(FORMAT_STRING);
if (annot == null) {
throw new IllegalStateException("Missing InlineMemberFormatString on level " + member.getLevel().getUniqueName());
}
Object key = member.getPropertyValue(Property.KEY.getName());
Format format = Format.get(annot.getValue().toString(), Locale.getDefault());
return format.format(key);
}
use of mondrian.util.Format in project mondrian by pentaho.
the class FormatFunDef method compileCall.
public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) {
final Exp[] args = call.getArgs();
final Calc calc = compiler.compileScalar(call.getArg(0), true);
final Locale locale = compiler.getEvaluator().getConnectionLocale();
if (args[1] instanceof Literal) {
// Constant string expression: optimize by
// compiling format string.
String formatString = (String) ((Literal) args[1]).getValue();
final Format format = new Format(formatString, locale);
return new AbstractStringCalc(call, new Calc[] { calc }) {
public String evaluateString(Evaluator evaluator) {
final Object o = calc.evaluate(evaluator);
return format.format(o);
}
};
} else {
// Variable string expression
final StringCalc stringCalc = compiler.compileString(call.getArg(1));
return new AbstractStringCalc(call, new Calc[] { calc, stringCalc }) {
public String evaluateString(Evaluator evaluator) {
final Object o = calc.evaluate(evaluator);
final String formatString = stringCalc.evaluateString(evaluator);
final Format format = new Format(formatString, locale);
return format.format(o);
}
};
}
}
use of mondrian.util.Format in project mondrian by pentaho.
the class I18nTest method testFormat.
public void testFormat() {
// Make sure Util is loaded, so that the LocaleFormatFactory gets
// registered.
Util.discard(Util.nl);
Locale spanish = new Locale("es", "ES");
Locale german = new Locale("de", "DE");
// Thousands and decimal separators are different in Spain
Format numFormat = new Format("#,000.00", spanish);
assertEquals("123.456,79", numFormat.format(new Double(123456.789)));
// Currency too
Format currencyFormat = new Format("Currency", spanish);
assertEquals("1.234.567,79 " + Euro, currencyFormat.format(new Double(1234567.789)));
// Dates
Format dateFormat = new Format("Medium Date", spanish);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2005);
// January, 0-based
calendar.set(Calendar.MONTH, 0);
calendar.set(Calendar.DATE, 22);
java.util.Date date = calendar.getTime();
assertEquals("22-ene-05", dateFormat.format(date));
// Dates in German
dateFormat = new Format("Long Date", german);
assertEquals("Samstag, Januar 22, 2005", dateFormat.format(date));
}
use of mondrian.util.Format in project mondrian by pentaho.
the class CurrentDateStringUdf method execute.
public Object execute(Evaluator evaluator, Argument[] arguments) {
Object arg = arguments[0].evaluateScalar(evaluator);
final Locale locale = Locale.getDefault();
final Format format = new Format((String) arg, locale);
Date currDate = evaluator.getQueryStartTime();
return format.format(currDate);
}
Aggregations