001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.attachment;
019:
020: import java.io.IOException;
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import org.apache.cxf.message.Attachment;
027:
028: public class LazyAttachmentCollection implements Collection<Attachment> {
029: private AttachmentDeserializer deserializer;
030: private final List<Attachment> attachments = new ArrayList<Attachment>();
031:
032: public LazyAttachmentCollection(AttachmentDeserializer deserializer) {
033: super ();
034: this .deserializer = deserializer;
035: }
036:
037: public List<Attachment> getLoadedAttachments() {
038: return attachments;
039: }
040:
041: private void loadAll() {
042: try {
043: Attachment a = deserializer.readNext();
044: while (a != null) {
045: attachments.add(a);
046: a = deserializer.readNext();
047: }
048: } catch (IOException e) {
049: throw new RuntimeException(e);
050: }
051: }
052:
053: public Iterator<Attachment> iterator() {
054: return new Iterator<Attachment>() {
055: int current;
056:
057: public boolean hasNext() {
058: if (attachments.size() > current) {
059: return true;
060: }
061:
062: // check if there is another attachment
063: try {
064: Attachment a = deserializer.readNext();
065: if (a == null) {
066: return false;
067: } else {
068: attachments.add(a);
069: return true;
070: }
071: } catch (IOException e) {
072: throw new RuntimeException(e);
073: }
074: }
075:
076: public Attachment next() {
077: Attachment a = attachments.get(current);
078: current++;
079: return a;
080: }
081:
082: public void remove() {
083: attachments.remove(current);
084: }
085:
086: };
087: }
088:
089: public int size() {
090: loadAll();
091:
092: return attachments.size();
093: }
094:
095: public boolean add(Attachment arg0) {
096: return attachments.add(arg0);
097: }
098:
099: public boolean addAll(Collection<? extends Attachment> arg0) {
100: return attachments.addAll(arg0);
101: }
102:
103: public void clear() {
104: attachments.clear();
105: }
106:
107: public boolean contains(Object arg0) {
108: return attachments.contains(arg0);
109: }
110:
111: public boolean containsAll(Collection<?> arg0) {
112: return attachments.containsAll(arg0);
113: }
114:
115: public boolean isEmpty() {
116: return attachments.isEmpty();
117: }
118:
119: public boolean remove(Object arg0) {
120: return attachments.remove(arg0);
121: }
122:
123: public boolean removeAll(Collection<?> arg0) {
124: return attachments.removeAll(arg0);
125: }
126:
127: public boolean retainAll(Collection<?> arg0) {
128: return attachments.retainAll(arg0);
129: }
130:
131: public Object[] toArray() {
132: loadAll();
133:
134: return attachments.toArray();
135: }
136:
137: public <T> T[] toArray(T[] arg0) {
138: loadAll();
139:
140: return attachments.toArray(arg0);
141: }
142:
143: }
|