001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2005-2005 Julian Hyde
008// Copyright (C) 2005-2011 Pentaho
009// All Rights Reserved.
010*/
011package mondrian.rolap;
012
013import mondrian.calc.TupleList;
014import mondrian.olap.*;
015
016import java.util.AbstractList;
017import java.util.List;
018
019/**
020 * Implementation of the Axis interface.
021 *
022 * @author Richard M. Emberson
023 * @author Julian Hyde
024 */
025public class RolapAxis implements Axis {
026    private final TupleList list;
027
028    public RolapAxis(TupleList list) {
029        this.list = list;
030    }
031
032    public TupleList getTupleList() {
033        return list;
034    }
035
036    public List<Position> getPositions() {
037        return new PositionList(list);
038    }
039
040    public static String toString(Axis axis) {
041        List<Position> pl = axis.getPositions();
042        return toString(pl);
043    }
044
045    public static String toString(List<Position> pl) {
046        StringBuilder buf = new StringBuilder();
047        for (Position p : pl) {
048            buf.append('{');
049            boolean firstTime = true;
050            for (Member m : p) {
051                if (! firstTime) {
052                    buf.append(", ");
053                }
054                buf.append(m.getUniqueName());
055                firstTime = false;
056            }
057            buf.append('}');
058            buf.append('\n');
059        }
060        return buf.toString();
061    }
062
063    /**
064     * List of positions.
065     */
066    private static class PositionList extends AbstractList<Position> {
067        private final TupleList list;
068
069        PositionList(TupleList list) {
070            this.list = list;
071        }
072
073        public boolean isEmpty() {
074            // may be considerably cheaper than computing size
075            return list.isEmpty();
076        }
077
078        public int size() {
079            return list.size();
080        }
081
082        public Position get(int index) {
083            return new PositionImpl(list, index);
084        }
085    }
086
087    /**
088     * Implementation of {@link Position} that reads from a given location in
089     * a {@link TupleList}.
090     */
091    private static class PositionImpl
092        extends AbstractList<Member>
093        implements Position
094    {
095        private final TupleList tupleList;
096        private final int offset;
097
098        PositionImpl(TupleList tupleList, int offset) {
099            this.tupleList = tupleList;
100            this.offset = offset;
101        }
102
103        public Member get(int index) {
104            return tupleList.get(index, offset);
105        }
106
107        public int size() {
108            return tupleList.getArity();
109        }
110    }
111}
112
113// End RolapAxis.java