Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2f5a2c3f8c7487142fc3f4b837609abae904035c (plain) (blame)
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
/*
Copyright (c) 2008 Arno Haase, André Arnold.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html

Contributors:
    Arno Haase - initial API and implementation
    André Arnold
 */
package org.eclipse.xtend.backend.functions;

import java.util.Collection;
import java.util.HashSet;

import org.eclipse.xtend.backend.common.NamedFunction;
import org.eclipse.xtend.backend.common.QualifiedName;
import org.eclipse.xtend.backend.util.Cache;


/**
 * This class collects functions. It removes an old one if a new one with identical signature is
 *  added, allowing overwriting. The check for "identical signature" is performed only if both
 *  functions have no guard. Functions with guards are never treated as having the same signature
 *  because an equality check would then require comparison of guards, a tricky undertaking which
 *  is left for a future version. 
 *  
 * @author Arno Haase (http://www.haase-consulting.com)
 * @author André Arnold
 */
public final class DuplicateAwareNamedFunctionCollection {
    private final Collection<NamedFunction> _allFunctions = new HashSet<NamedFunction>();
    private final Cache<QualifiedName, Collection<NamedFunction>> _byName = new Cache<QualifiedName, Collection<NamedFunction>> () {
        @Override
        protected Collection<NamedFunction> create (QualifiedName key) {
            return new HashSet<NamedFunction>();
        }
    };

    /**
     * registers a function and returns the old function of the same signature if one was replaced, and null otherwise 
     */
    public NamedFunction register (NamedFunction f) {
        NamedFunction result = null;
        
        for (NamedFunction candidate: _byName.get (f.getName())) {
            if (haveSameSignature (f, candidate)) {
                _byName.get (f.getName()).remove (candidate);
                _allFunctions.remove (candidate);
                result = candidate;
                break;
            }
        }
        
        _byName.get (f.getName()).add (f);
        _allFunctions.add (f);
        return result;
    }
    
    
    /**
     * checks if this collection already contains a function with identical signature. 
     */
    public boolean contains (NamedFunction f) {
        for (NamedFunction candidate: _byName.get (f.getName()))
            if (haveSameSignature(f, candidate))
                return true;
        
        return false;
    }

    
    public Collection<NamedFunction> getFunctions () {
        return _allFunctions;
    }
    
    
    /**
     * helper operation to compare two functions.
     */
    public static boolean haveSameSignature (NamedFunction f1, NamedFunction f2) {
        if (f1.getFunction().getGuard() != null || f2.getFunction().getGuard() != null)
            return false;

        if (! f1.getName().equals (f2.getName()))
            return false;
        
        return f1.getFunction().getParameterTypes().equals (f2.getFunction().getParameterTypes());
    }
    
    @Override
    public String toString () {
        return _allFunctions.toString();
    }
} 

Back to the top