Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Carsten Ziegeler | 9452bb2 | 2012-06-13 13:08:05 +0000 | [diff] [blame] | 19 | package org.apache.felix.scrplugin.annotations; |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 20 | |
| 21 | import java.util.Map; |
| 22 | |
| 23 | /** |
| 24 | * Base class for all annotation. |
| 25 | */ |
| 26 | public abstract class ScannedAnnotation { |
| 27 | |
| 28 | /** The fully qualified class name */ |
| 29 | protected final String name; |
| 30 | |
| 31 | /** The annotation values. */ |
| 32 | protected final Map<String, Object> values; |
| 33 | |
| 34 | /** |
| 35 | * Create a new description |
| 36 | * @param name The fully qualified class name of the annotation |
| 37 | * @param values The properties of the annotation (optional) |
| 38 | */ |
| 39 | public ScannedAnnotation(final String name, final Map<String, Object> values) { |
| 40 | this.name = name; |
| 41 | this.values = values; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get the fully qualified class name of the annotation. |
| 46 | * @return The fully qualified class name of the annotation. |
| 47 | */ |
| 48 | public String getName() { |
| 49 | return name; |
| 50 | } |
| 51 | |
| 52 | /** |
Carsten Ziegeler | 9452bb2 | 2012-06-13 13:08:05 +0000 | [diff] [blame] | 53 | * Get the simple name of the annotation |
| 54 | */ |
| 55 | public String getSimpleName() { |
| 56 | final int pos = name.lastIndexOf('.'); |
| 57 | return name.substring(pos + 1); |
| 58 | } |
| 59 | |
| 60 | /** |
Carsten Ziegeler | f38b5db | 2014-07-24 13:18:39 +0000 | [diff] [blame] | 61 | * Check if a value exists for this annotation. |
| 62 | * This method can be used to check whether a value exists, |
| 63 | * even if the value is <code>null</code>. |
| 64 | * @param paramName The property name |
| 65 | * @return <code>true</code> If a value exists. |
| 66 | */ |
| 67 | public boolean hasValue(final String paramName) { |
| 68 | if ( values != null ) { |
| 69 | return values.containsKey(paramName); |
| 70 | } |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | /** |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 75 | * Get a property value of the annotation. |
| 76 | * @param paramName The property name. |
| 77 | * @return The value of the property or <code>null</code> |
| 78 | */ |
| 79 | public Object getValue(final String paramName) { |
| 80 | if ( values != null ) { |
| 81 | return values.get(paramName); |
| 82 | } |
| 83 | return null; |
| 84 | } |
| 85 | |
Carsten Ziegeler | 9452bb2 | 2012-06-13 13:08:05 +0000 | [diff] [blame] | 86 | /** |
| 87 | * Get a boolean value of the annotation |
| 88 | * @param name The property name |
| 89 | * @param defaultValue A default value if the property is not set |
| 90 | * @return The property value or the default value. |
| 91 | */ |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 92 | public boolean getBooleanValue(final String name, final boolean defaultValue) { |
| 93 | final Object val = this.getValue(name); |
| 94 | if ( val != null ) { |
| 95 | return ((Boolean) val).booleanValue(); |
| 96 | } |
| 97 | return defaultValue; |
| 98 | } |
| 99 | |
Carsten Ziegeler | 9452bb2 | 2012-06-13 13:08:05 +0000 | [diff] [blame] | 100 | /** |
| 101 | * Get an integer value of the annotation |
| 102 | * @param name The property name |
| 103 | * @param defaultValue A default value if the property is not set |
| 104 | * @return The property value or the default value. |
| 105 | */ |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 106 | public int getIntegerValue(final String name, final int defaultValue) { |
| 107 | final Object val = this.getValue(name); |
| 108 | if ( val != null ) { |
| 109 | return ((Integer) val).intValue(); |
| 110 | } |
| 111 | return defaultValue; |
| 112 | } |
| 113 | |
Carsten Ziegeler | 9452bb2 | 2012-06-13 13:08:05 +0000 | [diff] [blame] | 114 | /** |
| 115 | * Get a long value of the annotation |
| 116 | * @param name The property name |
| 117 | * @param defaultValue A default value if the property is not set |
| 118 | * @return The property value or the default value. |
| 119 | */ |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 120 | public long getLongValue(final String name, final long defaultValue) { |
| 121 | final Object val = this.getValue(name); |
| 122 | if ( val != null ) { |
| 123 | return ((Long) val).intValue(); |
| 124 | } |
| 125 | return defaultValue; |
| 126 | } |
| 127 | |
Carsten Ziegeler | 9452bb2 | 2012-06-13 13:08:05 +0000 | [diff] [blame] | 128 | /** |
| 129 | * Get a string value of the annotation |
| 130 | * @param name The property name |
| 131 | * @param defaultValue A default value if the property is not set |
| 132 | * @return The property value or the default value. |
| 133 | */ |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 134 | public String getStringValue(final String name, final String defaultValue) { |
| 135 | final Object val = this.getValue(name); |
| 136 | if ( val != null && val.toString().trim().length() > 0 ) { |
| 137 | return val.toString().trim(); |
| 138 | } |
| 139 | return defaultValue; |
| 140 | } |
| 141 | |
Carsten Ziegeler | 9452bb2 | 2012-06-13 13:08:05 +0000 | [diff] [blame] | 142 | /** |
| 143 | * Get an enumeration value of the annotation |
| 144 | * @param name The property name |
| 145 | * @param defaultValue A default value if the property is not set |
| 146 | * @return The property value or the default value. |
| 147 | */ |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 148 | public String getEnumValue(final String name, final String defaultValue) { |
| 149 | final Object val = this.getValue(name); |
| 150 | if ( val != null ) { |
Carsten Ziegeler | aa66ac0 | 2012-06-27 15:49:34 +0000 | [diff] [blame] | 151 | if ( val instanceof String[] ) { |
| 152 | return ((String[])val)[1]; |
| 153 | } |
| 154 | if ( val instanceof String[][] ) { |
| 155 | return ((String[][])val)[0][1]; |
| 156 | } |
| 157 | return val.toString(); |
Carsten Ziegeler | 55c96d3 | 2012-06-13 12:03:35 +0000 | [diff] [blame] | 158 | } |
| 159 | return defaultValue; |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public String toString() { |
| 164 | return "AnnotationDescription [name=" + name + ", values=" + values |
| 165 | + "]"; |
| 166 | } |
| 167 | } |