summaryrefslogtreecommitdiff
path: root/tutorial/examples/multimethod/ParameterSetTree.java
blob: ad2845b928d4d5e6e0c335da0052844ce0aecfe8 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
 * ParameterSetTree.java
 *
 * @author   Michiaki Tatsubori
 * @version  %VERSION% %DATE%
 * @see      java.lang.Object
 *
 * COPYRIGHT 1999 by Michiaki Tatsubori, ALL RIGHTS RESERVED.
 */
package examples.multimethod;


import java.util.*;
import java.io.*;
import openjava.mop.*;
import openjava.ptree.*;


/**
 * The class <code>ParameterSetTree</code> sorts parameters set of method
 * and hold it.
 *
 * @author   Michiaki Tatsubori
 * @see openjava.mop.OJMethod
 */
public class ParameterSetTree implements Cloneable
{
    private OJClass[] params;
    private OJMethod value = null;
    private ParameterSetTree[] children = new ParameterSetTree[0];

    public ParameterSetTree( OJClass[] params, OJMethod value ) {
    this.params = params;
    this.value = value;
    }

    ParameterSetTree( OJClass[] params ) {
        this( params, null );
    }

    public OJClass[] getParameterTypes() {
    OJClass[] result = new OJClass[params.length];
    System.arraycopy( params, 0, result, 0, params.length );
    return result;
    }

    public OJMethod getValue() {
    return value;
    }

    public ParameterSetTree[] getChildren() {
    if (children.length == 0)  return children;
    ParameterSetTree[] result = new ParameterSetTree[children.length];
    System.arraycopy( children, 0, result, 0, children.length );
    return result;
    }

    public boolean hasChildren() {
    return children.length != 0;
    }

    public String toString() {
        StringWriter str_writer = new StringWriter();
        PrintWriter out = new PrintWriter( str_writer );
    writeState( out );
        out.flush();
        return str_writer.toString();
    }

    public void writeState( PrintWriter out ) {
    writeState( out, 0 );
    }

    private void writeState( PrintWriter out, int level ) {
    for (int i = 0; i < level; ++i)  out.print( "  " );

    out.print( "{ ");
    out.print( params[0].getName() );
    for (int i = 1; i < params.length; ++i) {
        out.print( "," );
        out.print( params[i].getName() );
    }
    out.print( " } " );
    out.println( value != null );

    for (int i = 0; i < children.length; ++i) {
        children[i].writeState( out, level + 1 );
    }
    }

    /* must adjust tree */
    OJMethod add( OJMethod method ) {
        OJClass[] params = method.getParameterTypes();

        if (this.isSameAs( params )) {
        /* exactly same */
        OJMethod result = this.value;
        this.value = method;
        return result;
    }
    if (this.isAssignableFrom( params )) {
        /* a subset of this */
        for (int i = 0; i < children.length; ++i) {
        if (children[i].isAssignableFrom( params )) {
            /* a subset of one of children */
            return children[i].add( method );
        }
        }
        addChild( new ParameterSetTree( params, method ) );
        return null;
    }

    /* a superset or an independent order set of this */
    ParameterSetTree copy = shallowCopy();
    OJClass[] common = commonBaseTypes( this.params, params );
    this.params = common;
    this.value = null;
    if (isAssignable( params, common )) {
        /* a superset */
        this.params = params;
        this.value = method;
        this.children = new ParameterSetTree[]{ copy };
    } else {
        /* a subset of the common superset */
        this.params = common;
        this.value = null;
        this.children = new ParameterSetTree[]{
        copy,
        new ParameterSetTree( params, method ) };
    }
    return null;
    }

    private ParameterSetTree shallowCopy() {
    try {
        return (ParameterSetTree) clone();
    } catch ( CloneNotSupportedException e ){
        e.printStackTrace();
        return null;
    }
    }

    /**
     * Childrens must not have any smaller common set than this set.
     */
    private void addChild( ParameterSetTree newbie ) {
    int tobelong = 0;
    OJClass[] subcommon = null;

        for (; tobelong < children.length; ++tobelong) {
        subcommon = commonBaseTypes( newbie.params,
                     children[tobelong].params );
        if (! isSameAs( subcommon ))  break;
    }

    if (tobelong == children.length) {
        /* simply add */
        ParameterSetTree[] result
        = new ParameterSetTree[children.length + 1];
        System.arraycopy( children, 0, result, 0, children.length );
        result[children.length] = newbie;
        children = result;
        return;
    }

    /* at least two has a sub common signature */
    if (children[tobelong].isSameAs( subcommon )) {
        /* newbie is a subset */
        children[tobelong].addChild( newbie );
    } else {
        ParameterSetTree common_set = new ParameterSetTree( subcommon );
        common_set.addChild( children[tobelong] );
        common_set.addChild( newbie );
        children[tobelong] = common_set;
    }
    }

    public boolean isAssignableFrom( OJClass[] params ) {
    return isAssignable( this.params, params );
    }

    public boolean isSameAs( OJClass[] params ) {
        if (this.params.length != params.length)  return false;
    for (int i = 0; i < this.params.length; ++i) {
        if (this.params[i] != params[i])  return false;
    }
    return true;
    }

    private static boolean isAssignable( OJClass[] p1, OJClass[] p2 ) {
        if (p1.length != p2.length)  return false;
    for (int i = 0; i < p1.length; ++i) {
        if (! p1[i].isAssignableFrom( p2[i] ))  return false;
    }
    return true;
    }

    private static OJClass[] commonBaseTypes( OJClass[] a, OJClass b[] ) {
    OJClass[] result = new OJClass[a.length];
    for (int i = 0; i < a.length; ++i) {
        result[i] = commonBaseType( a[i], b[i] );
    }
    return result;
    }

    /**
     * this is also available in the class <code>Signature</code>
     */
    private static OJClass commonBaseType( OJClass a, OJClass b ) {
        if (a.isAssignableFrom( b ))  return a;
        if (b.isAssignableFrom( a ))  return b;
        return commonBaseType( a.getSuperclass(), b.getSuperclass() );
    }

}