001/*
002// Licensed to Julian Hyde under one or more contributor license
003// agreements. See the NOTICE file distributed with this work for
004// additional information regarding copyright ownership.
005//
006// Julian Hyde licenses this file to you under the Apache License,
007// Version 2.0 (the "License"); you may not use this file except in
008// compliance with the License. You may obtain a copy of the License at:
009//
010// http://www.apache.org/licenses/LICENSE-2.0
011//
012// Unless required by applicable law or agreed to in writing, software
013// distributed under the License is distributed on an "AS IS" BASIS,
014// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015// See the License for the specific language governing permissions and
016// limitations under the License.
017*/
018package org.olap4j.mdx;
019
020import org.olap4j.impl.UnmodifiableArrayList;
021
022import java.util.List;
023
024/**
025 * Segment that represents a key or compound key.
026 *
027 * <p>Such a segment appears in an identifier with each component prefixed
028 * with "&amp;". For example, in the identifier
029 * "<code>[Customer].[State].&amp;[WA]&amp;[USA]</code>", the third segment is
030 * a compound key whose parts are "{@code WA}" and "{@code USA}".
031 *
032 * @see org.olap4j.mdx.NameSegment
033 *
034 * @author jhyde
035 */
036public class KeySegment implements IdentifierSegment {
037    private final List<NameSegment> subSegmentList;
038
039    /**
040     * Creates a KeySegment with one or more sub-segments.
041     *
042     * @param subSegments Array of sub-segments
043     */
044    public KeySegment(NameSegment... subSegments) {
045        if (subSegments.length < 1) {
046            throw new IllegalArgumentException();
047        }
048        this.subSegmentList = UnmodifiableArrayList.asCopyOf(subSegments);
049    }
050
051    /**
052     * Creates a KeySegment a list of sub-segments.
053     *
054     * @param subSegmentList List of sub-segments
055     */
056    public KeySegment(List<NameSegment> subSegmentList) {
057        if (subSegmentList.size() < 1) {
058            throw new IllegalArgumentException();
059        }
060        this.subSegmentList =
061            new UnmodifiableArrayList<NameSegment>(
062                subSegmentList.toArray(
063                    new NameSegment[subSegmentList.size()]));
064    }
065
066    public String toString() {
067        final StringBuilder buf = new StringBuilder();
068        toString(buf);
069        return buf.toString();
070    }
071
072    public void toString(StringBuilder buf) {
073        for (IdentifierSegment segment : subSegmentList) {
074            buf.append('&');
075            segment.toString(buf);
076        }
077    }
078
079    public ParseRegion getRegion() {
080        return IdentifierNode.sumSegmentRegions(subSegmentList);
081    }
082
083    public Quoting getQuoting() {
084        return Quoting.KEY;
085    }
086
087    public String getName() {
088        return null;
089    }
090
091    public List<NameSegment> getKeyParts() {
092        return subSegmentList;
093    }
094}
095
096// End KeySegment.java