Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +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 | */ |
Pierre De Rop | f74a116 | 2009-12-04 22:40:38 +0000 | [diff] [blame] | 19 | package org.apache.felix.dm.impl; |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 20 | |
| 21 | import java.lang.reflect.InvocationHandler; |
| 22 | import java.lang.reflect.Method; |
| 23 | |
| 24 | |
| 25 | /** |
| 26 | * Default null object implementation. Uses a dynamic proxy. Null objects are used |
| 27 | * as placeholders for services that are not available. |
| 28 | * |
| 29 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 30 | */ |
| 31 | public final class DefaultNullObject implements InvocationHandler { |
| 32 | private static final Boolean DEFAULT_BOOLEAN = Boolean.FALSE; |
| 33 | private static final Byte DEFAULT_BYTE = new Byte((byte) 0); |
| 34 | private static final Short DEFAULT_SHORT = new Short((short) 0); |
| 35 | private static final Integer DEFAULT_INT = new Integer(0); |
| 36 | private static final Long DEFAULT_LONG = new Long(0); |
| 37 | private static final Float DEFAULT_FLOAT = new Float(0.0f); |
| 38 | private static final Double DEFAULT_DOUBLE = new Double(0.0); |
| 39 | |
| 40 | /** |
| 41 | * Invokes a method on this null object. The method will return a default |
| 42 | * value without doing anything. |
| 43 | */ |
| 44 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { |
| 45 | Class returnType = method.getReturnType(); |
| 46 | if (returnType.equals(Boolean.class) || returnType.equals(Boolean.TYPE)) { |
| 47 | return DEFAULT_BOOLEAN; |
| 48 | } |
| 49 | else if (returnType.equals(Byte.class) || returnType.equals(Byte.TYPE)) { |
| 50 | return DEFAULT_BYTE; |
| 51 | } |
| 52 | else if (returnType.equals(Short.class) || returnType.equals(Short.TYPE)) { |
| 53 | return DEFAULT_SHORT; |
| 54 | } |
| 55 | else if (returnType.equals(Integer.class) || returnType.equals(Integer.TYPE)) { |
| 56 | return DEFAULT_INT; |
| 57 | } |
| 58 | else if (returnType.equals(Long.class) || returnType.equals(Long.TYPE)) { |
| 59 | return DEFAULT_LONG; |
| 60 | } |
| 61 | else if (returnType.equals(Float.class) || returnType.equals(Float.TYPE)) { |
| 62 | return DEFAULT_FLOAT; |
| 63 | } |
| 64 | else if (returnType.equals(Double.class) || returnType.equals(Double.TYPE)) { |
| 65 | return DEFAULT_DOUBLE; |
| 66 | } |
| 67 | else { |
| 68 | return null; |
| 69 | } |
| 70 | } |
| 71 | } |