001 /* 002 // $Id: //open/mondrian/src/main/mondrian/util/UtilCompatibleJdk15.java#5 $ 003 // This software is subject to the terms of the Common Public License 004 // Agreement, available at the following URL: 005 // http://www.opensource.org/licenses/cpl.html. 006 // Copyright (C) 2007-2008 Julian Hyde 007 // All Rights Reserved. 008 // You must accept the terms of that agreement to use this software. 009 */ 010 package mondrian.util; 011 012 import java.util.Set; 013 import java.util.EnumSet; 014 import java.util.regex.Pattern; 015 import java.math.BigDecimal; 016 // Only in Java5 and above 017 import java.math.MathContext; 018 import java.lang.reflect.Method; 019 import java.lang.reflect.InvocationTargetException; 020 import java.lang.annotation.Annotation; 021 022 /** 023 * Implementation of {@link UtilCompatible} which runs in 024 * JDK 1.5. 025 * 026 * <p>Prior to JDK 1.5, this class should never be loaded. Applications should 027 * instantiate this class via {@link Class#forName(String)} or better, use 028 * methods in {@link mondrian.olap.Util}, and not instantiate it at all. 029 * 030 * @author jhyde 031 * @version $Id: //open/mondrian/src/main/mondrian/util/UtilCompatibleJdk15.java#5 $ 032 * @since Feb 5, 2007 033 */ 034 public class UtilCompatibleJdk15 implements UtilCompatible { 035 public <E extends Enum<E>> Set<E> enumSetOf(E first, E... rest) { 036 return EnumSet.of(first, rest); 037 } 038 039 public <E extends Enum<E>> Set<E> enumSetNoneOf(Class<E> elementType) { 040 return EnumSet.noneOf(elementType); 041 } 042 043 public <E extends Enum<E>> Set<E> enumSetAllOf(Class<E> elementType) { 044 return EnumSet.allOf(elementType); 045 } 046 047 /** 048 * This generates a BigDecimal with a precision reflecting 049 * the precision of the input double. 050 * 051 * @param d input double 052 * @return BigDecimal 053 */ 054 public BigDecimal makeBigDecimalFromDouble(double d) { 055 return new BigDecimal(d, MathContext.DECIMAL64); 056 } 057 058 public String quotePattern(String s) { 059 return Pattern.quote(s); 060 } 061 062 @SuppressWarnings("unchecked") 063 public <T> T getAnnotation( 064 Method method, String annotationClassName, T defaultValue) 065 { 066 try { 067 Class<? extends Annotation> annotationClass = 068 (Class<? extends Annotation>) 069 Class.forName(annotationClassName); 070 if (method.isAnnotationPresent(annotationClass)) { 071 final Annotation annotation = 072 method.getAnnotation(annotationClass); 073 final Method method1 = 074 annotation.getClass().getMethod("value"); 075 return (T) method1.invoke(annotation); 076 } 077 } catch (IllegalAccessException e) { 078 return defaultValue; 079 } catch (InvocationTargetException e) { 080 return defaultValue; 081 } catch (NoSuchMethodException e) { 082 return defaultValue; 083 } catch (ClassNotFoundException e) { 084 return defaultValue; 085 } 086 return defaultValue; 087 } 088 } 089 090 // End UtilCompatibleJdk15.java