KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > target > EmptyTargetSource


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.aop.target;
18
19 import java.io.Serializable JavaDoc;
20
21 import org.springframework.aop.TargetSource;
22 import org.springframework.util.ObjectUtils;
23
24 /**
25  * Canonical <code>TargetSource</code> when there is no target
26  * (or just the target class known), and behavior is supplied
27  * by interfaces and advisors only.
28  *
29  * @author Rod Johnson
30  * @author Juergen Hoeller
31  */

32 public class EmptyTargetSource implements TargetSource, Serializable JavaDoc {
33
34     /** use serialVersionUID from Spring 1.2 for interoperability */
35     private static final long serialVersionUID = 3680494563553489691L;
36
37
38     //---------------------------------------------------------------------
39
// Static factory methods
40
//---------------------------------------------------------------------
41

42     /**
43      * The canonical (Singleton) instance of this {@link EmptyTargetSource}.
44      */

45     public static final EmptyTargetSource INSTANCE = new EmptyTargetSource(null);
46
47
48     /**
49      * Return an EmptyTargetSource for the given target Class.
50      * @param targetClass the target Class (may be <code>null</code>)
51      * @see #getTargetClass()
52      */

53     public static EmptyTargetSource forClass(Class JavaDoc targetClass) {
54         return (targetClass == null ? INSTANCE : new EmptyTargetSource(targetClass));
55     }
56
57
58     //---------------------------------------------------------------------
59
// Instance implementation
60
//---------------------------------------------------------------------
61

62     private final Class JavaDoc targetClass;
63
64
65     /**
66      * Create a new instance of the {@link EmptyTargetSource} class.
67      * <p>This constructor is <code>private</code> to enforce the
68      * Singleton pattern / factory method pattern.
69      * @param targetClass the target class to expose (may be <code>null</code>)
70      */

71     private EmptyTargetSource(Class JavaDoc targetClass) {
72         this.targetClass = targetClass;
73     }
74
75     /**
76      * Always returns the specified target Class, or <code>null</code> if none.
77      */

78     public Class JavaDoc getTargetClass() {
79         return this.targetClass;
80     }
81
82     /**
83      * Always returns <code>true</code>.
84      */

85     public boolean isStatic() {
86         return true;
87     }
88
89     /**
90      * Always returns <code>null</code>.
91      */

92     public Object JavaDoc getTarget() {
93         return null;
94     }
95
96     /**
97      * Nothing to release.
98      */

99     public void releaseTarget(Object JavaDoc target) {
100     }
101
102
103     /**
104      * Returns the canonical instance on deserialization in case
105      * of no target class, thus protecting the Singleton pattern.
106      */

107     private Object JavaDoc readResolve() {
108         return (this.targetClass == null ? INSTANCE : this);
109     }
110
111     public boolean equals(Object JavaDoc other) {
112         return (this == other || (other instanceof EmptyTargetSource &&
113                 ObjectUtils.nullSafeEquals(this.targetClass, ((EmptyTargetSource) other).targetClass)));
114     }
115
116     public int hashCode() {
117         return EmptyTargetSource.class.hashCode() * 13 + ObjectUtils.nullSafeHashCode(this.targetClass);
118     }
119
120     public String JavaDoc toString() {
121         return "EmptyTargetSource: " +
122                 (this.targetClass != null ? "target class [" + this.targetClass + "]" : "no target class");
123     }
124
125 }
126
Popular Tags