/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2014 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package io.undertow.util;

import java.util.Collections;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;

import io.undertow.UndertowMessages;

/**
 * A thing which can have named attachments.
 *
 * @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
 */
public abstract class AbstractAttachable implements Attachable {

    private Map<AttachmentKey<?>, Object> attachments;

    /**
     * {@inheritDoc}
     */
    @Override
    public <T> T getAttachment(final AttachmentKey<T> key) {
        if (key == null || attachments == null) {
            return null;
        }
        return key.cast(attachments.get(key));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public <T> List<T> getAttachmentList(AttachmentKey<? extends List<T>> key) {
        if (key == null || attachments == null) {
            return Collections.emptyList();
        }
        List<T> list = key.cast(attachments.get(key));
        if (list == null) {
            return Collections.emptyList();
        }
        return list;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public <T> T putAttachment(final AttachmentKey<T> key, final T value) {
        if (key == null) {
            throw UndertowMessages.MESSAGES.argumentCannotBeNull("key");
        }
        if(attachments == null) {
            attachments = createAttachmentMap();
        }
        return key.cast(attachments.put(key, key.cast(value)));
    }

    protected Map<AttachmentKey<?>, Object> createAttachmentMap() {
        return new IdentityHashMap<>(5);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public <T> T removeAttachment(final AttachmentKey<T> key) {
        if (key == null || attachments == null) {
            return null;
        }
        return key.cast(attachments.remove(key));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public <T> void addToAttachmentList(final AttachmentKey<AttachmentList<T>> key, final T value) {
        if (key != null) {
            if(attachments == null) {
                attachments = createAttachmentMap();
            }
            final Map<AttachmentKey<?>, Object> attachments = this.attachments;
            final AttachmentList<T> list = key.cast(attachments.get(key));
            if (list == null) {
                final AttachmentList<T> newList = new AttachmentList<>(((ListAttachmentKey<T>) key).getValueClass());
                attachments.put(key, newList);
                newList.add(value);
            } else {
                list.add(value);
            }
        }
    }

}

@@ -1337,33 +1337,28 @@
      return 
-key.cast(
+(T) 
 attachments.
@@ -1361,25 +1361,24 @@
 nts.get(key)
-)
 ;%0A    %7D%0A%0A   
@@ -1629,33 +1629,34 @@
 t%3CT%3E list = 
-key.cast(
+(List%3CT%3E) 
 attachments.
@@ -1655,33 +1655,32 @@
 chments.get(key)
-)
 ;%0A        if (li
@@ -2116,33 +2116,28 @@
      return 
-key.cast(
+(T) 
 attachments.
@@ -2149,25 +2149,14 @@
 ey, 
-key.cast(
 value)
-))
 ;%0A  
@@ -2491,25 +2491,20 @@
  return 
-key.cast(
+(T) 
 attachme
@@ -2514,25 +2514,24 @@
 .remove(key)
-)
 ;%0A    %7D%0A%0A   
@@ -2938,17 +2938,28 @@
 t = 
-key.cast(
+(AttachmentList%3CT%3E) 
 atta
@@ -2974,17 +2974,16 @@
 get(key)
-)
 ;%0A      

Remove Class.cast()