001    /*
002    // $Id: NameSegment.java 482 2012-01-05 23:27:27Z jhyde $
003    //
004    // Licensed to Julian Hyde under one or more contributor license
005    // agreements. See the NOTICE file distributed with this work for
006    // additional information regarding copyright ownership.
007    //
008    // Julian Hyde licenses this file to you under the Apache License,
009    // Version 2.0 (the "License"); you may not use this file except in
010    // compliance with the License. You may obtain a copy of the License at:
011    //
012    // http://www.apache.org/licenses/LICENSE-2.0
013    //
014    // Unless required by applicable law or agreed to in writing, software
015    // distributed under the License is distributed on an "AS IS" BASIS,
016    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017    // See the License for the specific language governing permissions and
018    // limitations under the License.
019    */
020    package org.olap4j.mdx;
021    
022    import org.olap4j.impl.Olap4jUtil;
023    
024    import java.util.List;
025    
026    /**
027     * Component in a compound identifier that describes the name of an object.
028     * Optionally, the name is quoted in brackets.
029     *
030     * @see KeySegment
031     *
032     * @version $Id: NameSegment.java 482 2012-01-05 23:27:27Z jhyde $
033     * @author jhyde
034     */
035    public class NameSegment implements IdentifierSegment {
036        final String name;
037        final Quoting quoting;
038        private final ParseRegion region;
039    
040        /**
041         * Creates a segment with the given quoting and region.
042         *
043         * @param region Region of source code
044         * @param name Name
045         * @param quoting Quoting style
046         */
047        public NameSegment(
048            ParseRegion region,
049            String name,
050            Quoting quoting)
051        {
052            this.region = region;
053            this.name = name;
054            this.quoting = quoting;
055            if (name == null) {
056                throw new NullPointerException();
057            }
058            if (!(quoting == Quoting.QUOTED || quoting == Quoting.UNQUOTED)) {
059                throw new IllegalArgumentException();
060            }
061        }
062    
063        /**
064         * Creates a quoted segment, "[name]".
065         *
066         * @param name Name of segment
067         */
068        public NameSegment(String name) {
069            this(null, name, Quoting.QUOTED);
070        }
071    
072        public String toString() {
073            switch (quoting) {
074            case UNQUOTED:
075                return name;
076            case QUOTED:
077                return IdentifierNode.quoteMdxIdentifier(name);
078            default:
079                throw Olap4jUtil.unexpected(quoting);
080            }
081        }
082    
083        public void toString(StringBuilder buf) {
084            switch (quoting) {
085            case UNQUOTED:
086                buf.append(name);
087                return;
088            case QUOTED:
089                IdentifierNode.quoteMdxIdentifier(name, buf);
090                return;
091            default:
092                throw Olap4jUtil.unexpected(quoting);
093            }
094        }
095        public ParseRegion getRegion() {
096            return region;
097        }
098    
099        public String getName() {
100            return name;
101        }
102    
103        public Quoting getQuoting() {
104            return quoting;
105        }
106    
107        public List<NameSegment> getKeyParts() {
108            return null;
109        }
110    }
111    
112    // End NameSegment.java