001    /*
002    // $Id: TupleType.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.type;
021    
022    import org.olap4j.OlapException;
023    import org.olap4j.metadata.*;
024    
025    /**
026     * Tuple type.
027     *
028     * @author jhyde
029     * @since Feb 17, 2005
030     * @version $Id: TupleType.java 482 2012-01-05 23:27:27Z jhyde $
031     */
032    public class TupleType implements Type {
033        final Type[] elementTypes;
034        private final String digest;
035    
036        /**
037         * Creates a type representing a tuple whose fields are the given types.
038         *
039         * @param elementTypes Array of field types
040         */
041        public TupleType(Type[] elementTypes) {
042            assert elementTypes != null;
043            this.elementTypes = elementTypes.clone();
044    
045            final StringBuilder buf = new StringBuilder("TupleType<");
046            for (int i = 0; i < elementTypes.length; i++) {
047                Type elementType = elementTypes[i];
048                if (i > 0) {
049                    buf.append(", ");
050                }
051                buf.append(elementType.toString());
052            }
053            buf.append(">");
054            digest = buf.toString();
055        }
056    
057        public String toString() {
058            return digest;
059        }
060    
061        public boolean usesDimension(Dimension dimension, boolean maybe) {
062            for (Type elementType : elementTypes) {
063                if (elementType.usesDimension(dimension, maybe)) {
064                    return true;
065                }
066            }
067            return false;
068        }
069    
070        public Dimension getDimension() {
071            return null;
072        }
073    
074        public Hierarchy getHierarchy() {
075            return null;
076        }
077    
078        public Level getLevel() {
079            return null;
080        }
081    
082        // not part of public olap4j API
083        private Type getValueType() throws OlapException {
084            for (Type elementType : elementTypes) {
085                if (elementType instanceof MemberType) {
086                    MemberType memberType = (MemberType) elementType;
087                    if (memberType.getDimension().getDimensionType()
088                        == Dimension.Type.MEASURE)
089                    {
090                        return memberType.getValueType();
091                    }
092                }
093            }
094            return new ScalarType();
095        }
096    
097        // not part of public olap4j API
098        boolean isUnionCompatibleWith(TupleType that) throws OlapException {
099            if (this.elementTypes.length != that.elementTypes.length) {
100                return false;
101            }
102            for (int i = 0; i < this.elementTypes.length; i++) {
103                if (!TypeUtil.isUnionCompatible(
104                        this.elementTypes[i],
105                        that.elementTypes[i]))
106                {
107                    return false;
108                }
109            }
110            return true;
111        }
112    }
113    
114    // End TupleType.java