OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
25 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.utils.StringUtils;
17
 
18
import java.util.ArrayList;
19
import java.util.Collections;
20
import java.util.HashMap;
21
import java.util.List;
22
import java.util.Map;
23
 
24
public class ElementMapper {
25
    private final Map<String, Object> idToObject = new HashMap<String, Object>();
26
    private final Map<Object, List<String>> objectsToIds = new HashMap<Object, List<String>>();
27
 
28
    private static ElementMapper instance = new ElementMapper();
29
 
30
    public ElementMapper() {
31
 
32
    }
33
 
34
    public static ElementMapper getInstance() {
35
        return instance;
36
    }
37
 
38
    public void map(String id, Object obj) {
39
        idToObject.put(id, obj);
40
        List<String> l = objectsToIds.get(obj);
41
        if (l == null) {
42
            l = new ArrayList<String>(3);
43
            l.add(id);
44
        } else if (!l.contains(obj)) {
45
            l.add(id);
46
        }
47
    }
48
 
49
    public Object get(String id) {
50
        return idToObject.get(id);
51
    }
52
 
53
    public String getString(String id) {
54
        final Object object = idToObject.get(id);
55
        if (object instanceof String) {
56
            return (String) object;
57
        }
58
        return null;
59
    }
60
 
61
    public Group getGroup(String id) {
62
        final Object object = idToObject.get(id);
63
        if (object instanceof Group) {
64
            return (Group) object;
65
        }
66
        return null;
67
    }
68
 
69
    public List<String> getIds(Object o) {
70
        return objectsToIds.get(o);
71
    }
72
 
73
    public void dump() {
74
        System.out.println(this.getClass().getName());
75
        List<String> ids = new ArrayList<String>();
76
        ids.addAll(this.idToObject.keySet());
77
        Collections.sort(ids);
78
        for (String id : ids) {
79
            System.out.println(StringUtils.leftAlign(id, 40) + " : " + idToObject.get(id));
80
        }
81
        System.out.println(ids.size() + " identifiers found");
82
    }
83
}