001    /*
002    // $Id: KeySegment.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.UnmodifiableArrayList;
023    
024    import java.util.List;
025    
026    /**
027     * Segment that represents a key or compound key.
028     *
029     * <p>Such a segment appears in an identifier with each component prefixed
030     * with "&amp;". For example, in the identifier
031     * "<code>[Customer].[State].&amp;[WA]&amp;[USA]</code>", the third segment is
032     * a compound key whose parts are "{@code WA}" and "{@code USA}".
033     *
034     * @see org.olap4j.mdx.NameSegment
035     *
036     * @version $Id: KeySegment.java 482 2012-01-05 23:27:27Z jhyde $
037     * @author jhyde
038     */
039    public class KeySegment implements IdentifierSegment {
040        private final List<NameSegment> subSegmentList;
041    
042        /**
043         * Creates a KeySegment with one or more sub-segments.
044         *
045         * @param subSegments Array of sub-segments
046         */
047        public KeySegment(NameSegment... subSegments) {
048            if (subSegments.length < 1) {
049                throw new IllegalArgumentException();
050            }
051            this.subSegmentList = UnmodifiableArrayList.asCopyOf(subSegments);
052        }
053    
054        /**
055         * Creates a KeySegment a list of sub-segments.
056         *
057         * @param subSegmentList List of sub-segments
058         */
059        public KeySegment(List<NameSegment> subSegmentList) {
060            if (subSegmentList.size() < 1) {
061                throw new IllegalArgumentException();
062            }
063            this.subSegmentList =
064                new UnmodifiableArrayList<NameSegment>(
065                    subSegmentList.toArray(
066                        new NameSegment[subSegmentList.size()]));
067        }
068    
069        public String toString() {
070            final StringBuilder buf = new StringBuilder();
071            toString(buf);
072            return buf.toString();
073        }
074    
075        public void toString(StringBuilder buf) {
076            for (IdentifierSegment segment : subSegmentList) {
077                buf.append('&');
078                segment.toString(buf);
079            }
080        }
081    
082        public ParseRegion getRegion() {
083            return IdentifierNode.sumSegmentRegions(subSegmentList);
084        }
085    
086        public Quoting getQuoting() {
087            return Quoting.KEY;
088        }
089    
090        public String getName() {
091            return null;
092        }
093    
094        public List<NameSegment> getKeyParts() {
095            return subSegmentList;
096        }
097    }
098    
099    // End KeySegment.java