KickJava   Java API By Example, From Geeks To Geeks.

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


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  * This class represents the <Sequence> tag as defined by the SyncML
26  * representation specifications.
27  * A sequence can contain the following commands: Add, Replace, Delete, Copy,
28  * Atomic, Map, Sync.
29  *
30  * @author Stefano Fornari @ Funambol
31  *
32  * @version $Id: Sequence.java,v 1.4 2005/03/02 20:57:37 harrie Exp $
33  *
34  */

35 public final class Sequence
36 extends AbstractCommand
37 implements java.io.Serializable JavaDoc {
38     
39     // --------------------------------------------------------------- Constants
40

41     public static String JavaDoc COMMAND_NAME = "Sequence";
42     
43     // ------------------------------------------------------------ Private data
44

45     private ArrayList commands = new ArrayList();
46     
47     // ------------------------------------------------------------ Constructors
48

49     /**
50      * For serialization purposes
51      */

52     protected Sequence() {}
53     
54     /**
55      * Create a new Sequence object. The commands in <i>commands</i>
56      * must be of the allowed types.
57      *
58      * @param cmdID command identifier - NOT NULL
59      * @param noResp is &lt;NoREsponse/&gt; required?
60      * @param meta meta information
61      * @param commands the sequenced commands - NOT NULL
62      *
63      * @throws java.lang.IllegalArgumentException is any of the parameters is invalid
64      *
65      */

66     public Sequence(
67                final CmdID cmdID ,
68                final boolean noResp ,
69                final Meta meta ,
70                final AbstractCommand[] commands ) {
71         super(cmdID, noResp);
72
73         setMeta(meta);
74         setCommands(commands);
75     }
76     
77     // ---------------------------------------------------------- Public methods
78

79     /**
80      * Gets an array of AbstractCommand
81      *
82      * @return an array of command objects
83      */

84     public ArrayList getCommands() {
85         return this.commands;
86     }
87     
88     /**
89      * Sets the sequenced commands. The given commands must be of the allowed
90      * types.
91      *
92      * @param commands the commands - NOT NULL and o the allawed types
93      *
94      * @throws IllegalArgumentException if the constraints are not met
95      */

96     public void setCommands(AbstractCommand[] commands) {
97         if (commands == null) {
98             throw new IllegalArgumentException JavaDoc("commands cannot be null");
99         }
100         
101         for (int i = 0; i < commands.length; i++) {
102             if (commands[i] == null) {
103                 throw new IllegalArgumentException JavaDoc("commands[" + i +"] cannot be null");
104             } else if ((!(commands[i] instanceof Add))
105                        && (!(commands[i] instanceof Replace))
106                        && (!(commands[i] instanceof Delete))
107                        && (!(commands[i] instanceof Copy))
108                        && (!(commands[i] instanceof Atomic))
109                        && (!(commands[i] instanceof Map))
110                        && (!(commands[i] instanceof Sync))) {
111                 throw new IllegalArgumentException JavaDoc(
112                     "commands[" + i + "] cannot be a " + commands[i].getName()
113                 );
114             }
115         }
116         this.commands.clear();
117         this.commands.addAll(Arrays.asList(commands));
118     }
119     
120     /**
121      * Returns the command name
122      *
123      * @return the command name
124      */

125     public String JavaDoc getName() {
126         return Sequence.COMMAND_NAME;
127     }
128 }
129
Popular Tags