All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.cxf.attachment.LazyAttachmentCollection Maven / Gradle / Ivy

There is a newer version: 2.7.18
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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 org.apache.cxf.attachment;

import java.io.IOException;
import java.util.AbstractCollection;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.activation.DataHandler;

import org.apache.cxf.message.Attachment;

public class LazyAttachmentCollection 
    implements Collection {
    
    private AttachmentDeserializer deserializer;
    private final List attachments = new ArrayList();
    
    public LazyAttachmentCollection(AttachmentDeserializer deserializer) {
        super();
        this.deserializer = deserializer;
    }

    public List getLoadedAttachments() {
        return attachments;
    }

    private void loadAll() {
        try {
            Attachment a = deserializer.readNext();
            while (a != null) {
                attachments.add(a);
                a = deserializer.readNext();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    /**
     * Check for more attachments by attempting to deserialize the next attachment.
     *
     * @param shouldLoadNew if false, the "loaded attachments" List will not be changed.
     * @return there is more attachment or not
     * @throws IOException
     */
    public boolean hasNext(boolean shouldLoadNew) throws IOException {
        if (shouldLoadNew) {
            Attachment a = deserializer.readNext();
            if (a != null) {
                attachments.add(a);
                return true;
            }
            return false;
        } 
        return deserializer.hasNext();
    }

    public boolean hasNext() throws IOException {
        return hasNext(true);
    }
    public Iterator iterator() {
        return new Iterator() {
            int current;
            boolean removed;
            
            public boolean hasNext() {
                if (attachments.size() > current) {
                    return true;
                }
                
                // check if there is another attachment
                try {
                    Attachment a = deserializer.readNext();
                    if (a == null) {
                        return false;
                    } else {
                        attachments.add(a);
                        return true;
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }

            public Attachment next() {
                Attachment a = attachments.get(current);
                current++;
                removed = false;
                return a;
            }

            public void remove() {
                if (removed) {
                    throw new IllegalStateException();
                }
                attachments.remove(--current);
                removed = true;
            }
            
        };
    }
    
    public int size() {
        loadAll();
        
        return attachments.size();
    }

    public boolean add(Attachment arg0) {
        return attachments.add(arg0);
    }

    public boolean addAll(Collection arg0) {
        return attachments.addAll(arg0);
    }

    public void clear() {
        attachments.clear();   
    }

    public boolean contains(Object arg0) {
        return attachments.contains(arg0);
    }

    public boolean containsAll(Collection arg0) {
        return attachments.containsAll(arg0);
    }

    public boolean isEmpty() {
        if (attachments.isEmpty()) {
            return !iterator().hasNext();
        }
        return attachments.isEmpty();
    }

    public boolean remove(Object arg0) {
        return attachments.remove(arg0);
    }

    public boolean removeAll(Collection arg0) {
        return attachments.removeAll(arg0);
    }

    public boolean retainAll(Collection arg0) {
        return attachments.retainAll(arg0);
    }

    public Object[] toArray() {
        loadAll();
        
        return attachments.toArray();
    }

    public  T[] toArray(T[] arg0) {
        loadAll();
        
        return attachments.toArray(arg0);
    }
    
    public Map createDataHandlerMap() {
        return new LazyAttachmentMap(this);
    }

    private static class LazyAttachmentMap implements Map {
        LazyAttachmentCollection collection;
        
        LazyAttachmentMap(LazyAttachmentCollection c) {
            collection = c;
        }
        
        public void clear() {
            collection.clear();
        }

        public boolean containsKey(Object key) {
            Iterator it = collection.iterator();
            while (it.hasNext()) {
                Attachment at = it.next();
                if (key.equals(at.getId())) {
                    return true;
                }
            }
            return false;
        }

        public boolean containsValue(Object value) {
            Iterator it = collection.iterator();
            while (it.hasNext()) {
                Attachment at = it.next();
                if (value.equals(at.getDataHandler())) {
                    return true;
                }
            }
            return false;
        }

        public DataHandler get(Object key) {
            Iterator it = collection.iterator();
            while (it.hasNext()) {
                Attachment at = it.next();
                if (key.equals(at.getId())) {
                    return at.getDataHandler();
                }
            }
            return null;
        }

        public boolean isEmpty() {
            return collection.isEmpty();
        }
        public int size() {
            return collection.size();
        }
        
        public DataHandler remove(Object key) {
            Iterator it = collection.iterator();
            while (it.hasNext()) {
                Attachment at = it.next();
                if (key.equals(at.getId())) {
                    collection.remove(at);
                    return at.getDataHandler();
                }
            }
            return null;
        }
        public DataHandler put(String key, DataHandler value) {
            Attachment at = new AttachmentImpl(key, value);
            collection.add(at);
            return value;
        }

        public void putAll(Map t) {
            for (Map.Entry ent : t.entrySet()) {
                put(ent.getKey(), ent.getValue());
            }
        }

        
        public Set> entrySet() {
            return new AbstractSet>() {
                public Iterator> iterator() {
                    return new Iterator>() {
                        Iterator it = collection.iterator();
                        public boolean hasNext() {
                            return it.hasNext();
                        }
                        public Map.Entry next() {
                            return new Map.Entry() {
                                Attachment at = it.next();
                                public String getKey() {
                                    return at.getId();
                                }
                                public DataHandler getValue() {
                                    return at.getDataHandler();
                                }
                                public DataHandler setValue(DataHandler value) {
                                    if (at instanceof AttachmentImpl) {
                                        DataHandler h = at.getDataHandler();
                                        ((AttachmentImpl)at).setDataHandler(value);
                                        return h;
                                    } else {
                                        throw new UnsupportedOperationException();
                                    }
                                }
                            };
                        }
                        public void remove() {
                            it.remove();
                        }
                    };
                }
                public int size() {
                    return collection.size();
                }
            };
        }

        public Set keySet() {
            return new AbstractSet() {
                public Iterator iterator() {
                    return new Iterator() {
                        Iterator it = collection.iterator();
                        public boolean hasNext() {
                            return it.hasNext();
                        }

                        public String next() {
                            return it.next().getId();
                        }

                        public void remove() {
                            it.remove();
                        }
                    };
                }

                public int size() {
                    return collection.size();
                }
            };
        }


        public Collection values() {
            return new AbstractCollection() {
                public Iterator iterator() {
                    return new Iterator() {
                        Iterator it = collection.iterator();
                        public boolean hasNext() {
                            return it.hasNext();
                        }
                        public DataHandler next() {
                            return it.next().getDataHandler();
                        }
                        public void remove() {
                            it.remove();
                        }
                    };
                }

                public int size() {
                    return collection.size();
                }
            };
        }
        
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy