1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2014 Red Hat, Inc., and individual contributors
4  * as indicated by the @author tags.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */

18
19 package io.undertow.util;
20
21 import java.util.Collections;
22 import java.util.IdentityHashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import io.undertow.UndertowMessages;
27
28 /**
29  * A thing which can have named attachments.
30  *
31  * @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
32  */

33 public abstract class AbstractAttachable implements Attachable {
34
35     private Map<AttachmentKey<?>, Object> attachments;
36
37     /**
38      * {@inheritDoc}
39      */

40     @Override
41     public <T> T getAttachment(final AttachmentKey<T> key) {
42         if (key == null || attachments == null) {
43             return null;
44         }
45         return (T) attachments.get(key);
46     }
47
48     /**
49      * {@inheritDoc}
50      */

51     @Override
52     public <T> List<T> getAttachmentList(AttachmentKey<? extends List<T>> key) {
53         if (key == null || attachments == null) {
54             return Collections.emptyList();
55         }
56         List<T> list = (List<T>) attachments.get(key);
57         if (list == null) {
58             return Collections.emptyList();
59         }
60         return list;
61     }
62
63     /**
64      * {@inheritDoc}
65      */

66     @Override
67     public <T> T putAttachment(final AttachmentKey<T> key, final T value) {
68         if (key == null) {
69             throw UndertowMessages.MESSAGES.argumentCannotBeNull("key");
70         }
71         if(attachments == null) {
72             attachments = createAttachmentMap();
73         }
74         return (T) attachments.put(key, value);
75     }
76
77     protected Map<AttachmentKey<?>, Object> createAttachmentMap() {
78         return new IdentityHashMap<>(5);
79     }
80
81     /**
82      * {@inheritDoc}
83      */

84     @Override
85     public <T> T removeAttachment(final AttachmentKey<T> key) {
86         if (key == null || attachments == null) {
87             return null;
88         }
89         return (T) attachments.remove(key);
90     }
91
92     /**
93      * {@inheritDoc}
94      */

95     @Override
96     public <T> void addToAttachmentList(final AttachmentKey<AttachmentList<T>> key, final T value) {
97         if (key != null) {
98             if(attachments == null) {
99                 attachments = createAttachmentMap();
100             }
101             final Map<AttachmentKey<?>, Object> attachments = this.attachments;
102             final AttachmentList<T> list = (AttachmentList<T>) attachments.get(key);
103             if (list == null) {
104                 final AttachmentList<T> newList = new AttachmentList<>(((ListAttachmentKey<T>) key).getValueClass());
105                 attachments.put(key, newList);
106                 newList.add(value);
107             } else {
108                 list.add(value);
109             }
110         }
111     }
112
113 }
114