001 /* 002 // $Id: //open/mondrian/src/main/mondrian/olap/fun/SetToStrFunDef.java#5 $ 003 // This software is subject to the terms of the Common Public License 004 // Agreement, available at the following URL: 005 // http://www.opensource.org/licenses/cpl.html. 006 // Copyright (C) 2006-2007 Julian Hyde 007 // All Rights Reserved. 008 // You must accept the terms of that agreement to use this software. 009 */ 010 package mondrian.olap.fun; 011 012 import mondrian.calc.Calc; 013 import mondrian.calc.ExpCompiler; 014 import mondrian.calc.ListCalc; 015 import mondrian.calc.impl.AbstractStringCalc; 016 import mondrian.mdx.ResolvedFunCall; 017 import mondrian.olap.Evaluator; 018 import mondrian.olap.Member; 019 import mondrian.olap.Exp; 020 import mondrian.olap.type.SetType; 021 022 import java.util.List; 023 024 /** 025 * Definition of the <code>SetToStr</code> MDX function. 026 * 027 * @author jhyde 028 * @version $Id: //open/mondrian/src/main/mondrian/olap/fun/SetToStrFunDef.java#5 $ 029 * @since Aug 3, 2006 030 */ 031 class SetToStrFunDef extends FunDefBase { 032 public static final FunDefBase instance = new SetToStrFunDef(); 033 034 private SetToStrFunDef() { 035 super("SetToStr", "Constructs a string from a set.", "fSx"); 036 } 037 038 public Calc compileCall(ResolvedFunCall call, ExpCompiler compiler) { 039 Exp arg = call.getArg(0); 040 final ListCalc listCalc = compiler.compileList(arg); 041 if (((SetType) arg.getType()).getArity() == 1) { 042 return new AbstractStringCalc(call, new Calc[] {listCalc}) { 043 public String evaluateString(Evaluator evaluator) { 044 final List<Member> list = 045 (List<Member>) listCalc.evaluateList(evaluator); 046 return memberSetToStr(list); 047 } 048 }; 049 } else { 050 return new AbstractStringCalc(call, new Calc[] {listCalc}) { 051 public String evaluateString(Evaluator evaluator) { 052 final List<Member[]> list = 053 (List<Member[]>) listCalc.evaluateList(evaluator); 054 return tupleSetToStr(list); 055 } 056 }; 057 } 058 } 059 060 static String memberSetToStr(List<Member> list) { 061 StringBuilder buf = new StringBuilder(); 062 buf.append("{"); 063 int k = 0; 064 for (Member member : list) { 065 if (k++ > 0) { 066 buf.append(", "); 067 } 068 buf.append(member.getUniqueName()); 069 } 070 buf.append("}"); 071 return buf.toString(); 072 } 073 074 static String tupleSetToStr(List<Member[]> list) { 075 StringBuilder buf = new StringBuilder(); 076 buf.append("{"); 077 int k = 0; 078 for (Member[] members : list) { 079 if (k++ > 0) { 080 buf.append(", "); 081 } 082 appendTuple(buf, members); 083 } 084 buf.append("}"); 085 return buf.toString(); 086 } 087 } 088 089 // End SetToStrFunDef.java