OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 41 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
41 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.sql.element;
15
 
16
import org.openconcerto.ui.group.Group;
17
import org.openconcerto.utils.StringUtils;
18
 
19
import java.util.ArrayList;
20
import java.util.Collections;
21
import java.util.HashMap;
22
import java.util.List;
23
import java.util.Map;
24
 
25
public class GlobalMapper {
26
    private final Map<String, Object> idToObject = new HashMap<String, Object>();
27
    private final Map<Object, List<String>> objectsToIds = new HashMap<Object, List<String>>();
28
 
29
    private static GlobalMapper instance = new GlobalMapper();
30
 
31
    public GlobalMapper() {
32
 
33
    }
34
 
35
    public static GlobalMapper getInstance() {
36
        return instance;
37
    }
38
 
39
    public void map(String id, Object obj) {
40
        idToObject.put(id, obj);
41
        List<String> l = objectsToIds.get(obj);
42
        if (l == null) {
43
            l = new ArrayList<String>(3);
144 ilm 44
            objectsToIds.put(obj, l);
41 ilm 45
            l.add(id);
144 ilm 46
            objectsToIds.put(obj, l);
41 ilm 47
        } else if (!l.contains(obj)) {
48
            l.add(id);
49
        }
50
    }
51
 
52
    public Object get(String id) {
53
        return idToObject.get(id);
54
    }
55
 
56
    public String getString(String id) {
57
        final Object object = idToObject.get(id);
58
        if (object instanceof String) {
59
            return (String) object;
60
        }
61
        return null;
62
    }
63
 
64
    public Group getGroup(String id) {
65
        final Object object = idToObject.get(id);
66
        if (object instanceof Group) {
67
            return (Group) object;
68
        }
69
        return null;
70
    }
71
 
72
    public List<String> getIds(Object o) {
73
        return objectsToIds.get(o);
74
    }
75
 
76
    public void dump() {
77
        System.out.println(this.getClass().getName());
78
        List<String> ids = new ArrayList<String>();
79
        ids.addAll(this.idToObject.keySet());
80
        Collections.sort(ids);
81
        for (String id : ids) {
82
            System.out.println(StringUtils.leftAlign(id, 40) + " : " + idToObject.get(id));
83
        }
84
        System.out.println(ids.size() + " identifiers found");
85
    }
86
}