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) 2003-2005 Julian Hyde
008// Copyright (C) 2005-2009 Pentaho
009// All Rights Reserved.
010*/
011package mondrian.olap;
012
013/**
014 * <code>AxisOrdinal</code> describes the allowable values for an axis code.
015 *
016 * @author jhyde
017 * @since Feb 21, 2003
018 */
019public interface AxisOrdinal {
020    /**
021     * Returns the name of this axis, e.g. "COLUMNS", "SLICER", "AXIS(17)".
022     *
023     * @return Name of the axis
024     */
025    String name();
026
027    /**
028     * Returns the ordinal of this axis.
029     * {@link StandardAxisOrdinal#COLUMNS} = 0,
030     * {@link StandardAxisOrdinal#ROWS} = 1, etc.
031     *
032     * @return ordinal of this axis
033     */
034    int logicalOrdinal();
035
036    /**
037     * Returns whether this is the filter (slicer) axis.
038     *
039     * @return whether this is the filter axis
040     */
041    boolean isFilter();
042
043    public enum StandardAxisOrdinal implements AxisOrdinal {
044        /** No axis.*/
045        NONE,
046
047        /** Slicer axis. */
048        SLICER,
049
050        /** Columns axis (also known as X axis), logical ordinal = 0. */
051        COLUMNS,
052
053        /** Rows axis (also known as Y axis), logical ordinal = 1. */
054        ROWS,
055
056        /** Pages axis, logical ordinal = 2. */
057        PAGES,
058
059        /** Chapters axis, logical ordinal = 3. */
060        CHAPTERS,
061
062        /** Sections axis, logical ordinal = 4. */
063        SECTIONS;
064
065        /**
066         * Returns an axis with a given number.
067         *
068         * <p>If ordinal is greater than 4, returns a non-standard axis called
069         * "AXIS(n)". Never returns null.
070         *
071         * @param ordinal Ordinal
072         * @return Axis
073         */
074        public static AxisOrdinal forLogicalOrdinal(final int ordinal) {
075            if (ordinal + 2 > SECTIONS.ordinal()) {
076                return new AxisOrdinal() {
077                    public String name() {
078                        return "AXIS(" + ordinal + ")";
079                    }
080
081                    public int logicalOrdinal() {
082                        return ordinal;
083                    }
084
085                    public boolean isFilter() {
086                        return false;
087                    }
088                };
089            } else {
090                return values()[ordinal + 2];
091            }
092        }
093
094        public int logicalOrdinal() {
095            return ordinal() - 2;
096        }
097
098        public boolean isFilter() {
099            return this == SLICER;
100        }
101    }
102}
103
104// End AxisOrdinal.java