KickJava   Java API By Example, From Geeks To Geeks.

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


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
19
20 package sync4j.framework.core;
21
22 import java.util.*;
23
24
25 /**
26  * Thic class corresponds to the <Sync> command in the SyncML represent DTD
27  *
28  * @author Stefano Fornari @ Funambol
29  *
30  * @version $Id: Sync.java,v 1.5 2005/03/02 20:57:37 harrie Exp $
31  */

32 public final class Sync
33 extends AbstractCommand
34 implements java.io.Serializable JavaDoc {
35     
36     // --------------------------------------------------------------- Constants
37
public static String JavaDoc COMMAND_NAME = "Sync";
38     
39     // ------------------------------------------------------------ Private data
40
private Target target;
41     private Source source;
42     private ArrayList commands = new ArrayList();
43     private Long JavaDoc numberOfChanges;
44     
45     // ------------------------------------------------------------ Constructors
46

47     /** For serialization purposes */
48     protected Sync(){}
49     
50     /**
51      * Creates a new Sync object
52      *
53      * @param cmdID the command identifier - NOT NULL
54      * @param noResp is <b>true</b> if no response is required
55      * @param cred the authentication credential
56      * @param target the target object
57      * @param source the source object
58      * @param meta the meta object
59      * @param numberOfChanges the number of changes
60      * @param commands an array of elements that must be of one of the
61      * following types: {@link Add}, {@link Atomic},
62      * {@link Copy}, {@link Delete}, {@link Replace},
63      * {@link Sequence}
64      *
65      * @throws java.lang.IllegalArgumentException
66      *
67      */

68     public Sync(final CmdID cmdID,
69                 final boolean noResp,
70                 final Cred cred,
71                 final Target target,
72                 final Source source,
73                 final Meta meta,
74                 final Long JavaDoc numberOfChanges,
75                 final AbstractCommand[] commands) {
76         super(cmdID, noResp, meta);
77         
78         setCommands(commands);
79         setCred(cred);
80         
81         this.noResp = (noResp) ? new Boolean JavaDoc(noResp) : null;
82         this.target = target;
83         this.source = source;
84         this.numberOfChanges = numberOfChanges;
85     }
86     
87     // ---------------------------------------------------------- Public methods
88

89     /**
90      * Gets the Target object property
91      *
92      * @return target the Target object property
93      */

94     public Target getTarget() {
95         return target;
96     }
97
98     /**
99      * Sets the Target object property
100      *
101      * @param target the Target object property
102      *
103      */

104     public void setTarget(Target target) {
105         this.target = target;
106     }
107         
108     /**
109      * Gets the Source object property
110      *
111      * @return source the Source object property
112      */

113     public Source getSource() {
114         return source;
115     }
116     
117     /**
118      * Gets the Source object property
119      *
120      * @param source the Source object property
121      */

122     public void setSource(Source source) {
123         this.source = source;
124     }
125
126     /**
127      *
128      * @return The return value is guaranteed to be non-null.
129      * The array elements are guaranteed to be non-null.
130      *
131      */

132     public ArrayList getCommands() {
133         return this.commands;
134     }
135     
136     /**
137      * Sets the sequenced commands. The given commands must be of the allowed
138      * types.
139      *
140      * @param commands the commands - NOT NULL and o the allawed types
141      *
142      * @throws IllegalArgumentException if the constraints are not met
143      */

144     public void setCommands(AbstractCommand[] commands) {
145         if (commands == null) {
146             throw new IllegalArgumentException JavaDoc("commands cannot be null");
147         }
148                 
149         for (int i = 0; i < commands.length; i++) {
150             if (commands[i] == null) {
151                 throw new IllegalArgumentException JavaDoc("commands[" + i +"] cannot be null");
152             } else if ((!(commands[i] instanceof Add))
153                        && (!(commands[i] instanceof Replace))
154                        && (!(commands[i] instanceof Delete))
155                        && (!(commands[i] instanceof Copy))
156                        && (!(commands[i] instanceof Atomic))
157                        && (!(commands[i] instanceof Map))
158                        && (!(commands[i] instanceof Sync))) {
159                 throw new IllegalArgumentException JavaDoc(
160                     "commands[" + i + "] cannot be a " + commands[i].getName()
161                 );
162             }
163         }
164         this.commands.clear();
165         this.commands.addAll(Arrays.asList(commands));
166     }
167     
168     /**
169      * Gets the total number of changes
170      *
171      * @return the total number of changes
172      */

173     public Long JavaDoc getNumberOfChanges() {
174         return numberOfChanges;
175     }
176
177     /**
178      * Sets the numberOfChanges property
179      *
180      * @param numberOfChanges the total number of changes
181      */

182     public void setNumberOfChanges(long numberOfChanges) {
183         this.numberOfChanges = new Long JavaDoc(numberOfChanges);
184     }
185     
186     /**
187      * Sets the numberOfChanges property
188      *
189      * @param numberOfChanges the total number of changes
190      */

191     public void setNumberOfChanges(Long JavaDoc numberOfChanges) {
192         this.numberOfChanges = numberOfChanges;
193     }
194     
195     public String JavaDoc getName() {
196         return Sync.COMMAND_NAME;
197     }
198 }
Popular Tags