1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package com.unitesk.atp.text.generation;
33
34 /***
35 * The default implementation of {@link Function} interface.
36 *
37 * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
38 * @version $Id: DefaultFunction.java,v 1.2 2004/10/11 15:00:43 all-x Exp $
39 */
40 public class DefaultFunction implements Function
41 {
42
43
44
45 /***
46 * Associates or deassociates this function with text generator.
47 * Function can be associated with text generator more than once - under different names.
48 * Function can not be associated with more than one text generator.
49 * So if parameter is not <code>null</code> association counter is incremented,
50 * otherwise - decremented.
51 *
52 * @param generator The specified text generator.
53 * @throws IllegalArgumentException If function is already associated with some generator
54 * and the specified non-null parameter differs from it.
55 * Or if function is not associated and parameter is
56 * <code>null</code>.
57 */
58 public void setGenerator( Generator generator )
59 {
60 if( generator != null )
61 {
62
63 if( count > 0 )
64 {
65 if( this.generator != generator )
66 {
67
68
69
70
71 throw new IllegalArgumentException();
72 }
73 } else {
74 this.generator = generator;
75 }
76 count++;
77 } else {
78
79 if( --count < 0 )
80 {
81
82
83
84 throw new IllegalArgumentException();
85 }
86 if( count == 0 )
87 {
88 this.generator = null;
89 }
90 }
91 }
92
93 /***
94 * Outputs text representation of the specified object to generator
95 * using {@link com.unitesk.atp.text.generation.Generator#txtAsIs(String)}.
96 *
97 * @param arg The specified object.
98 */
99 public void process( Object arg )
100 {
101 generator.txtAsIs( arg.toString() );
102 }
103
104
105
106
107 protected Generator generator;
108 /***
109 * Counts how many times this function
110 * is used by server under different names
111 */
112 int count;
113 }