001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.util;
018
019import java.util.AbstractMap;
020import java.util.AbstractSet;
021import java.util.Iterator;
022import java.util.Map;
023import java.util.Set;
024
025import javax.activation.DataHandler;
026
027import org.apache.camel.Attachment;
028import org.apache.camel.impl.DefaultAttachment;
029
030/**
031 * The AttachmentMap class provides a transparent Map<String, DataHandler>
032 * interface for a Map<String, Attachment>
033 */
034public class AttachmentMap extends AbstractMap<String, DataHandler> {
035    private Map<String, Attachment> map;
036
037    public AttachmentMap(Map<String, Attachment> backingMap) {
038        this.map = backingMap;
039    }
040
041    @Override
042    public DataHandler put(String key, DataHandler value) {
043        Attachment old = map.put(key, new DefaultAttachment(value));
044        if (old == null) {
045            return null;
046        } else {
047            return old.getDataHandler();
048        }
049    }
050
051    @Override
052    public Set<Map.Entry<String, DataHandler>> entrySet() {
053        return new AttachmentEntrySet(map.entrySet());
054    }
055
056    public Map<String, Attachment> getOriginalMap() {
057        return map;
058    }
059
060    private static class AttachmentEntrySet extends AbstractSet<Map.Entry<String, DataHandler>> {
061        private Set<Map.Entry<String, Attachment>> set;
062
063        AttachmentEntrySet(Set<Map.Entry<String, Attachment>> set) {
064            this.set = set;
065        }
066
067        @Override
068        public int size() {
069            return set.size();
070        }
071
072        @Override
073        public Iterator<Map.Entry<String, DataHandler>> iterator() {
074            return new AttachmentEntrySetIterator(set.iterator());
075        }
076    }
077
078    private static class AttachmentEntrySetIterator implements Iterator<Map.Entry<String, DataHandler>> {
079        private Iterator<Map.Entry<String, Attachment>> iter;
080
081        AttachmentEntrySetIterator(Iterator<Map.Entry<String, Attachment>> origIterator) {
082            iter = origIterator;
083        }
084
085        @Override
086        public boolean hasNext() {
087            return iter.hasNext();
088        }
089
090        @Override
091        public Map.Entry<String, DataHandler> next() {
092            return new AttachmentEntry(iter.next());
093        }
094
095        public void remove() {
096            iter.remove();
097        }
098    }
099
100    private static class AttachmentEntry implements Map.Entry<String, DataHandler> {
101        private Map.Entry<String, Attachment> entry;
102
103        AttachmentEntry(Map.Entry<String, Attachment> backingEntry) {
104            this.entry = backingEntry;
105        }
106
107        @Override
108        public String getKey() {
109            return entry.getKey();
110        }
111
112        @Override
113        public DataHandler getValue() {
114            Attachment value = entry.getValue();
115            if (value != null) {
116                return value.getDataHandler();
117            }
118            return null;
119        }
120
121        @Override
122        public DataHandler setValue(DataHandler value) {
123            Attachment oldValue = entry.setValue(new DefaultAttachment(value));
124            if (oldValue != null) {
125                return oldValue.getDataHandler();
126            }
127            return null;
128        }
129
130        // two AttachmentEntry objects are equal if the backing entries are equal
131        public boolean equals(Object o) {
132            if (o instanceof AttachmentEntry && entry.equals(((AttachmentEntry)o).entry)) {
133                return true;
134            }
135            return false;
136        }
137
138        public int hashCode() {
139            return entry.hashCode();
140        }
141    }
142}