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