OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
73 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.utils.i18n;
15
 
156 ilm 16
import java.util.Collections;
73 ilm 17
import java.util.LinkedHashMap;
18
import java.util.Map;
19
import java.util.TreeSet;
20
 
156 ilm 21
import com.ibm.icu.text.MessageFormat;
22
 
73 ilm 23
import net.jcip.annotations.ThreadSafe;
24
 
25
/**
26
 * {@link MessageFormat} can take numbered or named arguments, this class unifies both. This class
27
 * is thread safe if the array or map isn't modified.
28
 */
29
@ThreadSafe
30
public final class MessageArgs {
31
 
32
    static protected boolean isOrdered(final Map<?, ?> m) {
33
        return m instanceof LinkedHashMap;
34
    }
35
 
156 ilm 36
    static private final MessageArgs EMPTY = new MessageArgs(new Object[0]);
37
 
38
    public final static MessageArgs getEmpty() {
39
        return EMPTY;
40
    }
41
 
42
    public final static MessageArgs create(final String key, final Object value) {
43
        return new MessageArgs(Collections.singletonMap(key, value));
44
    }
45
 
80 ilm 46
    private final boolean mapPrimary;
73 ilm 47
    private Object[] array;
48
    private Map<String, ?> map;
49
 
50
    public MessageArgs(final Object[] array) {
51
        this(null, array);
52
    }
53
 
54
    public MessageArgs(final Map<String, ?> map) {
55
        this(map, null);
56
    }
57
 
58
    private MessageArgs(final Map<String, ?> map, final Object[] array) {
59
        super();
60
        if (map == null && array == null)
61
            throw new NullPointerException();
80 ilm 62
        this.mapPrimary = map != null;
63
        assert this.mapPrimary == (array == null);
73 ilm 64
        this.array = array;
65
        this.map = map;
66
    }
67
 
80 ilm 68
    public final boolean isMapPrimary() {
69
        return this.mapPrimary;
70
    }
156 ilm 71
 
73 ilm 72
    public final synchronized Object getAll() {
80 ilm 73
        return this.mapPrimary ? this.map : this.array;
73 ilm 74
    }
75
 
80 ilm 76
    protected synchronized Object[] getArray() {
73 ilm 77
        if (this.array == null) {
78
            this.array = new Object[this.map.size()];
79
            int i = 0;
80
            if (isOrdered(this.map)) {
81
                for (final Object v : this.map.values())
82
                    this.array[i++] = v;
83
            } else {
84
                for (final String name : new TreeSet<String>(this.map.keySet()))
85
                    this.array[i++] = this.map.get(name);
86
            }
87
            assert i == this.array.length;
88
        }
89
        return this.array;
90
    }
91
 
92
    protected synchronized Map<String, ?> getMap() {
93
        if (this.map == null) {
94
            final int stop = this.array.length;
156 ilm 95
            if (stop == 0) {
96
                this.map = Collections.emptyMap();
97
            } else {
98
                final Map<String, Object> res = new LinkedHashMap<>(stop);
99
                for (int i = 0; i < stop; i++) {
100
                    res.put(String.valueOf(i), this.array[i]);
101
                }
102
                this.map = Collections.unmodifiableMap(res);
73 ilm 103
            }
104
        }
105
        return this.map;
106
    }
107
 
108
    public final Object getArgument(int i) {
109
        return getArray()[i];
110
    }
111
 
112
    public final Object getArgument(String name) {
113
        return getMap().get(name);
114
    }
115
}