KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > core > SyncBody


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package sync4j.framework.core;
19
20 import java.util.*;
21
22 /**
23  * Corresponds to the <SyncBody> element in the SyncML represent DTD
24  *
25  * @author Stefano Fornari @ Funambol
26  * @see SyncHdr
27  *
28  * @version $Id: SyncBody.java,v 1.5 2005/03/02 20:57:37 harrie Exp $
29  */

30 public final class SyncBody
31 implements java.io.Serializable JavaDoc {
32     // ------------------------------------------------------------ Private data
33
private ArrayList commands = new ArrayList();
34     private Boolean JavaDoc finalMsg;
35
36     // ------------------------------------------------------------ Constructors
37

38     /** For serialization purposes */
39     protected SyncBody() {}
40
41     /**
42      * Create a new SyncBody object. The commands in <i>commands</i>
43      * must be of the allowed types.
44      *
45      * @param commands The array elements must be an instance of one of these
46      * classes: {@link Alert}, {@link Atomic}, {@link Copy},
47      * {@link Exec}, {@link Get}, {@link Map}, {@link Put},
48      * {@link Results}, {@link Search}, {@link Sequence},
49      * {@link Status}, {@link Sync}
50      * @param finalMsg is true if this is the final message that is being sent
51      *
52      */

53     public SyncBody( final AbstractCommand[] commands, final boolean finalMsg) {
54
55         setCommands(commands);
56         this.finalMsg = (finalMsg) ? new Boolean JavaDoc(finalMsg) : null;
57     }
58
59     // ---------------------------------------------------------- Public methods
60

61     /**
62      *
63      * @return the return value is guaranteed to be non-null. Also,
64      * the elements of the array are guaranteed to be non-null.
65      *
66      */

67     public ArrayList getCommands() {
68         return this.commands;
69     }
70
71
72     /**
73      * This get method always returns empty ArrayList. This is used
74      * in the JiBX mapping in order to unmarshall commands that could
75      * be followed by the tag &lt;Final/&gt;
76      *
77      * @return empty ArrayList
78      */

79     public ArrayList getEmptyCommands() {
80         return new ArrayList();
81     }
82
83     /**
84      * Add the list of commands to the global ArrayList of commands.
85      * This add method was introduced because into SyncML message
86      * the tag &lt;Final/&gt; could be inserted between the AbstractCommand
87      * and not at the end of SyncBody.
88      *
89      * The given commands must be of the allowed types.
90      *
91      * @param commands the commands - NOT NULL and o the allowed types
92      *
93      * @throws IllegalArgumentException if the constraints are not met
94      */

95     public void addAllCommands(List commands) {
96         if (commands == null) {
97             return;
98         }
99         for (int i = 0; i < commands.size(); i++) {
100             if (commands.get(i) == null) {
101                 throw new IllegalArgumentException JavaDoc("commands[" + i +"] cannot be null");
102             } else if ( (!(commands.get(i) instanceof Alert))
103                        && (!(commands.get(i) instanceof Add))
104                        && (!(commands.get(i) instanceof Atomic))
105                        && (!(commands.get(i) instanceof Copy))
106                        && (!(commands.get(i) instanceof Delete))
107                        && (!(commands.get(i) instanceof Exec))
108                        && (!(commands.get(i) instanceof Get))
109                        && (!(commands.get(i) instanceof Map))
110                        && (!(commands.get(i) instanceof Put))
111                        && (!(commands.get(i) instanceof Replace))
112                        && (!(commands.get(i) instanceof Results))
113                        && (!(commands.get(i) instanceof Search))
114                        && (!(commands.get(i) instanceof Sequence))
115                        && (!(commands.get(i) instanceof Status))
116                        && (!(commands.get(i) instanceof Sync))) {
117                 throw new IllegalArgumentException JavaDoc(
118                     "commands[" + i + "] cannot be a " + ((AbstractCommand)commands.get(i)).getName()
119                 );
120             }
121         }
122
123         this.commands.addAll(commands);
124     }
125
126     /**
127      * Sets the sequenced commands. The given commands must be of the allowed
128      * types.
129      *
130      * @param commands the commands - NOT NULL and o the allowed types
131      *
132      * @throws IllegalArgumentException if the constraints are not met
133      */

134     public void setCommands(AbstractCommand[] commands) {
135         if (commands == null) {
136             throw new IllegalArgumentException JavaDoc("commands cannot be null");
137         }
138         this.commands.clear();
139         addAllCommands(Arrays.asList(commands));
140     }
141
142     /**
143      * Sets the message as final
144      *
145      * @param finalMsg the Boolean value of finalMsg property
146      */

147     public void setFinalMsg(Boolean JavaDoc finalMsg) {
148         this.finalMsg = (finalMsg.booleanValue()) ? finalMsg : null;
149     }
150
151     /**
152      * Gets the value of finalMsg property
153      *
154      * @return true if this is the final message being sent, otherwise false
155      *
156      */

157     public boolean isFinalMsg() {
158         return (finalMsg != null);
159     }
160
161     /**
162      * Gets the value of finalMsg property
163      *
164      * @return true if this is the final message being sent, otherwise null
165      *
166      */

167     public Boolean JavaDoc getFinalMsg() {
168         if (!finalMsg.booleanValue()) {
169             return null;
170         }
171         return finalMsg;
172     }
173 }
Popular Tags