KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployers > plugins > attachments > AbstractAttachments


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.deployers.plugins.attachments;
23
24 import java.io.Serializable JavaDoc;
25
26 import org.jboss.deployers.spi.attachments.Attachments;
27
28 /**
29  * AbstractAttachments.
30  *
31  * @author <a HREF="adrian@jboss.com">Adrian Brock</a>
32  * @version $Revision: 1.1 $
33  */

34 public abstract class AbstractAttachments
35    implements Attachments, Serializable JavaDoc
36 {
37    private static final long serialVersionUID = 1;
38
39    public <T> T addAttachment(String JavaDoc name, T attachment, Class JavaDoc<T> expectedType)
40    {
41       if (expectedType == null)
42          throw new IllegalArgumentException JavaDoc("Null expectedType");
43       Object JavaDoc result = addAttachment(name, attachment);
44       if (result == null)
45          return null;
46       return expectedType.cast(result);
47    }
48
49    public <T> T addAttachment(Class JavaDoc<T> type, T attachment)
50    {
51       if (type == null)
52          throw new IllegalArgumentException JavaDoc("Null type");
53       return addAttachment(type.getName(), attachment, type);
54    }
55
56    public <T> T getAttachment(String JavaDoc name, Class JavaDoc<T> expectedType)
57    {
58       if (expectedType == null)
59          throw new IllegalArgumentException JavaDoc("Null expectedType");
60       Object JavaDoc result = getAttachment(name);
61       if (result == null)
62          return null;
63       return expectedType.cast(result);
64    }
65
66    public <T> T getAttachment(Class JavaDoc<T> type)
67    {
68       if (type == null)
69          throw new IllegalArgumentException JavaDoc("Null type");
70       return getAttachment(type.getName(), type);
71    }
72
73    public boolean isAttachmentPresent(String JavaDoc name, Class JavaDoc<?> expectedType)
74    {
75       if (expectedType == null)
76          throw new IllegalArgumentException JavaDoc("Null expectedType");
77       Object JavaDoc result = getAttachment(name);
78       if (result == null)
79          return false;
80       try
81       {
82          expectedType.cast(result);
83       }
84       catch (ClassCastException JavaDoc e)
85       {
86          return false;
87       }
88       return true;
89    }
90
91    public boolean isAttachmentPresent(Class JavaDoc<?> type)
92    {
93       if (type == null)
94          throw new IllegalArgumentException JavaDoc("Null type");
95       return isAttachmentPresent(type.getName(), type);
96    }
97
98    public <T> T removeAttachment(String JavaDoc name, Class JavaDoc<T> expectedType)
99    {
100       if (expectedType == null)
101          throw new IllegalArgumentException JavaDoc("Null expectedType");
102       Object JavaDoc result = removeAttachment(name);
103       if (result == null)
104          return null;
105       return expectedType.cast(result);
106    }
107
108    public <T> T removeAttachment(Class JavaDoc<T> type)
109    {
110       if (type == null)
111          throw new IllegalArgumentException JavaDoc("Null type");
112       return removeAttachment(type.getName(), type);
113    }
114
115    public void clear()
116    {
117       throw new UnsupportedOperationException JavaDoc("You cannot clear attachments here.");
118    }
119 }
120
Popular Tags