~ubuntu-branches/ubuntu/precise/jcsp/precise

« back to all changes in this revision

Viewing changes to src/org/jcsp/lang/ChannelInt.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-06-20 18:12:26 UTC
  • Revision ID: james.westby@ubuntu.com-20100620181226-8yg8d9rjjjiuy7oz
Tags: upstream-1.1-rc4
ImportĀ upstreamĀ versionĀ 1.1-rc4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
    //////////////////////////////////////////////////////////////////////
 
2
    //                                                                  //
 
3
    //  JCSP ("CSP for Java") Libraries                                 //
 
4
    //  Copyright (C) 1996-2008 Peter Welch and Paul Austin.            //
 
5
    //                2001-2004 Quickstone Technologies Limited.        //
 
6
    //                                                                  //
 
7
    //  This library is free software; you can redistribute it and/or   //
 
8
    //  modify it under the terms of the GNU Lesser General Public      //
 
9
    //  License as published by the Free Software Foundation; either    //
 
10
    //  version 2.1 of the License, or (at your option) any later       //
 
11
    //  version.                                                        //
 
12
    //                                                                  //
 
13
    //  This library is distributed in the hope that it will be         //
 
14
    //  useful, but WITHOUT ANY WARRANTY; without even the implied      //
 
15
    //  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR         //
 
16
    //  PURPOSE. See the GNU Lesser General Public License for more     //
 
17
    //  details.                                                        //
 
18
    //                                                                  //
 
19
    //  You should have received a copy of the GNU Lesser General       //
 
20
    //  Public License along with this library; if not, write to the    //
 
21
    //  Free Software Foundation, Inc., 59 Temple Place, Suite 330,     //
 
22
    //  Boston, MA 02111-1307, USA.                                     //
 
23
    //                                                                  //
 
24
    //  Author contact: P.H.Welch@kent.ac.uk                             //
 
25
    //                                                                  //
 
26
    //                                                                  //
 
27
    //////////////////////////////////////////////////////////////////////
 
28
 
 
29
package org.jcsp.lang;
 
30
 
 
31
import org.jcsp.util.ints.ChannelDataStoreInt;
 
32
 
 
33
/**
 
34
 * This class provides static factory methods for constructing
 
35
 * various different types of int channel objects. There are also methods
 
36
 * for constructing arrays of identical int channels.
 
37
 *
 
38
 * The current implementation constructs "safe" channels which have separate
 
39
 * delegate objects for their read and write ends. This stops a
 
40
 * <code>ChannelInputInt</code> from being cast into a <code>ChannelOutputInt</code>
 
41
 * object. The <code>SafeChannelIntFactory</code> class is used to construct the
 
42
 * channels.
 
43
 *
 
44
 * Non-safe channels can be constructed by using an instance of the
 
45
 * <code>StandardChannelIntFactory</code> class. The channels produced by this
 
46
 * factory have read and write ends implemented by the same object. This is
 
47
 * is more efficient (there are two extra objects and delegate method calls)
 
48
 * but could lead to errors if users make incorrect casts.
 
49
 *
 
50
 * @deprecated To create integer channels, use the methods in the Channel class.
 
51
 *
 
52
 * @author Quickstone Technologies Limited
 
53
 */
 
54
public class ChannelInt
 
55
{
 
56
    /**
 
57
     * Private constructor to stop users from instantiating this class.
 
58
     */
 
59
    private ChannelInt()
 
60
    {
 
61
        //this class should not be instantiated
 
62
    }
 
63
 
 
64
    /**
 
65
     * The factory to be used by this class. The class should implement
 
66
     * ChannelIntFactory, ChannelIntArrayFactory, BufferedChannelIntFactory and BufferedChannelIntArrayFactory.
 
67
     */
 
68
    private static final StandardChannelIntFactory factory = new StandardChannelIntFactory();
 
69
 
 
70
 
 
71
    /* Methods that are the same as the Factory Methods */
 
72
 
 
73
    /**
 
74
     * Constructs and returns a <code>One2OneChannelInt</code> object.
 
75
     *
 
76
     * @return the channel object.
 
77
     *
 
78
     * @see org.jcsp.lang.ChannelIntFactory#createOne2One()
 
79
     */
 
80
    public static One2OneChannelInt createOne2One()
 
81
    {
 
82
        return factory.createOne2One();
 
83
    }
 
84
 
 
85
    /**
 
86
     * Constructs and returns an <code>Any2OneChannelInt</code> object.
 
87
     *
 
88
     * @return the channel object.
 
89
     *
 
90
     * @see org.jcsp.lang.ChannelIntFactory#createAny2One()
 
91
     */
 
92
    public static Any2OneChannelInt createAny2One()
 
93
    {
 
94
        return factory.createAny2One();
 
95
    }
 
96
 
 
97
    /**
 
98
     * Constructs and returns a <code>One2AnyChannelInt</code> object.
 
99
     *
 
100
     * @return the channel object.
 
101
     *
 
102
     * @see org.jcsp.lang.ChannelIntFactory#createOne2Any()
 
103
     */
 
104
    public static One2AnyChannelInt createOne2Any()
 
105
    {
 
106
        return factory.createOne2Any();
 
107
    }
 
108
 
 
109
    /**
 
110
     * Constructs and returns an <code>Any2AnyChannelInt</code> object.
 
111
     *
 
112
     * @return the channel object.
 
113
     *
 
114
     * @see org.jcsp.lang.ChannelIntFactory#createAny2Any()
 
115
     */
 
116
    public static Any2AnyChannelInt createAny2Any()
 
117
    {
 
118
        return factory.createAny2Any();
 
119
    }
 
120
 
 
121
    /**
 
122
     * Constructs and returns an array of <code>One2OneChannelInt</code>
 
123
     * objects.
 
124
     *
 
125
     * @param   n       the size of the array of channels.
 
126
     * @return the array of channels.
 
127
     *
 
128
     * @see org.jcsp.lang.ChannelIntArrayFactory#createOne2One(int)
 
129
     */
 
130
    public static One2OneChannelInt[] createOne2One(int n)
 
131
    {
 
132
        return factory.createOne2One(n);
 
133
    }
 
134
 
 
135
    /**
 
136
     * Constructs and returns an array of <code>Any2OneChannelInt</code>
 
137
     * objects.
 
138
     *
 
139
     * @param   n       the size of the array of channels.
 
140
     * @return the array of channels.
 
141
     *
 
142
     * @see org.jcsp.lang.ChannelIntArrayFactory#createAny2One(int)
 
143
     */
 
144
    public static Any2OneChannelInt[] any2oneArray(int n)
 
145
    {
 
146
        return factory.createAny2One(n);
 
147
    }
 
148
 
 
149
    /**
 
150
     * Constructs and returns an array of <code>One2AnyChannelInt</code>
 
151
     * objects.
 
152
     *
 
153
     * @param   n       the size of the array of channels.
 
154
     * @return the array of channels.
 
155
     *
 
156
     * @see org.jcsp.lang.ChannelIntArrayFactory#createOne2Any(int)
 
157
     */
 
158
    public static One2AnyChannelInt[] createOne2Any(int n)
 
159
    {
 
160
        return factory.createOne2Any(n);
 
161
    }
 
162
 
 
163
    /**
 
164
     * Constructs and returns an array of <code>Any2AnyChannelInt</code>
 
165
     * objects.
 
166
     *
 
167
     * @param   n       the size of the array of channels.
 
168
     * @return the array of channels.
 
169
     *
 
170
     * @see org.jcsp.lang.ChannelIntArrayFactory#createAny2Any(int)
 
171
     */
 
172
    public static Any2AnyChannelInt[] createAny2Any(int n)
 
173
    {
 
174
        return factory.createAny2Any(n);
 
175
    }
 
176
 
 
177
    /**
 
178
     * Constructs and returns a <code>One2OneChannelInt</code> object which
 
179
     * uses the specified <code>ChannelDataStoreInt</code> object as a buffer.
 
180
     *
 
181
     * @param   buffer  the <code>ChannelDataStoreInt</code> to use.
 
182
     * @return the buffered channel.
 
183
     *
 
184
     * @see org.jcsp.lang.BufferedChannelIntFactory#createOne2One(ChannelDataStoreInt)
 
185
     * @see org.jcsp.util.ints.ChannelDataStoreInt
 
186
     */
 
187
    public static One2OneChannelInt createOne2One(ChannelDataStoreInt buffer)
 
188
    {
 
189
        return factory.createOne2One(buffer);
 
190
    }
 
191
 
 
192
    /**
 
193
     * Constructs and returns a <code>Any2OneChannelInt</code> object which
 
194
     * uses the specified <code>ChannelDataStoreInt</code> object as a buffer.
 
195
     *
 
196
     * @param   buffer  the <code>ChannelDataStoreInt</code> to use.
 
197
     * @return the buffered channel.
 
198
     *
 
199
     * @see org.jcsp.lang.BufferedChannelIntFactory#createAny2One(ChannelDataStoreInt)
 
200
     * @see org.jcsp.util.ints.ChannelDataStoreInt
 
201
     */
 
202
    public static Any2OneChannelInt createAny2One(ChannelDataStoreInt buffer)
 
203
    {
 
204
        return factory.createAny2One(buffer);
 
205
    }
 
206
 
 
207
    /**
 
208
     * Constructs and returns a <code>One2AnyChannelInt</code> object which
 
209
     * uses the specified <code>ChannelDataStoreInt</code> object as a buffer.
 
210
     *
 
211
     * @param   buffer  the <code>ChannelDataStoreInt</code> to use.
 
212
     * @return the buffered channel.
 
213
     *
 
214
     * @see org.jcsp.lang.BufferedChannelIntFactory#createOne2Any(ChannelDataStoreInt)
 
215
     * @see org.jcsp.util.ints.ChannelDataStoreInt
 
216
     */
 
217
    public static One2AnyChannelInt createOne2Any(ChannelDataStoreInt buffer)
 
218
    {
 
219
        return factory.createOne2Any(buffer);
 
220
    }
 
221
 
 
222
    /**
 
223
     * Constructs and returns a <code>Any2AnyChannelInt</code> object which
 
224
     * uses the specified <code>ChannelDataStoreInt</code> object as a buffer.
 
225
     *
 
226
     * @param   buffer  the <code>ChannelDataStoreInt</code> to use.
 
227
     * @return the buffered channel.
 
228
     *
 
229
     * @see org.jcsp.lang.BufferedChannelIntFactory#createAny2Any(ChannelDataStoreInt)
 
230
     * @see org.jcsp.util.ints.ChannelDataStoreInt
 
231
     */
 
232
    public static Any2AnyChannelInt createAny2Any(ChannelDataStoreInt buffer)
 
233
    {
 
234
        return factory.createAny2Any(buffer);
 
235
    }
 
236
 
 
237
    /**
 
238
     * Constructs and returns an array of <code>One2OneChannelInt</code> objects
 
239
     * which use the specified <code>ChannelDataStoreInt</code> object as a
 
240
     * buffer.
 
241
     *
 
242
     * @param   buffer  the <code>ChannelDataStoreInt</code> to use.
 
243
     * @param   n           the size of the array of channels.
 
244
     * @return the array of buffered channels.
 
245
     *
 
246
     * @see org.jcsp.lang.BufferedChannelIntArrayFactory#createOne2One(ChannelDataStoreInt, int)
 
247
     * @see org.jcsp.util.ints.ChannelDataStoreInt
 
248
     */
 
249
    public static One2OneChannelInt[] createOne2One(ChannelDataStoreInt buffer, int n)
 
250
    {
 
251
        return factory.createOne2One(buffer, n);
 
252
    }
 
253
 
 
254
    /**
 
255
     * Constructs and returns an array of <code>Any2OneChannelInt</code> objects
 
256
     * which use the specified <code>ChannelDataStoreInt</code> object as a
 
257
     * buffer.
 
258
     *
 
259
     * @param   buffer  the <code>ChannelDataStoreInt</code> to use.
 
260
     * @param   n           the size of the array of channels.
 
261
     * @return the array of buffered channels.
 
262
     *
 
263
     * @see org.jcsp.lang.BufferedChannelIntArrayFactory#createAny2One(ChannelDataStoreInt, int)
 
264
     * @see org.jcsp.util.ints.ChannelDataStoreInt
 
265
     */
 
266
    public static Any2OneChannelInt[] any2oneArray(ChannelDataStoreInt buffer, int n)
 
267
    {
 
268
        return factory.createAny2One(buffer, n);
 
269
    }
 
270
 
 
271
    /**
 
272
     * Constructs and returns an array of <code>One2AnyChannelInt</code> objects
 
273
     * which use the specified <code>ChannelDataStoreInt</code> object as a
 
274
     * buffer.
 
275
     *
 
276
     * @param   buffer  the <code>ChannelDataStoreInt</code> to use.
 
277
     * @param   n           the size of the array of channels.
 
278
     * @return the array of buffered channels.
 
279
     *
 
280
     * @see org.jcsp.lang.BufferedChannelIntArrayFactory#createOne2Any(ChannelDataStoreInt, int)
 
281
     * @see org.jcsp.util.ints.ChannelDataStoreInt
 
282
     */
 
283
    public static One2AnyChannelInt[] createOne2Any(ChannelDataStoreInt buffer, int n)
 
284
    {
 
285
        return factory.createOne2Any(buffer, n);
 
286
    }
 
287
 
 
288
    /**
 
289
     * Constructs and returns an array of <code>Any2AnyChannelInt</code> objects
 
290
     * which use the specified <code>ChannelDataStoreInt</code> object as a
 
291
     * buffer.
 
292
     *
 
293
     * @param   buffer  the <code>ChannelDataStoreInt</code> to use.
 
294
     * @param   n           the size of the array of channels.
 
295
     * @return the array of buffered channels.
 
296
     *
 
297
     * @see org.jcsp.lang.BufferedChannelIntArrayFactory#createAny2Any(ChannelDataStoreInt, int)
 
298
     * @see org.jcsp.util.ints.ChannelDataStoreInt
 
299
     */
 
300
    public static Any2AnyChannelInt[] createAny2Any(ChannelDataStoreInt buffer, int n)
 
301
    {
 
302
        return factory.createAny2Any(buffer, n);
 
303
    }
 
304
 
 
305
    /**
 
306
     * Constructs and returns an array of input channel ends, each of which can be shared by multiple
 
307
     * concurrent readers. The returned array, <code>r</code>, is constructed such that
 
308
     * <code>r[i] = c[i].in ()</code> for <code>0 <= i < c.length</code>.
 
309
     *
 
310
     * @param c the array of channel to obtain input ends from.
 
311
     * @return the array of channel input ends.
 
312
     */
 
313
    public static SharedChannelInputInt[] getInputArray(Any2AnyChannelInt[] c)
 
314
    {
 
315
        SharedChannelInputInt[] in = new SharedChannelInputInt[c.length];
 
316
        for (int i = 0; i < c.length; i++)
 
317
            in[i] = c[i].in();
 
318
        return in;
 
319
    }
 
320
 
 
321
    /**
 
322
     * Constructs and returns an array of input channel ends, each of which can be used as guards
 
323
     * in an <code>Alternative</code>. The returned array, <code>r</code>, is constructed such that
 
324
     * <code>r[i] = c[i].in ()</code> for <code>0 <= i < c.length</code>.
 
325
     *
 
326
     * @param c the array of channel to obtain input ends from.
 
327
     * @return the array of channel input ends.
 
328
     */
 
329
    public static AltingChannelInputInt[] getInputArray(Any2OneChannelInt[] c)
 
330
    {
 
331
        AltingChannelInputInt[] in = new AltingChannelInputInt[c.length];
 
332
        for (int i = 0; i < c.length; i++)
 
333
            in[i] = c[i].in();
 
334
        return in;
 
335
    }
 
336
 
 
337
    /**
 
338
     * Constructs and returns an array of input channel ends, each of which can be shared by multiple
 
339
     * concurrent readers. The returned array, <code>r</code>, is constructed such that
 
340
     * <code>r[i] = c[i].in ()</code> for <code>0 <= i < c.length</code>.
 
341
     *
 
342
     * @param c the array of channel to obtain input ends from.
 
343
     * @return the array of channel input ends.
 
344
     */
 
345
    public static SharedChannelInputInt[] getInputArray(One2AnyChannelInt[] c)
 
346
    {
 
347
        SharedChannelInputInt[] in = new SharedChannelInputInt[c.length];
 
348
        for (int i = 0; i < c.length; i++)
 
349
            in[i] = c[i].in();
 
350
        return in;
 
351
    }
 
352
 
 
353
    /**
 
354
     * Constructs and returns an array of input channel ends, each of which can be used as guards
 
355
     * in an <code>Alternative</code>. The returned array, <code>r</code>, is constructed such that
 
356
     * <code>r[i] = c[i].in ()</code> for <code>0 <= i < c.length</code>.
 
357
     *
 
358
     * @param c the array of channel to obtain input ends from.
 
359
     * @return the array of channel input ends.
 
360
     */
 
361
    public static AltingChannelInputInt[] getInputArray(One2OneChannelInt[] c)
 
362
    {
 
363
        AltingChannelInputInt[] in = new AltingChannelInputInt[c.length];
 
364
        for (int i = 0; i < c.length; i++)
 
365
            in[i] = c[i].in();
 
366
        return in;
 
367
    }
 
368
 
 
369
    /**
 
370
     * Constructs and returns an array of output channel ends, each of which can be shared by multiple
 
371
     * concurrent writers. The returned array, <code>r</code>, is constructed such that
 
372
     * <code>r[i] = c[i].out ()</code> for <code>0 <= i < c.length</code>.
 
373
     *
 
374
     * @param c the array of channel to obtain output ends from.
 
375
     * @return the array of output input ends.
 
376
     */
 
377
    public static SharedChannelOutputInt[] getOutputArray(Any2AnyChannelInt[] c)
 
378
    {
 
379
        SharedChannelOutputInt[] in = new SharedChannelOutputInt[c.length];
 
380
        for (int i = 0; i < c.length; i++)
 
381
            in[i] = c[i].out();
 
382
        return in;
 
383
    }
 
384
 
 
385
    /**
 
386
     * Constructs and returns an array of output channel ends, each of which can be shared by multiple
 
387
     * concurrent writers. The returned array, <code>r</code>, is constructed such that
 
388
     * <code>r[i] = c[i].out ()</code> for <code>0 <= i < c.length</code>.
 
389
     *
 
390
     * @param c the array of channel to obtain output ends from.
 
391
     * @return the array of output input ends.
 
392
     */
 
393
    public static SharedChannelOutputInt[] getOutputArray(Any2OneChannelInt[] c)
 
394
    {
 
395
        SharedChannelOutputInt[] in = new SharedChannelOutputInt[c.length];
 
396
        for (int i = 0; i < c.length; i++)
 
397
            in[i] = c[i].out();
 
398
        return in;
 
399
    }
 
400
 
 
401
    /**
 
402
     * Constructs and returns an array of output channel ends, each of which can only be used by a
 
403
     * single writer. The returned array, <code>r</code>, is constructed such that
 
404
     * <code>r[i] = c[i].out ()</code> for <code>0 <= i < c.length</code>.
 
405
     *
 
406
     * @param c the array of channel to obtain output ends from.
 
407
     * @return the array of output input ends.
 
408
     */
 
409
    public static ChannelOutputInt[] getOutputArray(One2AnyChannelInt[] c)
 
410
    {
 
411
        ChannelOutputInt[] in = new ChannelOutputInt[c.length];
 
412
        for (int i = 0; i < c.length; i++)
 
413
            in[i] = c[i].out();
 
414
        return in;
 
415
    }
 
416
 
 
417
    /**
 
418
     * Constructs and returns an array of output channel ends, each of which can only be used by a
 
419
     * single writer. The returned array, <code>r</code>, is constructed such that
 
420
     * <code>r[i] = c[i].out ()</code> for <code>0 <= i < c.length</code>.
 
421
     *
 
422
     * @param c the array of channel to obtain output ends from.
 
423
     * @return the array of output input ends.
 
424
     */
 
425
    public static ChannelOutputInt[] getOutputArray(One2OneChannelInt[] c)
 
426
    {
 
427
        ChannelOutputInt[] in = new ChannelOutputInt[c.length];
 
428
        for (int i = 0; i < c.length; i++)
 
429
            in[i] = c[i].out();
 
430
        return in;
 
431
    }
 
432
}