001    /*
002    // $Id: //open/mondrian/src/main/mondrian/rolap/StringList.java#4 $
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) 2001-2002 Kana Software, Inc.
007    // Copyright (C) 2001-2006 Julian Hyde and others
008    // All Rights Reserved.
009    // You must accept the terms of that agreement to use this software.
010    //
011    // jhyde, 29 December, 2001
012    */
013    
014    package mondrian.rolap;
015    import mondrian.olap.Util;
016    
017    /**
018     * <code>StringList</code> makes it easy to build up a comma-separated string.
019     *
020     * @author jhyde
021     * @since 29 December, 2001
022     * @version $Id: //open/mondrian/src/main/mondrian/rolap/StringList.java#4 $
023     */
024    class StringList
025    {
026        private final StringBuilder buf;
027        private final String first, mid, last;
028        private int count;
029    
030        StringList(String first, String mid)
031        {
032            this.buf = new StringBuilder(first);
033            this.count = 0;
034            this.first = first;
035            this.mid = mid;
036            this.last = "";
037        }
038        StringList(String first)
039        {
040            this(first, ", ");
041        }
042        int getCount()
043        {
044            return count;
045        }
046        boolean isEmpty()
047        {
048            return count == 0;
049        }
050        /** Creates a new item. */
051        void newItem(String s)
052        {
053            if (count++ > 0) {
054                buf.append(mid);
055            }
056            buf.append(s);
057        }
058        /** Appends to an existing item. */
059        void append(String s)
060        {
061            Util.assertTrue(count > 0);
062            buf.append(s);
063        }
064        // override Object
065        public String toString()
066        {
067            buf.append(last);
068            return buf.toString();
069        }
070    };
071    
072    
073    // End StringList.java