1 /*
2 * Copyright (c) 2001-2004,
3 * RedVerst Group, ISP RAS http://www.ispras.ru
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * 3. The names "ATP", "TreeDL", "RedVerst", "ISP RAS"
17 * may not be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 // interface Function
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 // add
63 if( count > 0 )
64 {
65 if( this.generator != generator )
66 {
67 /*
68 * function can't be used by more than one
69 * generator simultaneously.
70 */
71 throw new IllegalArgumentException();
72 }
73 } else {
74 this.generator = generator;
75 }
76 count++;
77 } else {
78 // remove
79 if( --count < 0 )
80 {
81 /*
82 * can't free non-linked function
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 // implementation
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 }