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) 2012-2012 Pentaho 008// All Rights Reserved. 009*/ 010package mondrian.olap4j; 011 012import mondrian.olap.OlapElement; 013 014import org.olap4j.OlapWrapper; 015 016import java.sql.SQLException; 017 018/** 019 * Basic features of metadata elements in Mondrian's olap4j driver. 020 * 021 * @author jhyde 022 */ 023abstract class MondrianOlap4jMetadataElement 024 implements OlapWrapper 025{ 026 /** 027 * Helper for {@link #unwrap(Class)} and {@link #isWrapperFor(Class)}. 028 * 029 * @param iface Desired interface 030 * @param <T> Type 031 * @return This as desired interface, or null 032 */ 033 protected <T> T unwrapImpl(Class<T> iface) { 034 if (iface.isInstance(this)) { 035 return iface.cast(this); 036 } 037 final OlapElement element = getOlapElement(); 038 if (element != null && iface.isInstance(element)) { 039 return iface.cast(element); 040 } else { 041 return null; 042 } 043 } 044 045 /** 046 * Returns the Mondrian metadata element inside this wrapper, or null if 047 * there is none. 048 * 049 * @return The Mondrian metadata element, if any 050 */ 051 protected abstract OlapElement getOlapElement(); 052 053 public <T> T unwrap(Class<T> iface) throws SQLException { 054 final T t = unwrapImpl(iface); 055 if (t == null) { 056 throw new SQLException("not a wrapper for " + iface); 057 } 058 return t; 059 } 060 061 public boolean isWrapperFor(Class<?> iface) { 062 return unwrapImpl(iface) != null; 063 } 064} 065 066// End MondrianOlap4jMetadataElement.java