/* * Copyright 1999-2012 Alibaba Group. * * 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 com.alibaba.dubbo.remoting.exchange.support; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * @author <a href="mailto:gang.lvg@alibaba-inc.com">kimi</a> * @see com.alibaba.dubbo.remoting.transport.MultiMessageHandler */ public final class MultiMessage implements Iterable { public static MultiMessage createFromCollection(Collection collection) { MultiMessage result = new MultiMessage(); result.addMessages(collection); return result; } public static MultiMessage createFromArray(Object... args) { return createFromCollection(Arrays.asList(args)); } public static MultiMessage create() { return new MultiMessage(); } private final List messages = new ArrayList(); private MultiMessage() {} public void addMessage(Object msg) { messages.add(msg); } public void addMessages(Collection collection) { messages.addAll(collection); } public Collection getMessages() { return Collections.unmodifiableCollection(messages); } public int size() { return messages.size(); } public Object get(int index) { return messages.get(index); } public boolean isEmpty() { return messages.isEmpty(); } public Collection removeMessages() { Collection result = Collections.unmodifiableCollection(messages); messages.clear(); return result; } public Iterator iterator() { return messages.iterator(); } }